Advanced Commands Cheatsheet
Cherry-Pick
| Command | Action |
|---|---|
git cherry-pick <hash> | Apply a commit to current branch |
git cherry-pick --no-commit <hash> | Apply without committing |
git cherry-pick A..B | Cherry-pick a range |
git cherry-pick --abort | Cancel in-progress cherry-pick |
git cherry-pick --continue | Continue after conflict |
Bisect
| Command | Action |
|---|---|
git bisect start | Start bisect session |
git bisect bad | Mark current commit as bad |
git bisect good <ref> | Mark a commit as good |
git bisect good / git bisect bad | Rate each step |
git bisect run <script> | Automated bisect |
git bisect reset | End bisect session |
Submodules
| Command | Action |
|---|---|
git submodule add <url> <path> | Add a submodule |
git submodule init | Initialize submodules |
git submodule update | Update to recorded commit |
git submodule update --remote | Update to latest remote |
git clone --recurse-submodules <url> | Clone with submodules |
git submodule deinit <path> | Remove a submodule |
Subtrees
| Command | Action |
|---|---|
git subtree add --prefix=<dir> <url> <branch> --squash | Add subtree |
git subtree pull --prefix=<dir> <url> <branch> --squash | Update subtree |
git subtree push --prefix=<dir> <url> <branch> | Push changes back |
Hooks
| Hook | Trigger |
|---|---|
pre-commit | Before commit created |
commit-msg | After message entered |
pre-push | Before push to remote |
post-merge | After merge completes |
pre-rebase | Before rebase starts |
post-checkout | After branch switch |
Debugging & Inspection
| Command | Action |
|---|---|
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 -sn | Commits count per author |
git cat-file -p <hash> | Inspect any Git object |
git cat-file -t <hash> | Show object type |
git rev-parse HEAD | Get current commit hash |
git reflog | View HEAD movement history |
Maintenance
| Command | Action |
|---|---|
git gc | Garbage collection (optimize) |
git gc --prune=now | Remove all unreachable objects |
git fsck | Check repository integrity |
git count-objects -v | Show object database stats |
git prune | Remove unreachable objects |
Large Repos
| Command | Action |
|---|---|
git clone --depth 1 | Shallow clone |
git sparse-checkout init --cone | Enable sparse checkout |
git sparse-checkout set <dirs> | Check out specific dirs |
git lfs install | Enable Git LFS |
git lfs track "*.psd" | Track large files |
Aliases (Recommended)
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"