0% found this document useful (0 votes)
75 views2 pages

Cheatsheet Git A4

This document is a cheat sheet for Git that provides commands and descriptions for advanced Git topics. It covers commands and explanations for Git operations like squashing commits, finding branches by commit hash, cloning repositories, adding and removing remotes, committing, undoing commits, logging history, comparing diffs, configuring settings, working with branches and tags, using submodules, and more GitHub resources. The cheat sheet is periodically updated and provides links to additional documentation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views2 pages

Cheatsheet Git A4

This document is a cheat sheet for Git that provides commands and descriptions for advanced Git topics. It covers commands and explanations for Git operations like squashing commits, finding branches by commit hash, cloning repositories, adding and removing remotes, committing, undoing commits, logging history, comparing diffs, configuring settings, working with branches and tags, using submodules, and more GitHub resources. The cheat sheet is periodically updated and provides links to additional documentation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Blog URL: https://cheatsheet.dennyzhang.

com/cheatsheet-git-A4 Updated: June 7, 2020

1 Git CheatSheet Tools


• PDF Link: cheatsheet-git-A4.pdf, Category: tools
• Blog URL: https://cheatsheet.dennyzhang.com/cheatsheet-git-A4
• Related posts: Visual Studio CheatSheet

File me Issues or star this repo.

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://\protect\T1\textdollar\{git_repo_link\}/commit/\protect\T1\textdolla
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 [email protected]:user/project.git’
Git clone to a specific folder git clone https://github.com/google/cadvisor.git /go/src/github.com/google/c
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 [email protected]: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’

https://raw.githubusercontent.com/dennyzhang/cheatsheet-git-A4/master/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>

GitHub: https://github.com/dennyzhang/cheatsheet-git-A4 1 of 2
Blog URL: https://cheatsheet.dennyzhang.com/cheatsheet-git-A4 Updated: June 7, 2020

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."[email protected]:dennyzhang/myrepo.git:".insteadOf "https://git
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
Name Comment
Github Shortcut Link: Using keyboard shortcuts
Generate TOC gh-md-toc
Reference link: generate link for code block, link: git clone wiki repo

1.12 More Resources


https://github.com/git-tips/tips
License: Code is licensed under MIT License.

GitHub: https://github.com/dennyzhang/cheatsheet-git-A4 2 of 2

You might also like