Learn advanced Git commands like cherry-pick, bisect, and rebase to handle complex development scenarios. This guide provides practical examples and tips to enhance your Git skills.

1. Git Cherry-Pick

The git cherry-pick command lets you apply a specific commit from one branch to another. It’s useful for hotfixes or selectively merging changes without a full merge.

Example Usage

Suppose you want to apply a commit from the feature-login branch to the main branch:

git checkout main
git cherry-pick 

If conflicts occur, resolve them, stage the changes, and continue:

git cherry-pick --continue

2. Git Bisect

Use git bisect to identify the commit that introduced a bug. Git performs a binary search through commits to find the problematic change efficiently.

Example Usage

Start the bisect process:

git bisect start
git bisect bad 
git bisect good 

Git will check out a commit. Test it and mark it as good or bad:

git bisect good
git bisect bad

Repeat until Git identifies the offending commit. End the bisect process:

git bisect reset

3. Interactive Rebase

Interactive rebase (git rebase -i) lets you edit, squash, reorder, or drop commits to clean up your history.

Example Usage

Rebase the last three commits interactively:

git rebase -i HEAD~3

In the rebase editor, use commands like squash or reword to modify commits.

4. Git Stash Apply and Pop

git stash temporarily saves changes, but git stash apply and git stash pop are key for managing stashes:

git stash apply   # Applies a specific stash without removing it
git stash pop              # Applies the latest stash and removes it

5. Git Blame

git blame shows the author and commit information for each line of a file. It’s useful for understanding the history behind a change:

git blame 

6. Git Reflog

git reflog tracks changes to your HEAD, helping you recover lost commits:

git reflog
git reset --hard 

7. Git Worktrees

git worktree lets you check out multiple branches simultaneously by creating separate working directories:

git worktree add  

8. Git Diff

git diff compares changes between commits, branches, or the working directory:

git diff ..  # Compare two branches
git diff --staged             # Compare staged changes with the last commit

Example: Using Advanced Commands in a .NET Project

Suppose you’re debugging a .NET project and need to identify the commit that introduced a bug. Use git bisect:

  1. Start bisecting:
    git bisect start
    git bisect bad HEAD
    git bisect good 
    
  2. Test each commit and mark it as good or bad:
    dotnet test
    git bisect good
    # or
    git bisect bad
    
  3. When the faulty commit is identified, end the process:
    git bisect reset
    

Best Practices for Advanced Git Commands

  • Use cherry-pick sparingly: Avoid overusing cherry-pick, as it can complicate history in large teams.
  • Document workflows: Share best practices for rebase, stash, and bisect with your team.
  • Backup before risky operations: Use git branch or git tag to safeguard important commits.

Conclusion

Mastering advanced Git commands like cherry-pick and bisect empowers you to tackle complex version control scenarios with confidence. By integrating these tools into your workflow, you can debug issues, streamline collaboration, and maintain a clean repository. Experiment with these commands to enhance your Git proficiency and efficiency.