Use git rebase-onto command

Source: Internet
Author: User

Sometimes, when the branch submits changes, it will forget the rebase and directly commit the changes, or forget to do merge with the local remote branch, and then directly rebase the other branches. Sometimes I really want to have a way of slicing, so that my branch only needs to be connected to a certain segment. Now you can use rebase -- onto in git.

Suppose scenario B----C A, now want to put the slice between B-C (note the boundary of B and C, which will be discussed later) above.

Suppose git rebase -- onto a B c, this demo. a Represents the branch where you actually want to put the slice, and B represents the start of the slice Branch (pay special attention to the open and close problem of B. Here the rebase-onto mechanism isLeft open right closed) That is to say, if you really want this node slice of B, you should start with a branch before B. At this time, git has prepared for us, that is, B ~ 1 or B ^, both represent a node before B. C indicates the end of the slice, which is closed at this time, that is, the current node of C will be taken to.

Many of you will say that it is so simple, I will! In fact, you have ignored a very serious problem, that is, which branch of A, B, and C is the real active branch after me. That is, after I complete the slice operation, is a history above the slice?

What I want to tell you is that the history of a has never changed. Students asked: What !? So what am I doing with slice? I want to put my slice history on! In fact, we should pay special attention to the details at this time, that is, the true active branch is C, that is, if you want to keep the history of A and C, you need to create a branch temp at the end of the slice so that I can call the command git rebase -- onto a B ~ 1 temp, that is, I add the history between B and C to A, andThe Historical Status of the current temp branch is what I want. In this way, you have completed the GIT rebase -- onto command.

In addition to the above scenarios, there is also a classic usage. Suppose you have a long history like A-1-2-3-4-5-6-7-8-B.

At this time, you suddenly found that there was a mistake in the first place, but you didn't want git reset B ~ 8. After modification, rewrite the submission information one by one. Now you can use git rebase --. In fact, it is very simple. Suppose we have an error on the node 1. First, I cut a branch temp in step 1 and switch it over to make changes. After the change, I useGit commit -- amend,That is, rewrite the submission history. After the submission, the submission information of my 1 has not changed, but the file has been changed. Well, the next thing to do is to record the history from 1 to B, add to the new 1. As we can see through TIG or gitk that 1 and 1 completely go through two branches at this time, we will naturally think of doing so.

At this time, we can use our git rebase -- onto. First, create an active branch result at the end of the slice, that is, B, and then call the command git rebase -- onto generate a new 1 result, pay attention to the problem of opening and closing intervals, because I have a history of 1 at this time, so I should start from the next one, but because it is left open and closed, so I can write 1 directly here. This completes the rollback history modification, and you do not need to re-write and submit the information. It's cool!

I hope this article will help you when using git and leave your footprints. This is my greatest support. Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.