Cheatsheet of Git Commands
- Set Up
- Configuration Files
- Create
- Local Changes
- Search
- Commit History
- Branches & Tags
- Update & Publish
- Merge & Rebase
- Undo
$ 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://[email protected]/repo.git
Via HTTP
$ git clone http://domain.com/user/repo.git
$ git init
$ git status
$ git diff
$ git add
$ git add -p <file>
$ 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"
Don't amend published commits!
$ git commit -a --amend
git stash
git checkout branch2
git stash pop
git stash apply
$ git grep "Hello"
$ git grep "Hello" v2.5
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 branch
$ git branch -r
$ git checkout <branch>
$ git checkout -b <branch>
$ git branch <new-branch>
$ git branch --track <new-branch> <remote-branch>
$ git branch -d <branch>
You will lose unmerged changes!
$ git branch -D <branch>
$ git tag <tag-name>
$ git tag -a <tag-name>
$ git remote -v
$ git remote show <remote>
$ git remote add <remote> <url>
$ git fetch <remote>
$ git remote pull <remote> <url>
$ git pull origin master
git pull --rebase <remote> <branch>
$ git push remote <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 merge <branch>
Don't rebase published commit!
$ git rebase <branch>
$ git rebase --abort
$ git rebase --continue
$ git mergetool
$ 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"