Git rebase –onto an overview

In article How to change parent branch in git? Ι talked quickly about using git rebase --onto in α case when you want to replace the current parent branch. But git rebase --onto is more than just replacing parent branch. We can do much more with git rebase --onto help. It’s α good topic to take α look closely. To use it freely first, you need to understand it.

There are two cases when you can go for git rebase --onto:

  1. You have α branch, where you want to change its parent branch.
  2. You want to quickly remove some commits from your current branch.

Of course, you can combine these two reasons. You can change the parent branch and remove some commits at the same time. We will get to this point. But before we go there, we need to understand the differences between git rebase --onto with two and three arguments.

Let’s start from the beginning. First, we will focus on just α simple git rebase. Ι wrote α separate article about git rebase. So if you want to know more about git rebase go there. Right now, we will cover it very quickly.

Git rebase

git rebase <newparentvàgt; <branchvàgt; is α command which allows us to have access to the latest commit that is reachable from <newparentvàgt; and move our <branchvàgt; commits on top of it.

In case we use the command:

git rebase master next-feature

we will get:

Before                            After
𝓐---Ɓ---₵---₣---₲ (HEAD master)   𝓐---Ɓ---₵---₣---₲ (master)
                                                  
          𝓓---E (next-feature)                      𝓓'---E' (HEAD next-feature)

As you see above after git rebase our HEAD is always the last argument. In our case on branch next-feature. So we switch branch to next-feature. On this branch, we still get access to our code in commits 𝓓 and E, but they are not the same commits. Their unique identifier generated by cryptographic hash function SHA-1 (dce79fd), which we often call just SHA, changed. This is why Ι marked them as 𝓓' and E'.

When we use git rebase and we are already on the branch which we want to rebase, we can skip α second argument. We can do:

git rebase master

and the result will be the same

Before                              After
𝓐---Ɓ---₵---₣---₲ (master)          𝓐---Ɓ---₵---₣---₲ (master)
                                                    
          𝓓---E (HEAD next-feature)                   𝓓'---E' (HEAD next-feature)

In both cases on the master branch, we had two commits and that are not reachable from the next-feature branch. When we do git rebase we take 𝓓 commit (which is the first commit on the next-feature branch) with all next commits on this branch and we move them on top of the last commit on master branch so on top of . In our case, when we look at the diagram, it will be better to say that we move our commits on the end of the master branch. However, keep in mind that when you use tools like git log, you will see changes on top of the master branch. In other words, we change the parent of our next-feature branch from commit to commit on the master branch.

XEM THÊM  Gta Vice City Free Download For Pc Windows 10
XEM THÊM  Font awesome là gì? cách sử dụng font awesome

Git rebase –onto

More precise changing parent branch

In case of git rebase --onto we can change the point where our branch is starting not only to the last commit on parent branch, but we can choose specific commit where we start and also where we finish. This is true not only on one specific branch but for other branches (all valid commits) too. We can say that git rebase --onto in precise and elastic solution. It grants you control over what and where is being rebased.

For example, you would like to change the branch starting point from to and you would like to remove commit 𝓓 from your next-feature branch. To do that we need to use this command:

git rebase --onto ₣ 𝓓

The effect will look like this:

Before                                    After
𝓐---Ɓ---₵---₣---₲ (branch)                𝓐---Ɓ---₵---₣---₲ (branch)
                                                      
          𝓓---E---Н---Ι (HEAD my-branch)                E'---Н'---Ι' (HEAD my-branch)

We rebase the commit reachable from HEAD (my-branch) where parent commit is 𝓓 on top of the commit. So, we can say that we change the parent of commit E from 𝓓 to .

The same effect, we will get when we call:

git rebase --onto ₣ 𝓓 my-branch

The situation looks different when instead of HEAD as the third argument we will use the last commit. In our case Ι. When we will call:

git rebase --onto ₣ 𝓓 Ι

The effect looks like this:

Before                                    After
𝓐---Ɓ---₵---₣---₲ (branch)                𝓐---Ɓ---₵---₣---₲ (branch)
                                                 |    
          𝓓---E---Н---Ι (HEAD my-branch)          |     E'---Н'---Ι' (HEAD)
                                                   
                                                    𝓓---E---Н---Ι (my-branch)

As in the normal git rebase we switch HEAD to the last argument of git rebase --onto command. In this case, this is α commit Ι'. We see that our branch my-branch stayed like it was before. Nothing has changed on the my-branch. But we have new branch which is our new HEAD. Right now it is not the real branch, but we can name it. What happened here? We told git that we change the parent of commit E. You can think: “But, why commit E? We don’t have the commit E in our command.” Since the current parent is the commit 𝓓, then its child is the commit E. So we change the parent of commit E from 𝓓 to , but we also switch HEAD from branch my-branch to the new commit Ι'.

The same effect, we will get when we call:

git rebase --onto ₣ 𝓓 HEAD

𝓐 similar situation is when we want to switch HEAD to commit Н. We will use the command:

git rebase --onto ₣ 𝓓 Н

Our branches will look like:

Before                                    After
𝓐---Ɓ---₵---₣---₲ (branch)                𝓐---Ɓ---₵---₣---₲ (branch)
                                                 |    
          𝓓---E---Н---Ι (HEAD my-branch)          |     E'---Н' (HEAD)
                                                   
                                                    𝓓---E---Н---Ι (my-branch)

The only thing which changed from the last example is that we don’t finish our new HEAD on commit Ι', but on commit Н'. We are ignoring where HEAD was pointing to and we choose one commit before old HEAD – commit Н. Equivalent behavior we will get when we will do these commands:

git rebase --onto ₣ 𝓓 Н
git rebase --onto ₣ 𝓓 HEAD^
git rebase --onto ₣ 𝓓 HEAD~
git rebase --onto ₣ 𝓓 HEAD~1

Removing commits from the current branch

This is α nice solution. When you want to quickly remove some commits from your current branch without using interactive rebase. If we have α branch and we want to remove commits and 𝓓, we can do that by using:

git rebase --onto Ɓ 𝓓

This gives us:

Before                                 After
𝓐---Ɓ---₵---𝓓---E---₣ (HEAD branch)    𝓐---Ɓ---E'---₣' (HEAD branch)

In this example, we say rebase HEAD on top of commit Ɓ, where the old parent branch was α commit 𝓓. The same effect, we will get for:

git rebase --onto Ɓ 𝓓 my-branch

If we use git rebase --onto with three arguments, where the last one is α commit identifier, the situation will look slightly different. We can say rebase HEAD on top of commit Ɓ, where the old parent branch was α commit 𝓓, but only to commit E and switch HEAD there. To achieve that we will use the command:

git rebase --onto Ɓ 𝓓 E

We will get new branch with only commit E' with parent commit Ɓ.

Before                                 After
𝓐---Ɓ---₵---𝓓---E---₣ (HEAD branch)    𝓐---Ɓ---₵---𝓓---E---₣ (branch)
                                            
                                             E' (HEAD)

Summary git rebase –onto

Let’s summarize how git rebase --onto is working. We can call git rebase --onto with two or three arguments. When we use two arguments general syntax looks like this:

git rebase --onto <newparentvàgt; <oldparentvàgt;

This will allow as to change the current parent <oldparentvàgt; to new one <newparentvàgt;. Because we didn’t specify the third argument, we will stay on our current branch. For git rebase ---onto with three arguments situation will look different. This is general syntax:

git rebase --onto <newparentvàgt; <oldparentvàgt; <untilvàgt;

Now we can change the old parent <oldparentvàgt; to new one <newparentvàgt;, but we will take commits only until <untilvàgt; commit. Remember <untilvàgt; will become the new HEAD after rebase is completed.

XEM THÊM  Cách hỏi thăm chúc sức khoẻ bằng tiếng anh
XEM THÊM  Gta Vice City Free Download For Pc Windows 10

Xem thêm bài viết thuộc chuyên mục: Kiến thức lập trình

Related Articles

Stay Connected

FansLike
3,052FollowersFollow
SubscribersSubscribe

Latest Articles