Git CheetSheet
Basic
Automatically stages everything and commit it:
git commit -a
Only staged files are to committed, others remain unstaged:
git commit -m 'message'
See the changes you have made in the working directory:
git diff
See the changes of the staged files:
git diff --staged
Branch
- Create a new branch:
git branch <branchname>
- Switch to that branch:
git checkout <branchname>
- Merge the branch:
git merge <branchname>
- Delete branch, use
-D
to delete branch by ignoring changes:
git delete -d <branchname>
- Create a Branch to track remote branch:
git checkout -b serverfix origin/serverfix
- Use remote branch to replace local branch:
git fetch origin
git reset --hard origin/xxx
- Simply delete remote tracking branch(this will not delete the branch on the remote repo):
git branch -d -r origin/<remote branch name>
- Delete a branch remotely:
# delete branch "foo" in origin remote
git push origin --delete foo
Revert
- Revert changes to working directory:
git checkout .
- Unstage the changes to the staging area:
git reset
- Remove untracked files:
git clean -f
- Remove untracked directories:
git clean -d
Commits
- Change commit message before pushing to remote branch:
git commit --amend
- Add commits without commit record:
git commit --amend --no-edit
- Squash last X commits together:
git rebase -i <after-this-commit>
Then replace pick
on the second and subsequent commits with squash
and chanage the commit message. after-this-commit
means the parent of the oldest commit you want to squash.
- Undo the last commit:
# any changes you made will be removed
git reset --hard HEAD~1
# or
git reset --hard <commitid>
# any changes you made will be kept in workspace
git reset --soft HEAD~1
# or
git reset --soft <commitid>
- Options
--soft
: Staged area and working directory will not be changed;--mixed
: Default. Staged ahea will be changed but working directory will not;--hard
: Any changes to tracked files in the working tree sinceare discarded;
- Undo the last commit you have pushed to remote repo:
# create new commit to revert last commit and push to the remote
git revert HEAD~1..HEAD
# or
git revert <commitid>
git push
- Delete local branch which does not exist in remote:
git remote prune origin
Submodule
- Update all submodules:
git submodule foreach git pull
- Init and update submodules:
git clone --recursive /path/to/repos/foo.git
Equels to:
git clone /path/to/repos/foo.git
git submodule init
git submodule update
-End-