CheatSheet For Git & GitHub
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

1 Git CheatSheet


PRs Welcome

File me Issues or star this repo.

1.1 Git Advanced

Git squash to make history cleangit rebase -i HEAD~4; git push origin <branch-name> --force link
Find git branch by commit hashgit name-rev ${commit_hash}, or https://${git_repo_link}/commit/${commit_hash}
List all remote git branchesgit ls-remote --heads origin
Git clone repo with another ssh keyssh-agent bash -c ‘ssh-add yourkey; git clone’

1.2 Git Commit

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

1.3 Git Undo

Undo git commitgit reset --hard HEAD~1
Undo git pullgit reset --hard
Undo git mergegit merge --abort
Undo git addgit reset filename.txt

1.4 Git log

Check recent commitsgit log -n 3
Check commit by usernamegit log origin/master -n 3 --author <denny>
Check changes for a given file from a given usergit log origin/master -n 3 --author <denny> <>

1.5 Git Diff

Compare git diff after commitgit diff --cached
Compare two git tagsgit diff ${sha1sum}..${sha2sum}
Show changed file listgit diff --name-status, git diff --name-status --cached
Git diff two revisiongit diff ${sha1sum}..${sha2sum}
Git show file changesgit diff --name-only ${sha1sum} ${sha2sum}
Show changeset of the latest commitgit diff HEAD^
Show prvious changeset for one filegit diff HEAD^ default.rb
Compare two branches in CLIgit diff <branch_1>..<branch_2> Make sure you have those branches locally
Compare two revision in GitHub UI…/…/compare/sha1…sha2
Compare latest 3 commits in GitHub UI…HEAD

1.6 Git Config

Show git configgit config --global/system
Configure default editorexport pager=cat
Edit git global configgit config --global --edit
Alias for git statusgit config --global status Link: git aliases
Alias for git checkoutgit config --global checkout
Alias for git commitgit config --global commit
ReferenceGitHub: gitignore examples

1.7 Git Branch

List all remote git branchesgit ls-remote --heads origin
Delete local branchgit branch -d <branch_name>
Delete remote branchgit push origin --delete <branch_name>

1.8 Git Tag

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

1.9 Git Submodule

Git add a repo to current repogit submodule add <git_repo_url>
Update submodulegit submodule update

1.10 GitHub

Github ShortcutLink: Using keyboard shortcuts
Generate TOCgh-md-toc
Referencelink: generate link for code block, link: git clone wiki repo

1.11 More Resources

License: Code is licensed under MIT License.

linkedin github slack