A repository for commonly used git commands.
$ git config --global -e
$ git config --global user.name "User Name"
$ git config --global user.email "user_email@domain.com"
$ git config user.name "User Name"
$ git config user.email "user_email@domain.com"
$ git config -l
# Windows
$ git config --global credential.helper wincred
# Mac
$ git config --global credential.helper osxkeychain
# Linux
$ git config --global credential.helper cache
$ git init
$ git remote add <aliasName> <remoteAddress>
$ git remote add origin "git@github.com:Aaron-Zhao/git-cookbook.git"
$ git branch <newBranchName>
$ git checkout <branchName>
$ git checkout -b <newBranchName>
$ git fetch
$ git checkout <branchName>
$ git branch -m <newBranchName>
$ git branch -m <oldBranchName> <newBranchName>
# rename locally
$ git branch -m <oldBranchName> <newBranchName>
# delete old remote branch
$ git push <remote> :<oldBranchName>
# push new branch to remote
$ git push <remote> <newBranchName>
$ git branch
$ git add <file>
$ git commit -m 'Your comment'
$ git add .
$ git commit -m 'Your comment'
$ git commit -am 'Your comment'
$ git add --all
$ git commit -m 'Your comment'
$ git commit --amend -m 'New commit message'
$ git status
$ git push <remoteAliasName> <localBranchName>
# To remove all ignored but staged files
git rm -r --cached .
git add .
git commit -m "Removing all files in .gitignore"
Ignoring files (not to be checked in git), refer to this docs for more details.
# To UNDO local unstaged file changes only (for tracked files)
$ git checkout .
# To UNDO a local unstaged file changes
$ git checkout -- <file>
# To UNDO staged file changes or new files only
$ git reset .
$ git reset <file>
$ git rm --cached <file>
$ git rm -r --cached <folder>
# To UNDO local file changes (stage or unstaged) but NOT REMOVE your last commit, then use
$ git reset --hard
# To UNDO local file changes AND REMOVE your last commit, then use
$ git reset --hard HEAD~
# To KEEP local file changes and REMOVE ONLY your last commit, then use
$ git reset --soft HEAD~
# undo x number of commits
$ git reset HEAD~<x>
# push undo to remote
$ git revert HEAD
# remove untracked files
# to see what files will be deleted
$ git clean -n
# delete untracked files
$ git clean -f
# delete directories
$ git clean -fd
# delete ignored files
$ git clean -fX
# delete ignored and non-ignored files
$ git clean -fx
# This create a new commit that reverts all changes made by <commit>
$ git revert <commit>
# list
$ git remote
$ git ls-remote
# verbose
$ git remote -v
# add a new remote repository of your project
$ git remote add <aliasName> <remoteAddress>
# removing an existing remote alias
$ git remote rm <aliasName>
# rename remote alias
$ git remote rename <oldAliasName> <newAliasName>
# update an existing remote URL
$ git remote set-url <aliasName> <NewRemoteAddress>
# clean up remote branches in local
$ git remote prune origin
# test without doing it
$ git remote prune origin --dry-run
# push your new branch to a remote repository
$ git push <aliasName> <branchName>
# pull a remote branch
$ git pull <aliasName> <branchName>
# download new branches and data from a remote repository
$ git fetch <aliasName>
# merge alias to your branch
$ git merge [aliasName/branchName]
# show local, [remote], [all] branches
$ git branch [-r] [-a]
# create and checkout a branch
$ git checkout -b <branchName>
# rename
$ git branch -m <branchName> <newBranchName>
# get back deleted branch before it gets clean up (30 days after deletion)
$ git reflog
$ git branch <newBranchName> <deletedBranchId>
# switching between 2 branches
$ git checkout -
# delete branch with possible error message
$ git branch -d <branchName>
# delete branch without questioning
$ git branch -D <branchName>
# delete remote branch
$ git push -d <remote> <branchName>
# push local branch to remote
$ git push <remote> <branchName>
# force push local branch to remote, if you're the only one working on the branch
$ git push -f <remote> <branchName>
# force with lease otherwise
$ git push --force-with-lease <remote> <branchName>
# pull remote branch to local
$ git pull origin <branchName>
# list branches merged
$ git branch -a --merged
# list branches not merged
$ git branch -a --no-merged
# merge a branch
$ git merge <branchName>
# merge a branch with message
$ git merge <branchName> -m "Your merge message"
# rebase onto a branch
$ git rebase <branchName>
# squash x number of commits into one, follow the instructions to complete it
$ git rebase -i HEAD~<x>
# do if you want update the timestamp after squashing
$ git commit --amend --date="now"
# abort
$ git rebase --abort
$ git merge --abort
# show files in conflicts
$ git diff --name-only --diff-filter=U
# add it to an alias so you can reuse it
$ git config --global alias.conflicts "diff --name-only --diff-filter=U"
$ git conflicts
# to resolve rebase conflicts, first resolve any code conflicts by either accepting their or your changes
# then add those files to staging, you can see them by $ git status
$ git add .
# after that continue rebasing
$ git rebase --continue
# if all you code changes are replaced by their changes, skip rebase and apply your changes manually
$ git rebase --skip
# update remote with
# force push local branch to remote, only if you're the only one working on the branch
$ git push -f <remote> <branchName>
# force with lease otherwise
$ git push --force-with-lease <remote> <branchName>
# revert rebase
# reference log
$ git reflog
# reset to the place right before your rebase
$ git reset --hard HEAD@{x}
# sometimes you would want to branch off a feature branch to do more work that depends on it, while waiting for this feature branch to be reviewed and merged into its parent branch. But what to do after this feature branch is merged into its parent.
# 1. you can make a new branch off the grandparent branch (e.g. master), and cherry pick all the changes you made from your working branch.
# 2. squash all commits of the feature branch into the first commit of your working branch and rebase onto master. This will keep the history clean.
# Notice: potentially master could have some changes made in the places where the feature branch has made changes, and this strategy will overwirte all the changes made by the master in these places. If this is the case, strategy 1. should be used.
# cherry pick commit
$ git cherry-pick <commit>
# resolve conflicts and commit if any
$ git add --all
$ git commit -m "your msg"
For advanced cherry-pick refer to this docs for more details.
# list
$ git tag
# regex
$ git tag -l "v1.8.5*"
# create
$ git tag <tagName>
$ git push <remoteAlias> <tagName>
# create annotated
$ git tag -a v1.4 -m "my version 1.4"
# create and attach to a specific commit
$ git tag -a <tagName> <commitId>
# push a tag
$ git push <remoteAlias> <tagName>
# push all tags
$ git push <remoteAlias> --tags
# pull all tags
git fetch origin --tags
git fetch --all --tags --prune
# check a branch at a tag
$ git checkout -b <branchName> <tagName>
# reset to tag
$ git tag BACKUP
$ git reset --hard BACKUP
# delete local tag
$ git tag -d TAG
# delete remote tag
$ git push --delete origin TAG
# show commits, press 'q' to quit
$ git log
# show commits one line
$ git log --oneline
# search
$ git log -S "search string"
# count commits
$ git log --oneline | wc -l
# commit graph
$ git log --oneline --graph
# count commits by committers
$ git shortlog -s
$ git shortlog -sne
# commits by an author
$ git log --author "Author Name"
# commits by author and search string
$ git log --grep "search string" --author "Author Name"
# commits by time
$ git log --after="2014-02-12T16:36:00-07:00"
$ git log --before="2014-02-12T16:36:00-07:00"
$ git log --until="2014-02-12"
$ git log --since="yesterday"
$ git log --since="1 month ago"
$ git log --since="2 weeks 3 days 2 hours 30 minutes 59 seconds ago"
# stash changes
$ git stash
$ git stash save "message"
# list
$ git stash list
# show changes
$ git stash show -p
# apply
$ git stash apply
$ git stash apply stash@{stash_number}
# drop
$ git stash drop
# apply and drop
$ git stash pop
$ git stash pop stash@{stash_number}
# apply stash to a new branch
$ git stash branch
$ git stash branch <branch> <stash>
# file diff
$ git diff <file>
$ git diff --cached <file>
# commit diff
$ git show <commit>
# show all changes in staged
$ git diff --cached
# list of files to be pushed
$ git diff --stat <remote/branch>
$ git diff --stat origin/master
# code diff of the files to be pushed
$ git diff <remote/branch>
# full file paths of the files that will change, run:
$ git diff --numstat <remote/branch>
# compare branches
$ git diff <branch_1> <branch_2>
$ gitk <branch_1> <branch_2>
# search content in commits
$ git grep -n 'search string'
# return x number of lines before and after of the matches
$ git grep -n -C<x> 'search string'
# only "before"
$ git grep -n -B<x> 'search string'
# only "after"
$ git grep -n -A<x> 'search string'
# show code changes line by line, with authors
$ git blame [file]
$ git blame [file] -l
$ git blame [file] -l > file_change_log.txt
git ssh config file
C:\Program Files\Git\etc\ssh\ssh_config