Git

Git Advanced

Advanced Git commands for rebasing, cherry-picking, and workflows.

36 commands
Windows MacOS Linux
#version-control #workflow

Rebasing

Rebase current branch onto main
git rebase main
Interactive rebase last 5 commits
git rebase -i HEAD~5
git rebase --onto main feature~3 feature # Rebase subset of commits onto main
Continue after resolving conflicts
git rebase --continue
Abort rebase and restore original state
git rebase --abort
Skip current conflicting commit
git rebase --skip

Cherry-Pick

Apply a specific commit to current branch
git cherry-pick abc1234
Apply a range of commits
git cherry-pick abc1234..def5678
Apply changes without committing
git cherry-pick --no-commit abc1234
Edit commit message before applying
git cherry-pick --edit abc1234
Abort cherry-pick in progress
git cherry-pick --abort

Reset & Revert

Undo last commit, keep changes staged
git reset --soft HEAD~1
Undo last commit, unstage changes
git reset --mixed HEAD~1
Undo last commit, discard all changes
git reset --hard HEAD~1
Create new commit that undoes a commit
git revert abc1234
Revert multiple commits without committing
git revert --no-commit HEAD~3..HEAD
Show history of HEAD movements
git reflog

Bisect

Start binary search for bad commit
git bisect start
Mark current commit as bad
git bisect bad
Mark a known good commit
git bisect good abc1234
End bisect and return to original branch
git bisect reset
Automate bisect with a test script
git bisect run ./test.sh

Worktrees

Create worktree for main branch
git worktree add ../hotfix main
Create worktree with new branch
git worktree add -b feature ../feature
List all worktrees
git worktree list
Remove a worktree
git worktree remove ../hotfix
Clean up stale worktree references
git worktree prune

Submodules

Add a submodule to the project
git submodule add https://repo.git lib
Initialize submodule configuration
git submodule init
Clone and checkout all submodules
git submodule update --init --recursive
git submodule foreach git pull origin main # Update all submodules to latest
Unregister a submodule
git submodule deinit lib
Show status of all submodules
git submodule status

Quick Commands

Interactive rebase last 5 commits
git rebase -i HEAD~5
Apply a specific commit to current branch
git cherry-pick abc1234
Automate bisect with a test script
git bisect run ./test.sh