Skip to content

Advanced Git Techniques Banner

Welcome, fellow developers! πŸ‘‹ Today, we're diving deep into the powerful world of Advanced Git Techniques. While you might be comfortable with git commit, git push, and git pull, mastering commands like rebase, cherry-pick, and bisect can significantly elevate your workflow, making you a more efficient and effective developer. These tools are crucial for maintaining a clean commit history, selectively applying changes, and debugging with precision.

Before we jump in, if you're looking for a refresher on the basics of Git, check out our catalogue entry on Understanding Git and Version Control.

Let's unlock some serious Git power! πŸ’ͺ


πŸ”„ Git Rebase: Reshaping Your History ​

git rebase is a command that allows you to integrate changes from one branch onto another, but unlike merge, it rewrites the commit history by moving or combining commits. Think of it as taking your changes, putting them aside, updating your branch with the latest changes from another branch, and then putting your changes back on top.

Why use rebase?

  • Clean, Linear History: It helps avoid messy merge commits, resulting in a linear and easier-to-understand project history. This is especially useful for feature branches before merging them into main or develop.
  • Easier Code Reviews: A clean history makes it simpler for others to review your changes.
  • Squashing Commits: You can combine multiple small, incremental commits into a single, more meaningful commit before pushing.

How it works (Basic):

Let's say you have a feature-branch based off main, and main has new commits:

bash
# First, switch to your feature branch
git switch feature-branch

# Then, rebase it onto main
git rebase main

Git will take all the commits from your feature-branch that are not on main, and reapply them one by one on top of main's latest commit. If conflicts arise, Git will pause, allowing you to resolve them.

Interactive Rebase (-i): The Real Powerhouse!

This is where rebase truly shines. Interactive rebase allows you to:

  • Reorder commits: Change the order of commits.
  • Squash commits: Combine multiple commits into one.
  • Edit commits: Modify a commit message or its content.
  • Drop commits: Delete commits.
bash
# Rebase the last 3 commits interactively
git rebase -i HEAD~3

This command will open an editor displaying the commits you're about to rebase, along with instructions on what you can do.

Example Scenario: Cleaning up a feature branch You've been working on a new feature and made several small commits: "WIP", "Fix typo", "Add component", "Refactor styles". Before merging, you want to combine these into a single, descriptive commit.

  1. git rebase -i main (assuming your branch is based on main)
  2. In the editor, change pick to squash (or s) for the commits you want to combine, leaving the first commit as pick.
  3. Save and close the editor. Git will then prompt you to write a new commit message for the squashed commit.

Important Note: Never rebase commits that have already been pushed to a shared remote repository! Rebasing rewrites history, which can cause major problems for collaborators who have based their work on the original history. Only rebase local commits or branches that haven't been shared.


πŸ’ Git Cherry-Pick: Surgical Precision for Commits ​

git cherry-pick allows you to apply specific commits from one branch onto another, without bringing over the entire branch. It's like picking a single cherry from a tree and placing it on another.

Why use cherry-pick?

  • Hotfixes: Quickly apply a critical fix from a development branch to a stable release branch without merging the whole development branch.
  • Selective Integration: Bring over a specific feature or bug fix from one feature branch to another, without full merge.
  • Backporting: Apply a change from a newer version of software to an older, maintained version.

How it works:

bash
# Switch to the branch where you want to apply the commit
git switch target-branch

# Cherry-pick the commit using its hash
git cherry-pick <commit-hash>

Example Scenario: Applying a bug fix to a release branch Imagine you found a critical bug in your main branch that also exists in your release-v1.0 branch. You fix it in main (commit abcdef1). You can then cherry-pick this fix to release-v1.0:

  1. git switch release-v1.0
  2. git cherry-pick abcdef1

This will create a new commit on release-v1.0 with the same changes as abcdef1, but with a different commit hash.


πŸ” Git Bisect: The Bug Hunter's Best Friend ​

git bisect is an incredibly powerful tool for finding the specific commit that introduced a bug. It performs a binary search through your commit history, effectively narrowing down the range of commits where the bug might have been introduced.

Why use bisect?

  • Efficient Bug Locating: Instead of manually checking commits one by one, bisect automates the process, making bug hunting much faster, especially in large repositories.
  • Pinpointing Regressions: Easily find when a previously working feature broke.

How it works:

  1. Start bisecting:

    bash
    git bisect start
  2. Mark a "bad" commit: Tell Git about a commit where the bug is present (usually your current HEAD if the bug is active).

    bash
    git bisect bad
  3. Mark a "good" commit: Tell Git about a commit where the bug was not present (an older, stable commit).

    bash
    git bisect good <good-commit-hash-or-tag>

    Git will then check out a commit in the middle of the good and bad range.

  4. Test the current commit: Run your tests or manually check if the bug exists in the currently checked-out commit.

    • If the bug is present, mark it as bad:
      bash
      git bisect bad
    • If the bug is not present, mark it as good:
      bash
      git bisect good
  5. Repeat: Git will continue to check out commits, narrowing down the range, until it finds the first "bad" commit.

  6. End bisecting: Once the culprit commit is found, or you want to stop the process:

    bash
    git bisect reset

    This will return you to the branch you were on when you started bisecting.

Example Scenario: Finding a UI bug You notice a UI element is misaligned, but you're not sure when it broke.

  1. git bisect start
  2. git bisect bad (assuming the bug is present now)
  3. git bisect good v1.2.0 (assuming v1.2.0 was a stable release without the bug)
  4. Git checks out a commit. You test. If it's still broken, git bisect bad. If fixed, git bisect good.
  5. Repeat until Git tells you: abcdef123456 is the first bad commit

✨ Conclusion ​

Mastering these advanced Git techniquesβ€”rebase, cherry-pick, and bisectβ€”can dramatically improve your productivity and the cleanliness of your project's history. They empower you with surgical precision over your commits and provide efficient ways to debug issues. Practice them in a safe environment, understand their implications (especially rebase and shared history), and soon you'll be wielding Git like a true samurai! πŸ₯‹

Keep pushing boundaries, and happy coding! πŸ’»

Explore, Learn, Share. | Sitemap