Skip to content

Instantly share code, notes, and snippets.

@gorshkov-leonid
Last active January 8, 2024 14:25
Show Gist options
  • Save gorshkov-leonid/364024980e394a5b86353cb7a81f3cd2 to your computer and use it in GitHub Desktop.
Save gorshkov-leonid/364024980e394a5b86353cb7a81f3cd2 to your computer and use it in GitHub Desktop.
GIT useful commands

Revision selection

  1. https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection

Compare branches

  1. Get all branches not merged to master git branch --no-merged master (see)

  2. Get all branches merged to master git branch --merged (see)

  3. Remove local branches that have gone remotes git fetch --prune && git branch -lvv | cut -c3- | awk '/: gone]/ {print $1}' | xargs git branch -d (see)

    1. git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done (see)
  4. List of all revisions in branch git rev-list master..aaa (see)

    1. For grandchildren git rev-list ^master ^aaa bbb ([see](https://stackoverflow.com/questions/15589862/how-to-find-all-unmerged-
  5. List of revision not merged to master

    1. git rev-list aaa --not master --no-merges (see)
    2. or git rev-list --first-parent --no-merges master..aaa (see)
      1. ... --reverse commits-in-master-grouped-by-the-branches-they-were-cre))
  6. List of revisions to merge from master git rev-list master --not aaa (see).

  7. Get first accessor and application (see)

    git config --global alias.oldest-ancestor '!zsh -c '\''diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | head -1'\'' -'

    1. All changes in branch
      git config --global alias.branchdiff '!sh -c "git diff `git oldest-ancestor`.."'
      
    2. Al commits in branch
      git config --global alias.branchlog '!sh -c "git log `git oldest-ancestor`.."'
      

Log

  1. Graph log (see)
    log --all --decorate --oneline --graph
    
  2. How to get commits by users
    git shortlog
    # or
    git shortlog -sne
  3. How to count changed lines before commit:
    git diff --numstat
    #or 
    git diff --shortstat
  4. Get commit hash prior another one git rev-list --parents -n 1 42de6ad | awk -F ' ' '{print $2}'
  5. Get branches contain specific commit git branch -a --contains 42de6ad
  6. Nearest parent branch
    git log --pretty=format:'%D' HEAD^ | grep 'origin/' | head -n1 | sed 's@origin/@@' | sed 's@,.*@@'
    
  7. Current branch to variable
    test=`git branch --show-current`
    

Update using pull / rebase

  1. Pull master if curent branch is aaa git pull origin master

  2. Rebase all local commits on the top of master. To be exact move base commit and that apply all local commits on the top of the last one (see)

    ⚠️ do not use it if you have pushed commits

    git pull --rebase origin master
    

Merge

  1. Prepare merge request
    git config --global alias.tomerge '!currentBranch=\`git branch --show-current\`; tempBranch="${currentBranch}_tomerge"; git checkout -b "$tempBranch"; git checkout main; git rebase; git checkout $tempBranch; git rebase main; git reset $(git merge-base main $(git rev-parse --abbrev-ref HEAD)); git add -A'
    
    (see)

Cancel

  1. Cancel merge

Change commits

  1. Change commit author

rln -> ln

find . -name "*" -exec git ls-files -s {} \;
@gorshkov-leonid
Copy link
Author

gorshkov-leonid commented Dec 7, 2021

prepare commit to merge request (see)

  1. calculate base branch
git config --global alias.tomerge '!currentBranch=`git branch --show-current`; tempBranch="${currentBranch}_tomerge"; git checkout -b "$tempBranch"; git checkout main; git rebase; git checkout $tempBranch; git rebase main; git reset $(git merge-base main $(git rev-parse --abbrev-ref HEAD)); git add -A'

currentBranch=`git branch --show-current`
tempBranch=${currentBranch}_tomerge
git checkout -b $tempBranch
## git fetch origin main:main?
git checkout main
git rebase
git checkout $tempBranch
git rebase main
git reset $(git merge-base main $(git rev-parse --abbrev-ref HEAD)) 
git add -A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment