Git cheat sheet saves you from learning all the commands by heart.
Be free to contribute, update the grammar mistakes. You are also free to add your language file.
- Set Up
- Configuration Files
- Create
- Local Changes
- Search
- Commit History
- Move / Rename
- Branches & Tags
- Update & Publish
- Merge & Rebase
- Undo
- Git Flow
$ git config --list
$ git config --local --list
$ git config --global --list
$ git config --system --list
$ git config --global user.name "[firstname lastname]"
$ git config --global user.email "[valid-email]"
$ git config --global color.ui auto
$ git config --global core.editor vi
<repo>/.git/config
~/.gitconfig
/etc/gitconfig
There are two ways:
Via SSH
$ git clone ssh://user@domain.com/repo.git
Via HTTP
$ git clone http://domain.com/user/repo.git
$ git init
$ git init <directory>
$ git status
$ git diff
$ git diff <file>
$ git add .
$ git add -p <file>
$ git add <filename1> <filename2>
$ git commit -a
$ git commit
$ git commit -m 'message here'
$ git commit -am 'message here'
$ git commit --date="`date --date='n day ago'`" -am "<Commit Message Here>"
Don't amend published commits!
$ git commit -a --amend
Don't amend published commits!
$ git commit --amend --no-edit
GIT_COMMITTER_DATE="date" git commit --amend
$ git commit --amend --date="date"
$ git stash
$ git checkout branch2
$ git stash pop
$ git stash apply
- {stash_number} can be obtained from
git stash list
$ git stash apply stash@{stash_number}
$ git stash drop
$ git grep "Hello"
$ git grep "Hello" v2.5
$ git log -S 'keyword'
$ git log -S 'keyword' --pickaxe-regex
Show all commits, starting with newest (it'll show the hash, author information, date of commit and title of the commit):
$ git log
$ git log --oneline
$ git log --author="username"
$ git log -p <file>
$ git log --oneline <origin/master>..<remote/master> --left-right
$ git blame <file>
$ git reflog show
$ git reflog delete
Rename Index.txt to Index.html
$ git mv Index.txt Index.html
$ git branch
$ git branch -a
$ git branch -r
$ git checkout <branch>
$ git checkout <branch> -- <filename>
$ git checkout -b <branch>
$ git checkout -
$ git checkout -b <new_branch> <existing_branch>
$ git checkout <commit-hash> -b <new_branch_name>
$ git branch <new-branch>
$ git branch --track <new-branch> <remote-branch>
$ git branch -d <branch>
$ git branch -m <new_branch_name>
You will lose unmerged changes!
$ git branch -D <branch>
git cherry-pick <commit hash>
$ git tag <tag-name>
$ git tag -a <tag-name>
$ git tag <tag-name> -am 'message here'
$ git tag
$ git tag -n
$ git remote -v
$ git remote show <remote>
$ git remote add <remote> <url>
$ git remote rename <remote> <new_remote>
$ git remote rm <remote>
Note: git remote rm does not delete the remote repository from the server. It simply removes the remote and its references from your local repository.
$ git fetch <remote>
$ git remote pull <remote> <url>
$ git pull origin master
$ git pull --rebase <remote> <branch>
$ git push <remote> <branch>
$ git push <remote> :<branch> (since Git v1.5.0)
OR
$ git push <remote> --delete <branch> (since Git v1.7.0)
$ git push --tags
$ git config --global merge.tool meld
$ git mergetool
$ git merge <branch>
$ git branch --merged
Don't rebase published commit!
$ git rebase <branch>
$ git rebase --abort
$ git rebase --continue
$ git add <resolved-file>
$ git rm <resolved-file>
$ git rebase -i <commit-just-before-first>
Now replace this,
pick <commit_id>
pick <commit_id2>
pick <commit_id3>
to this,
pick <commit_id>
squash <commit_id2>
squash <commit_id3>
$ git reset --hard HEAD
$ git reset HEAD
$ git checkout HEAD <file>
$ git revert <commit>
$ git reset --hard <commit>
$ git reset --hard <remote/branch> e.g., upstream/master, origin/my-feature
$ git reset <commit>
$ git reset --keep <commit>
$ git rm -r --cached .
$ git add .
$ git commit -m "remove xyz file"
Improved Git-flow
$ brew install git-flow-avh
$ port install git-flow
$ sudo apt-get install git-flow
$ wget -q -O - --no-check-certificate https://raw.githubusercontent.com/petervanderdoes/gitflow/develop/contrib/gitflow-installer.sh install <state> | bash
Git flow needs to be initialized in order to customize your project setup. Start using git-flow by initializing it inside an existing git repository:
You'll have to answer a few questions regarding the naming conventions for your branches. It's recommended to use the default values.
git flow init
OR
git flow init -d
git flow feature start MYFEATURE
git flow feature finish MYFEATURE
Are you developing a feature in collaboration? Publish a feature to the remote server so it can be used by other users.
git flow feature publish MYFEATURE
git flow feature pull origin MYFEATURE
git flow feature track MYFEATURE
Support preparation of a new production release. Allow for minor bug fixes and preparing meta-data for a release
To start a release, use the git flow release command. It creates a release branch created from the 'develop' branch. You can optionally supply a [BASE] commit sha-1 hash to start the release from. The commit must be on the 'develop' branch.
git flow release start RELEASE [BASE]
It's wise to publish the release branch after creating it to allow release commits by other developers. Do it similar to feature publishing with the command:
git flow release publish RELEASE
git flow release finish RELEASE
Hotfixes arise from the necessity to act immediately upon an undesired state of a live production version. May be branched off from the corresponding tag on the master branch that marks the production version.
$ git flow hotfix start VERSION [BASENAME]
The version argument hereby marks the new hotfix release name. Optionally you can specify a basename to start from.
By finishing a hotfix it gets merged back into develop and master. Additionally the master merge is tagged with the hotfix version
git flow hotfix finish VERSION