Advanced Git operations: rebasing, cherry-picking, and troubleshooting
Reapply commits on top of another branch
git rebase mainInteractive rebase to edit commits
git rebase -i HEAD~3Continue rebase after resolving conflicts
git rebase --continueAbort rebase and return to original state
git rebase --abortSkip current commit in rebase
git rebase --skipApply specific commit to current branch
git cherry-pick abc123Cherry-pick without auto-commit
git cherry-pick -n abc123Continue cherry-pick after conflict
git cherry-pick --continueAbort cherry-pick operation
git cherry-pick --abortSkip current commit in cherry-pick
git cherry-pick --skipStash current changes
git stashStash with custom message
git stash push -m "WIP feature"List all stashes
git stash listApply and remove latest stash
git stash popApply stash without removing
git stash apply stash@{1}Remove specific stash
git stash drop stash@{0}Remove all stashes
git stash clearCompact log format
git log --onelineShow commit graph
git log --graph --onelineFilter by author
git log --author="John"Filter by date
git log --since="2024-01-01"Show patch for each commit
git log -pShow statistics for each commit
git log --statRename current branch
git branch -m new-nameDelete branch (safe)
git branch -d feature-branchForce delete branch
git branch -D feature-branchList remote branches
git branch -rList all branches
git branch -aShow merged branches
git branch --mergedShow unmerged branches
git branch --no-mergedReset to commit, keep changes staged
git reset --soft HEAD~1Reset to commit, unstage changes
git reset --mixed HEAD~1Reset to commit, discard all changes
git reset --hard HEAD~1Reset to commit, keep working tree
git reset --keep HEAD~1Reset to commit, preserve merge
git reset --merge HEAD~1Add submodule
git submodule add https://github.com/user/repoInitialize submodules
git submodule initUpdate submodules
git submodule updateRun command in each submodule
git submodule foreach git pullShow submodule status
git submodule statusCheck repository status
git statusShow differences
git diffShow staged differences
git diff --cachedAbort merge operation
git merge --abortReset to last commit
git reset --hard HEADShow reference log
git reflogCheck repository integrity
git fsckGarbage collect repository
git gcRemove unreferenced objects
git pruneRemove untracked files
git clean -fdClean 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 --continueAdvanced 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-featureSelectively 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..def5678Temporarily 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-branchAlways 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