Using CheatSheets To Apply Best Practices

Git Free CheatSheet

Git Free CheatSheet

1.1 Git Advanced

Name Comment
Git squash to make history clean git rebase -i HEAD~4; git push origin <branch-name> --force
Find git branch by commit hash https://${git_repo_link}/commit/${commit_hash}, git name-rev ${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’
Git clone to a specific folder git clone https://github.com/google/cadvisor.git /go/src/github.com/google/cadvisor
Code Owners Define individuals or teams that are responsible for code in a repository.

1.2 Git Remote

Name Comment
Git add a remote source git remote add upstream git@github.com:thoughtbot/dotfiles.git
Git add a local folder as remote git init ., git remote add bak /tmp/test_git_folder

1.3 Git Commit

Name Comment
Git squash to make history clean git rebase -i HEAD~4; git push origin <branch-name> --force
Git amend git commit --amend --no-edit
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’

git-concept.png

1.4 Git Undo

Name Comment
Undo git commit git reset --hard HEAD~1
Undo git pull git reset --hard
Undo git add git reset filename.txt
Undo git merge git merge --abort

1.5 Git log

Name Comment
git sh1sum git log -1 --pretty=format:%h
Check recent commits git log -n 3
Show change content between tow commits git log --pretty=oneline --abbrev-commit 1234...5678
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.6 Git Diff

Name Comment
Compare git diff after commit git diff --cached
Compare two git tags git diff ${sha1sum}..${sha2sum}
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}
Show changeset of the latest commit git diff HEAD^
Show prvious changeset for one file 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

1.7 Git Config

Name Comment
Show git config git config --global/system
Configure default editor export pager=cat, git config --global core.editor nano
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
Reset git url git config --global url."git@github.com:dennyzhang/myrepo.git:".insteadOf "https://github.com/dennyzhang/myrepo/"
Reference GitHub: gitignore examples

1.8 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.9 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.10 Git Submodule

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

1.11 GitHub

1.12 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 *