Branch
Misc
Remove gone branches
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D
Replace local branch with origin
git reset --hard origin/featureXXX
Merge
Check if commit merged:
git fetch origin # ??
git branch -r --contains <commit>
Merged branches
git branch --merged master # local branches
git branch -r --merged master # remote branches
git branch -a --merged master # all branches
Not merged branches
git branch --no-merged master # local branches
git branch -r --no-merged master # remote branches
git branch -a --no-merged master # all branches
--dry-run
git merge --no-commit --no-ff my_branch
git diff --cached
git merge --abort
Squash
Merge branch as a new single commit:
git checkout master
git merge --squash bugfix
git commit
Remove
git branch -a # show list of branches
git branch -d <branch_name> # remove “<branch_name>” branch locally
git push origin --delete <branch_name> # remove remote “<branch_name>” branch
git branch -a # show list of branches to ensure “<branch_name>” is removed
Rename
git branch -m old-name new-name # rename
git push origin :old-name new-name # push to origin
git push origin -u new-name # add upstream (tracking) reference
Rebase
git checkout master
git pull
git checkout my_brarch
git rebase -i master # pick first comment and fixup the rest ones
git push
#git push -f # if changed were already pushed to remote
Rebase onto another branch
Precondition:
git checkout master
git checkout -b feature_1 # create a branch from master
# add & commit changes
git checkout -b feature_2 # create a branch from feature_1
# add & commit changes
feature_2
onto master
branch:
git checkout feature_2
git rebase --onto master feature_1
Rebase onto a rebased branch
git rebase --onto feature1 feature1@{1} feature2
Reverting git rebase
git reflog
git reset --hard HEAD@{2} # be absolutely sure before resetting with the --hard option
Undoing a 'git push'
git push -f origin last_known_good_commit:branch_name
Copy commit to a new branch:
git checkout -b new_branch_name
git cherry-pick <commit>
Back branch to the last good commit:
git checkout branch_name
git reset --hard <last_good_commit>
git push -f