CheatSheet For Git & GitHub
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
misc
.gitignore
README.org
cheatsheet-git-A4.pdf
git-concept.png

README.org

1 Git CheatSheet

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

1.1 Git Advanced

NameComment
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 git@github.com:user/project.git’

1.2 Git Commit

NameComment
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’

https://raw.githubusercontent.com/dennyzhang/cheatsheet-git-A4/master/git-concept.png

1.3 Git Undo

NameComment
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

NameComment
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> <somefile.py>

1.5 Git Diff

NameComment
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 UIhttps://github.com/…/…/compare/sha1…sha2
Compare latest 3 commits in GitHub UIhttps://github.com/dennyzhang/cheatsheet-git-a4/compare/HEAD~3…HEAD

1.6 Git Config

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

1.7 Git Branch

NameComment
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

NameComment
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

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

1.10 GitHub

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

1.11 More Resources

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

License: Code is licensed under MIT License.

linkedin github slack