Using CheatSheets To Apply Best Practices

Git CheatSheet

Git CheatSheet

1.1 Git Advanced

Name Comment
Git squash to make history clean git rebase -i HEAD~4; git push origin <branch-name> --force link
Find git branch by commit hash git name-rev ${commit_hash}, or https://${git_repo_link}/commit/${commit_hash}
List all remote git branches git ls-remote --heads origin
Git clone repo with another ssh key ssh-agent bash -c ‘ssh-add yourkey; git clone git@github.com:user/project.git’

1.2 Git Commit

Name Comment
Git squash to make history clean git rebase -i HEAD~4; git push origin <branch-name> --force link
Git commit and squash git commit --amend --no-edit, git push origin <branch-name> --force
Git add patch git add -p
Git commit git commit -m ‘XXX’
Git add and commit git ci -m ‘XXX’

1.3 Git Undo

Name Comment
Undo git commit git reset --hard HEAD~1
Undo git pull git reset --hard
Undo git merge git merge --abort

1.4 Git log

Name Comment
Check recent commits git log -n 3
Check commit by username git log origin/master -n 3 --author <denny>
Check changes for a given file from a given user git log origin/master -n 3 --author <denny> <somefile.py>

1.5 Git Diff

Name Comment
Compare git diff after commit git diff --cached
Show changed file list git diff --name-status, git diff --name-status --cached
Git diff two revision git diff ${sha1sum}..${sha2sum}
Git show file changes git diff --name-only ${sha1sum} ${sha2sum}
Compare with previous version git diff HEAD^ default.rb
Compare two branches in CLI git diff <branch_1>..<branch_2> Make sure you have those branches locally
Compare two revision in GitHub UI https://github.com/…/…/compare/sha1…sha2
Compare latest 3 commits in GitHub UI https://github.com/dennyzhang/cheatsheet-git-a4/compare/HEAD~3…HEAD
Configure default editor export pager=cat

1.6 Git Branch

Name Comment
List all remote git branches git ls-remote --heads origin
Delete local branch git branch -d <branch_name>
Delete remote branch git push origin --delete <branch_name>

1.7 Git Tag

Name Comment
Git list all tags git ls-remote --tags
Git Fetch all tags git fetch --tags; git checkout tags/<tag_name>
Git delete local tag git tag -d <tag_name>
Git delete remote tag git push --delete origin <tag_name>

1.8 Git Config

Name Comment
Show git config git config --global/system
Edit git global config git config --global --edit
Alias for git status git config --global alias.st status Link: git aliases
Alias for git checkout git config --global alias.co checkout
Alias for git commit git config --global alias.ci commit

1.9 Git Submodule

Name Comment
Git add a repo to current repo git submodule add <git_repo_url>
Update submodule git submodule update

1.10 GitHub

Name Comment
Generate link for a code block link
Git clone wiki link
Generate TOC gh-md-toc
Github Shortcut link
  • GitHub/GitLab markdown image size
<img src="images/pks_logging_components.png" width="700x">

![](/uploads/d19fcc3d3b4d313c8cd7960a343463b6/table.png)
<img src="/uploads/d19fcc3d3b4d313c8cd7960a343463b6/table.png"  width="120" height="120">

![](./pic/pic1_50.png =100x20)
You can skip the HEIGHT

![](./pic/pic1s.png =250x)

1.11 More Resources

https://github.com/git-tips/tips

License: Code is licensed under MIT License.


linkedin
github
slack




Leave a Reply

Your email address will not be published. Required fields are marked *