Skip to content

dennyzhang/cheatsheet-git-A4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
Find git branch by commit hashhttps://${git_repo_link}/commit/${commit_hash}, git name-rev ${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 [email protected]:user/project.git’
Git clone to a specific foldergit clone https://github.com/google/cadvisor.git /go/src/github.com/google/cadvisor
Code OwnersDefine individuals or teams that are responsible for code in a repository.

1.2 Git Remote

NameComment
Git add a remote sourcegit remote add upstream [email protected]:thoughtbot/dotfiles.git
Git add a local folder as remotegit init ., git remote add bak /tmp/test_git_folder

1.3 Git Commit

NameComment
Git squash to make history cleangit rebase -i HEAD~4; git push origin <branch-name> --force
Git amendgit commit --amend --no-edit
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.4 Git Undo

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

1.5 Git log

NameComment
git sh1sumgit log -1 --pretty=format:%h
Check recent commitsgit log -n 3
Show change content between tow commitsgit log --pretty=oneline --abbrev-commit 1234...5678
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.6 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.7 Git Config

NameComment
Show git configgit config --global/system
Configure default editorexport pager=cat, git config --global core.editor nano
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
Reset git url=git config –global url.”[email protected]:dennyzhang/myrepo.git:”.insteadOf “https://github.com/dennyzhang/myrepo/”=
ReferenceGitHub: gitignore examples

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

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

1.11 GitHub

NameComment
Github ShortcutLink: Using keyboard shortcuts
Generate TOCgh-md-toc
Referencelink: 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.

linkedin github slack