Skip to main content

Advanced Commands Cheatsheet

Cherry-Pick

CommandAction
git cherry-pick <hash>Apply a commit to current branch
git cherry-pick --no-commit <hash>Apply without committing
git cherry-pick A..BCherry-pick a range
git cherry-pick --abortCancel in-progress cherry-pick
git cherry-pick --continueContinue after conflict

Bisect

CommandAction
git bisect startStart bisect session
git bisect badMark current commit as bad
git bisect good <ref>Mark a commit as good
git bisect good / git bisect badRate each step
git bisect run <script>Automated bisect
git bisect resetEnd bisect session

Submodules

CommandAction
git submodule add <url> <path>Add a submodule
git submodule initInitialize submodules
git submodule updateUpdate to recorded commit
git submodule update --remoteUpdate to latest remote
git clone --recurse-submodules <url>Clone with submodules
git submodule deinit <path>Remove a submodule

Subtrees

CommandAction
git subtree add --prefix=<dir> <url> <branch> --squashAdd subtree
git subtree pull --prefix=<dir> <url> <branch> --squashUpdate subtree
git subtree push --prefix=<dir> <url> <branch>Push changes back

Hooks

HookTrigger
pre-commitBefore commit created
commit-msgAfter message entered
pre-pushBefore push to remote
post-mergeAfter merge completes
pre-rebaseBefore rebase starts
post-checkoutAfter branch switch

Debugging & Inspection

CommandAction
git blame <file>Who changed each line
git blame -L 10,20 <file>Blame specific lines
git log -p <file>Full patch history of file
git log --follow <file>History across renames
git shortlog -snCommits count per author
git cat-file -p <hash>Inspect any Git object
git cat-file -t <hash>Show object type
git rev-parse HEADGet current commit hash
git reflogView HEAD movement history

Maintenance

CommandAction
git gcGarbage collection (optimize)
git gc --prune=nowRemove all unreachable objects
git fsckCheck repository integrity
git count-objects -vShow object database stats
git pruneRemove unreachable objects

Large Repos

CommandAction
git clone --depth 1Shallow clone
git sparse-checkout init --coneEnable sparse checkout
git sparse-checkout set <dirs>Check out specific dirs
git lfs installEnable Git LFS
git lfs track "*.psd"Track large files
git config --global alias.st "status -s"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.ci "commit"
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "restore --staged"