Logo

dev-resources.site

for different kinds of informations.

Fixing the branch source with git rebase

Published at
2/13/2022
Categories
git
rebase
opensource
Author
jesstemporal
Categories
3 categories in total
git
open
rebase
open
opensource
open
Author
12 person written this
jesstemporal
open
Fixing the branch source with git rebase

The command git rebase can be used to make various history adjustments, from rewriting the commit tree (and by doing that rewriting the history) to even pushing commits forward as if the branch were created in the future.

In this pro tip I will show you how to use git rebase to fix the source of a given branch.

Creating a branch from an incorrect branch

Suppose you have two tasks to do in the next few weeks and you want to work on each task in a different branch.

So to work on task #1 you create a branch with that name from main.

image showing the result of creating the branch task-1

Let’s say that during your implementation you got tired of messing with that problem and decided that it would be a good idea to change the context for a bit and start working on task 2.

For that you need to create a new branch also from main, however, without realizing it you ended up creating your branch from branch task-1:

image showing the result of creating the branch task-2 from task-1

And then, you started working and only realized the mistake later. After you commit to task-2 your history looks something like this:

drawing showing that the branch task-2 depends on the branch task-1

If you look at your history graph you’ll see that it shows that the most recent commit (1078aa) on the branch task-2 depends on the commit made on the branch task-1 (a7f89a):

image showing the result of command git log --graph on branch tarefa-2 showing the history dependant of the branch tarefa-1

So we need to fix this.

Fixing the branch source

With this mistake in hand and since the implementations of each task are independent of each other you want to make this correction.

For this you must use git rebase followed by the flag --onto and the name of the three branches , the full command looks like this:

git rebase --onto main task-1 task-2

Enter fullscreen mode Exit fullscreen mode

The result should look similar to the image below:

image showing the result of the command git rebase --onto main tarefa-1 task-2

Now, having run the above command, your history becomes what it should have been from the beginning:

drawing showing the branch task-2 as dependant of the branch main

And if you check the history graph again, you’ll see that we only see the commits from task 2 in the corresponding branch:

image showing the result of the command git log --graph showing the branch task-2 with the correct history

Three important things to notice:

  1. First, it is always necessary to give the names of the three branches involved to avoid missing commits;
  2. Second, now branch task-2 has its source in the branch main;
  3. And third, the hash of the commit on the branch task-2 is no longer 1078aa but b25a97 since the commit has changed.

With that now you know how to change or fix the source of a branch, if you want more details about the command git rebase, I recommend reading the command’s documentation.

Below you can see a card that can help you remember the command git rebase --onto:


GitFicha #028: git rebase --onto

rebase Article's
30 articles in total
Favicon
Understanding Git Rebase Merge: Chronological vs Logical Order and Commit History
Favicon
A Study on Git Rebase
Favicon
Git Rebase and Code Refactoring for VShell Tool
Favicon
Git Merge vs Rebase
Favicon
Como atualizar um repositório 'forkado' com git rebase
Favicon
Why I prefer rebase over merge (and everything else)
Favicon
Step-by-Step Guide to Rebasing Your Branch onto Dev
Favicon
Git update-refs in a nutshell
Favicon
A Guide to Understanding the Nuances of Git Merge and Rebase
Favicon
Git Merge vs. Rebase: Key Differences
Favicon
Rebasing a Branch Created from Another Branch to Main After Squash Merge: A Step-by-Step Guide
Favicon
A Hands-on Guide to Git Rebase & Resolving Conflicts
Favicon
Refactoring and Rebasing
Favicon
Everything you need to know about git rebase
Favicon
git rebase and git revert to fix the broken application.
Favicon
A clean Git history with Git Rebase and Conventional Commits
Favicon
How to use Git rebase to alter previous commits in the project history
Favicon
Git remove file from not last commit
Favicon
Fixing the branch source with git rebase
Favicon
Project Management: Static Analysis Tools
Favicon
Interactive rebase
Favicon
OSD600 - Refactoring My SSG
Favicon
จะทำยังไงดีน้าาาา.. อยากแก้คำผิดใน commit message ที่ผ่านๆ มา
Favicon
GitHub Workflow, Merge and Rebase
Favicon
Rebase and Merge Don't Mix
Favicon
Professional Version Control with Git: Pt 3 – Rebase and Bisect
Favicon
Git Merge vs Rebase
Favicon
Git Rebase and Interactive Rebase
Favicon
Atomic commits will help you git legit.
Favicon
NOTES: Merge or Rebase

Featured ones: