Advanced Git operations: rebasing, cherry-picking, and troubleshooting
Reapply commits on top of another branch
git rebase main
Interactive rebase to edit commits
git rebase -i HEAD~3
Continue rebase after resolving conflicts
git rebase --continue
Abort rebase and return to original state
git rebase --abort
Skip current commit in rebase
git rebase --skip
Apply specific commit to current branch
git cherry-pick abc123
Cherry-pick without auto-commit
git cherry-pick -n abc123
Continue cherry-pick after conflict
git cherry-pick --continue
Abort cherry-pick operation
git cherry-pick --abort
Skip current commit in cherry-pick
git cherry-pick --skip
Stash current changes
git stash
Stash with custom message
git stash push -m "WIP feature"
List all stashes
git stash list
Apply and remove latest stash
git stash pop
Apply stash without removing
git stash apply stash@{1}
Remove specific stash
git stash drop stash@{0}
Remove all stashes
git stash clear
Compact log format
git log --oneline
Show commit graph
git log --graph --oneline
Filter by author
git log --author="John"
Filter by date
git log --since="2024-01-01"
Show patch for each commit
git log -p
Show statistics for each commit
git log --stat
Rename current branch
git branch -m new-name
Delete branch (safe)
git branch -d feature-branch
Force delete branch
git branch -D feature-branch
List remote branches
git branch -r
List all branches
git branch -a
Show merged branches
git branch --merged
Show unmerged branches
git branch --no-merged
Reset to commit, keep changes staged
git reset --soft HEAD~1
Reset to commit, unstage changes
git reset --mixed HEAD~1
Reset to commit, discard all changes
git reset --hard HEAD~1
Reset to commit, keep working tree
git reset --keep HEAD~1
Reset to commit, preserve merge
git reset --merge HEAD~1
Add submodule
git submodule add https://github.com/user/repo
Initialize submodules
git submodule init
Update submodules
git submodule update
Run command in each submodule
git submodule foreach git pull
Show submodule status
git submodule status
Check repository status
git status
Show differences
git diff
Show staged differences
git diff --cached
Abort merge operation
git merge --abort
Reset to last commit
git reset --hard HEAD
Show reference log
git reflog
Check repository integrity
git fsck
Garbage collect repository
git gc
Remove unreferenced objects
git prune
Remove untracked files
git clean -fd
Clean up commit history with interactive rebase
# Start interactive rebase
git rebase -i HEAD~5
# In the editor, you can:
# pick - keep commit as is
# reword - change commit message
# edit - modify commit
# squash - combine with previous commit
# fixup - combine but discard message
# drop - remove commit
# Example:
pick abc1234 First commit
reword def5678 Second commit
squash ghi9012 Third commit
drop jkl3456 Fourth commit
# After editing, save and close
# Resolve any conflicts if they occur
git rebase --continue
Advanced feature branch management
# Create feature branch from main
git checkout main
git pull origin main
git checkout -b feature/new-feature
# Work on feature
git add .
git commit -m "Add new feature"
# Keep feature branch up to date
git fetch origin
git rebase origin/main
# Push feature branch
git push origin feature/new-feature
# Create pull request on GitHub/GitLab
# After merge, clean up
git checkout main
git pull origin main
git branch -d feature/new-feature
git push origin --delete feature/new-feature
Selectively apply commits from other branches
# Find commits to cherry-pick
git log --oneline other-branch
# Cherry-pick specific commits
git cherry-pick abc1234
git cherry-pick def5678
# If conflicts occur
git status
# Edit conflicted files
git add .
git cherry-pick --continue
# Or abort if needed
git cherry-pick --abort
# Cherry-pick range of commits
git cherry-pick abc1234..def5678
Temporarily save work in progress
# Stash current work
git stash push -m "WIP: working on feature"
# Switch to another branch
git checkout hotfix-branch
# Fix urgent issue
git add .
git commit -m "Fix critical bug"
git push origin hotfix-branch
# Return to original branch
git checkout feature-branch
# Apply stashed changes
git stash pop
# Or apply specific stash
git stash apply stash@{1}
# List all stashes
git stash list
# Show stash contents
git stash show -p stash@{0}
Powerful log filtering and formatting
# Show commit graph
git log --graph --oneline --all
# Filter by author and date
git log --author="John Doe" --since="1 week ago"
# Show commits affecting specific file
git log --follow -- filename.txt
# Show commits with patches
git log -p -- filename.txt
# Show commits with statistics
git log --stat --since="1 month ago"
# Custom log format
git log --pretty=format:"%h - %an, %ar : %s"
# Show merge commits only
git log --merges
# Show commits in range
git log main..feature-branch
Always create a backup branch before rebasing: git branch backup-branch
Use git reflog
to recover from mistakes
Never rebase commits that have been pushed to shared repositories
Use descriptive stash messages: git stash push -m "WIP: feature description"
Regularly clean up merged branches and old stashes