Git Advanced Commands

Advanced Git operations: rebasing, cherry-picking, and troubleshooting

Rebasing

git rebase

Reapply commits on top of another branch

git rebase main

git rebase -i

Interactive rebase to edit commits

git rebase -i HEAD~3

git rebase --continue

Continue rebase after resolving conflicts

git rebase --continue

git rebase --abort

Abort rebase and return to original state

git rebase --abort

git rebase --skip

Skip current commit in rebase

git rebase --skip

Cherry-picking

git cherry-pick

Apply specific commit to current branch

git cherry-pick abc123

git cherry-pick -n

Cherry-pick without auto-commit

git cherry-pick -n abc123

git cherry-pick --continue

Continue cherry-pick after conflict

git cherry-pick --continue

git cherry-pick --abort

Abort cherry-pick operation

git cherry-pick --abort

git cherry-pick --skip

Skip current commit in cherry-pick

git cherry-pick --skip

Stashing

git stash

Stash current changes

git stash

git stash push

Stash with custom message

git stash push -m "WIP feature"

git stash list

List all stashes

git stash list

git stash pop

Apply and remove latest stash

git stash pop

git stash apply

Apply stash without removing

git stash apply stash@{1}

git stash drop

Remove specific stash

git stash drop stash@{0}

git stash clear

Remove all stashes

git stash clear

Advanced Logging

git log --oneline

Compact log format

git log --oneline

git log --graph

Show commit graph

git log --graph --oneline

git log --author

Filter by author

git log --author="John"

git log --since

Filter by date

git log --since="2024-01-01"

git log -p

Show patch for each commit

git log -p

git log --stat

Show statistics for each commit

git log --stat

Branch Management

git branch -m

Rename current branch

git branch -m new-name

git branch -d

Delete branch (safe)

git branch -d feature-branch

git branch -D

Force delete branch

git branch -D feature-branch

git branch -r

List remote branches

git branch -r

git branch -a

List all branches

git branch -a

git branch --merged

Show merged branches

git branch --merged

git branch --no-merged

Show unmerged branches

git branch --no-merged

Advanced Reset

git reset --soft

Reset to commit, keep changes staged

git reset --soft HEAD~1

git reset --mixed

Reset to commit, unstage changes

git reset --mixed HEAD~1

git reset --hard

Reset to commit, discard all changes

git reset --hard HEAD~1

git reset --keep

Reset to commit, keep working tree

git reset --keep HEAD~1

git reset --merge

Reset to commit, preserve merge

git reset --merge HEAD~1

Submodules

git submodule add

Add submodule

git submodule add https://github.com/user/repo

git submodule init

Initialize submodules

git submodule init

git submodule update

Update submodules

git submodule update

git submodule foreach

Run command in each submodule

git submodule foreach git pull

git submodule status

Show submodule status

git submodule status

Conflict Resolution

git status

Check repository status

git status

git diff

Show differences

git diff

git diff --cached

Show staged differences

git diff --cached

git merge --abort

Abort merge operation

git merge --abort

git reset --hard HEAD

Reset to last commit

git reset --hard HEAD

Recovery

git reflog

Show reference log

git reflog

git fsck

Check repository integrity

git fsck

git gc

Garbage collect repository

git gc

git prune

Remove unreferenced objects

git prune

git clean -fd

Remove untracked files

git clean -fd

Common Patterns

Interactive Rebase Workflow

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

Feature Branch Workflow

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

Cherry-pick Workflow

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

Stash Workflow

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}

Advanced Logging

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

Tips & Best Practices

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