To create a new branch from the current branch and check it out in one command:
git checkout -b my-branch
To delete a local branch that has been merged:
git branch -d my-branch
If the branch is unmerged but you're sure you want to delete it, use the -D
flag instead.
To delete a remote branch:
git push origin :my-branch # notice the colon
git reset --hard <sha> # the SHA you want to reset to
Create a patch from a git commit:
git format-patch -1 <sha> # for specific SHA
git format-patch -1 HEAD # for commit at HEAD
Apply the patch:
patch -p1 < file.patch
To review individual change "chunks", do the following:
git add -p
Add a chunk with a
, skip a chunk with n
, split it with s
and stop adding chunks with q
.
To learn more about available sub commands, type ?
.
Compare one branch with another
git diff master...other-branch
git commit --amend
git revert HEAD^ # or HEAD^1, HEAD^2... HEAD^n for the previous 2, 3 or n+1 commits
To temporarily save changes with committing, you can stash:
git stash
Then when you're ready to re-apply your changes, you can pop:
git stash pop
To take a set of commits you've made locally and squash them into one history, try interactive rebase:
git rebase -i master
This will take all your changes that are on top of the master
branch and then allow you to choose which commits you want to merge together. Note that this changes your repo history, so be careful and read up on this before attempting.
To create a new tag at the current point in the repository history:
git tag v1.2 # or "2014-04-16" or "cats" or whatever you want...
To delete a tag:
git tag -d v1.2 # or whatever tag you want to remove
Push your local tags to the remote repo:
git push --tags