A Hands-on Guide to Git Rebase & Resolving Conflicts

A Hands-on Guide to Git Rebase & Resolving Conflicts

Goal

By the end of this tutorial, you will have a practical understanding of how to use git rebase to integrate changes from one branch to another and how to maintain a clean commit history.

Want more information about this image? Check out the "Understanding Git Rebase" guide.

Prerequisites

  • Basic understanding of Git

  • Git installed on your system

Overview

Git rebase is a powerful command that allows you to integrate changes from one branch into another. Instead of merging, which takes all the changes in one branch and merges them into another with a new merge commit, rebase takes a different approach. It replays your changes on top of the branch you're rebasing onto. This results in a cleaner, more linear commit history.

Of course, it's not all sunshine and rainbows - problems do pop up. So, lets take a look at how to use git rebase and also how to deal with conflicts when they arise.

git rebase

  1. Set up a Test Repository

    Lets go ahead and create a test repository that we can use to play around with git rebase.

     # Initialize a new repository
     git init rebase-demo
     cd rebase-demo
    
     # Create a main branch for primary development
     git checkout -b main
    
     # Create a file and commit it to the main branch
     echo "Initial content" > file.txt
     git add file.txt
     git commit -m "Initial commit on main"
    
  2. Create a New Feature Branch

    Now, lets create a new feature branch that we will rebase into the main branch later. We will create a simple file and commit that change to the branch.

     # Create a feature branch and add a file to it.
     git checkout -b feature-branch
     echo "Some feature content" >> file.txt
     git commit -am "Add feature content"
    
  3. Make More Changes to the Main Branch

    Lets go back to the main branch and make some more changes. These changes are what our rebase will be added to.

     # Go back to the main branch and make changes
     git checkout main
     echo "Another line in main" >> file.txt
     git commit -am "Update in main"
    

    Now, you have two branches: main and feature-branch, both with unique commits.

    git rebase tutorial - main brnach vs feature branch

  4. Rebase the Feature Branch

    Now, the magic happens. We switch to the feature branch, and rebase it into main.

     git checkout feature-branch
     git rebase master
    

    Wait, is that it? Is it really that simple?

    It can be, for sure! Assuming all went well, the commit history will show the feature-branch commits will be on the main branch. Those experienced or very observant developers may have noticed a problem above... we will have a conflict. However, not all is lost. Lets take a look at how you can adapt to this situation and fix any minor problems

  5. Resolve Conflicts

    During the rebase, you'll encounter a conflict because line 2 of file.txt is different in each branch. That's ok - no need to panic. All merge conflicts can be fixed with the same basic steps. Here's how to resolve it:

    • Identifying the Conflict: Git will indicate that there's a conflict. You'll see a message similar to:

        Auto-merging file.txt
        CONFLICT (content): Merge conflict in file.txt
        error: could not apply a669cc6... Add feature content
      

      This message tells you that there's a conflict in file.txt that needs resolution.

    • Evaluating the status: One of the great things about Git is that you can always ask for a status update to see what is going on and what you may need to work on. Just make note of the problems to fix and handle them one at a time.

        interactive rebase in progress; onto 117fde3
        Last command done (1 command done):
            pick a669cc6 Add feature content
        No commands remaining.
        You are currently rebasing branch 'feature-branch' on '117fde3'.
            (fix conflicts and then run "git rebase --continue")
            (use "git rebase --skip" to skip this patch)
            (use "git rebase --abort" to check out the original branch)
        Unmerged paths:
            (use "git restore --staged <file>..." to unstage)
            (use "git add <file>..." to mark resolution)
            both modified:   file.txt
      
    • Viewing the Conflict: Depending on how many changes you are dealing with, you may know exactly what the problem is. Alternatively, you can also us git diff to see the changes. Open file.txt in your favorite editor. You'll see the conflicting changes demarcated like this:

        Initial content
        <<<<<<< HEAD
        Another line in master
        Some feature content
         >>>>>>> Add feature content
      

      The content between <<<<<<< HEAD and ======= is from the main branch, and the content between ======= and >>>>>>> Add feature content is from the feature-branch.

    • Resolving the Conflict: Edit file.txt to keep the content you want. For instance, if you want to keep the content from both branches but want the feature content to appear before the master content, you'd modify file.txt to look like this:

        Initial content
        Some feature content
        Another line in master
      

      Save and close the file.

    • Marking the Conflict as Resolved: After resolving the conflict, mark it as resolved with Git:

        git add file.txt
      
    • Continuing the Rebase: Once the conflict is resolved and marked as such, continue the rebase process:

        git rebase --continue
      

      If there are more conflicts, Git will pause again and let you resolve them. If there are no more conflicts, the rebase will complete.

  1. Check the Commit History:

    Use git log to see a clean, linear history of commits, showing that the feature branch commits now come after the master branch commits. Once the rebase is successful, it will be a fast-forward merge, maintaining a linear history.

    git rebase tutorial - rebase complete

Conclusion

Git rebase is a powerful tool for maintaining a clean commit history and integrating changes from one branch into another. While it can be a bit more complex than merging, especially when conflicts arise, the resulting linear history can be worth the extra effort, especially in large projects.

Remember, practice makes perfect. Feel free to experiment with git in safe environments (like our demo repo) before applying it to larger projects. Happy rebasing!

Additional Resources