Add
git add -p - interactive commit all your local changes
History
git commit --amend -m - change last commit: discard last commit and save local changes with new description
git reset - reset index (discard all changes with command git add)
git reset file - remove the specified file from the staging area, but leave the working directory unchanged
git reset HEAD~ - explicity reset to specified commit (in this case - last)
git reset --hard 777f - reset to commit 777f, without saving changes and without creating new commit
git revert 777f - revert to commit 777f (only changes in selected commit) and with creating new commit
Difference
git diff - show difference between index and working directory
git diff --staged - show difference between index and current commit
git diff --name-only --diff-filter=U - show files with conflicts after merge
Rebase
git rebase -i HEAD~n - edit history for commit n
git-rebase master topic - apply commits from branch topic on last commit in branch master
Branch
git branch - show current branch
git branch --all - show local and remote branches
git branch -d foo - delete branch with name foo
git branch -m foo - rename current branch to foo
git checkout 777f - switch to commit 777f and save all subsequent commits
git checkout 777f file.txt - recovery file.txt to state in commiet 777f and save all subsequent commits
git checkout some-other-branch - switch to branch some-other-branch
git checkout -b feature - create branch feature and switch to it
git checkout branch2; git merge branch1 - merge branch1 into branch2
Search
git grep 'foo' - search working directory for foo
git grep -e 'first' --and -e 'another' - search working directory for first and second
Show
git blame file - show edit information about file
git log dir/ - show commits that modify files in the directory dir
git log --stat - show commits with aux info: file name and amount of changes on it
git log -p - show log in form of patch
git log --dirstat - show log with commits in directories
git log --pretty=format:'%C(yellow)%h %Cblue%ad %Cgreen%d %Creset%s' --since='2017-08-04' --until='2017-08-11' --date=short - show log for period
git shortlog - group commits by author
git ls-tree -r --name-only 777f - show files that was changed in commit 777f
git show 777f - show changes in commit 777f
git show-ref master - show ID of last commit in all branches
Tags
git tag -l - list all tags
git tag stable2 777f - mark commit 777f with tag stable2
Clean
git clean -f -d - remove all untracked files and directories
Useful patterns
Quickly fix some bug (current branch is master)
git commit -a -m "save master" git checkout -b fixes 777f git commit -a -m "save fixes" git checkout master git merge fixes git diff --merge # show conflicts
Fix wrong commit
# wrong commit git commit # reset to previous state git reset --soft HEAD^ # fixes edit WRONGFILE edit ANOTHERWRONGFILE # staging git add . git commit -c ORIG_HEAD
Check for errors and remove trash
git fsck git count-objects git gc
Emergency fix (with stashing current changes in working directory)
git stash vim file_to_fix.txt git commit -a -m "Fix in a hurry" git stash pop
Updating a forked repository
# add the upstream remote repository git remote add upstream https://github.com/angular/angular.js.git # fetch all files from all branches from the upstream repository git fetch upstream # switch to the branch you're working on git checkout master # rebase to replay all of the deltas from your previous commits on top # of the files you just pulled down with the fetch. git rebase upstream/master # push your changes back up to your own remote fork # the -f flag is to force the push and ignore checks and make sure # that the remote doesn't have issues reconciling which deltas to trust git push -f origin master
Additional information