GIT: Daily commands
Git commands are massive and chances are, you only need a small subset of them in daily usages. So here are the common git commands that I used daily:-
Undo last commit
git reset HEAD~1
git merge --abort
Set color on output
By default git already use color but not on OSX:-
git diff --color
Show diff on index/staging area
git diff --cached # OR git diff --staged
Delete all branch except master
git branch | grep -v master | xargs git branch -d
-D instead to force delete. Sometimes git refused to delete like this
error: The branch '41-messages-too-long' is not fully merged. If you are sure you want to delete it, run 'git branch -D 41-messages-too-long'.
List commits on local but not yet on remote (kind of
git log origin/master..master
Git add patch
git add -p I think is pretty well known but I’ve just discovered one of it’s very handy option
Stage this hunk [y,n,q,a,d,/,K,g,e,?]? s y - stage this hunk n - do not stage this hunk q - quit; do not stage this hunk nor any of the remaining ones a - stage this hunk and all later hunks in the file d - do not stage this hunk nor any of the later hunks in the file g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print help
So the proposed hunk might contain 2 unrelated changes you don’t want to commit together. The
s option allow you to split the hunk into much smaller hunk.
Stash untracked files
git stash save -u
Discovered this today. I’m on a branch and there’s some changes there that I want to commit to master. Git won’t allow you to checkout
master unless you commit all the changes, or stash them. But there’s only certain hunk that I want to commit to master, while the rest should be committed to the current branch (when ready). So it would be great if I can stash all the changes into proper hunks that I want to commit.
git stash -p is the equivalent of
git add -p for stashing.