Rebase a branch
Rebasing is a powerful technique in Git that allows you to streamline your commit history, ensuring a clean and linear development timeline. By rebasing a branch, you can incorporate the latest changes from another branch and place your commits on top, resulting in a more cohesive and organized commit history.
In this part of our tutorial, we will explore the process of rebasing a branch in Git.
Using the git rebase command. 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:
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:
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: