Skip to main content
  1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. How to use Git
  6. How to use branching in Git
  7. Rebase a branch
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Rebase a branch

Another approach to integrate the issue3 branch into the main branch is the git rebase command. Using rebase, we can clean up our history tree, as discussed earlier in this guide.

Let’s start by undoing the previous merge.

$ git reset --hard HEAD~

This is how our history looks now:

History before rebase

Next, switch to the issue3 branch and rebase onto the main branch.

$ git checkout issue3
Switched to branch 'issue3'
$ git rebase main
Auto-merging sample.txt
CONFLICT (content): Merge conflict in sample.txt
error: could not apply 470d684... My Commit
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 470d684... My Commit

When the conflict occurs during the rebase, you must resolve it to resume the operation.

Anyone can learn Git with this tutorial and Backlog
add: Register a change in an index
<<<<<<< HEAD
commit: Save the status of an index
=======
pull: Obtain the content of a remote repository
>>>>>>> issue3

Once the conflict is resolved, you can resume rebasing with the --continue option.

Before applying your changes, Git will open your default text editor (Vim or Emacs on most Unix systems) to give you the opportunity to edit the rebase message. If you wish to quit and roll back the rebase operation, you can do so with the --abort option.

$ git add myfile.txt
$ git rebase --continue
Applying: append description of the pull command

Now our history looks like this:

Current history

With the issue3 branch rebased onto main, we can now issue a fast-forward merge.

Switch over to the main branch and merge issue3 with main

$ git checkout main
Switched to branch 'main'
$ git merge issue3
Updating 8f7aa27..96a0ff0
Fast-forward
myfile.txt |    1 +
  1 files changed, 1 insertions(+), 0 deletions(-)

The content of the myfile.txt file will now be identical to the one in the previous merge. And the history will now look like the following:

Current history

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life