Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify the history before git

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to modify the historical records before git, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Sometimes we suddenly find that something needs to be modified, and the most common document that should not be submitted has been submitted. We hope that it will not just stop appearing in subsequent versions, but that the whole will be removed from the git repository. At this point, we need to modify the previous history of git. What should I do at this time?

Don't worry, there are many ways to modify previous historical submissions in git.

Modify the last submission

As we have mentioned in previous articles, it is relatively simple if we just want to modify the record of the last submission. We just need to directly modify the part we want to modify and add a parameter-amend when submitting.

Git commit-amend

Amend means patch, which can merge our changes this time into the previous history record without generating a new commit record. After running, it opens a vim editor, and we can also modify the prompt we entered in the last commit.

If we use git log to check, we will find that the modification time of the history is still the last time. It looked as if nothing had happened and quietly changed it.

Modify multiple information

-- amend is easy to use, but it can only modify the last commit information. What should we do if the commit record we want to change is before that?

There are no direct tools available in git to do this, but we can use rebase to do so. We can add-I to change the base interactively, we can stop after any desired modification is completed, or we can add files or do whatever we want. But our goal of changing the base is not a branch but a history node of the current branch, so we need to provide a specific commitid or pointer position.

Git rebase-I is so powerful that we can use it to do almost everything we want to do.

For example, if we want to modify the penultimate commit, we can execute git rebase-I HEAD~3. That is, the base transformation is performed with the penultimate node as the benchmark node, and git will enter a vim window where we can see the record of the last three submissions.

First of all, we can see that the above three lines are the three commit that we can modify, showing the operation to be performed as well as commitid and commit message. The default for the operation here is pick, that is, using the commit. Git also gives a good hint of what we can do below, of which the more common ones are pick, edit, and squash.

What we want to do this time is to change the commit record, so we should execute edit, and we change the pick before the commit we want to change to edit. Like this:

After exiting, git will automatically take us back to the committed version of the branch we chose edit. Let's make the changes we want, and here I added a line to my 15th article: try rebase. Then use git add and git commit-- amend to modify and submit the result.

After that, we execute git rebase-continue to complete the remaining changes to be applied.

When it's all over, we can use the git show command to check the record of our modified bee9ce3 commit. You can see that this line has been added, indicating that our modification has been successful.

Order change, merge, split

Sequence change

Not only can we modify the contents of a commit, but we can also modify the relative order of these commit and make them merge and split.

The modification order is actually very simple, we only need to artificially modify the vim file that pops up after rebase-I. For example, the original record is:

Pick A change A pick B change B pick C change C

If we want to change the order, we just need to modify the file. For example, it becomes:

Pick B change B pick A change A pick C change C

So when we exit vim, git will first apply the changes of B commit, then apply An and finally apply C.

Merge

In addition, we can merge multiple commit records into one. The method of operation is also very simple, that is, we only need to change pick to squash. Git automatically merges all squash commit records together.

Pick A change A squash B change B squash C change

Split

Sometimes a commit is very large, we may also want to split it, in fact, the operation is very simple. For example, we want to split commit B into two strips. First, when we rebase, we change the pick in front of commit B to edit.

Pick A change An edit B change B pick C change C

When we quit, we will enter the state that B commit has just submitted. Since what we are going to do is split the commit B, we need to execute git reset head ^ to reset the last commit. Then separate the add and we want to split the submitted file.

The whole operation is as follows:

Git reset head ^ git add test/* git ci-m 'add test' git add code/* git ci-m' update code' git rebase-- continue

So we split commit B into two commit and inserted it into the history record.

Finally, it is important to note that although these methods are very useful when modifying records. But if these commit have been submitted to the remote, we can not directly git push synchronization. Because git validates the hash value we submit and forbids our submission when we find a mismatch. So if you want to submit to the remote, you can only use git push-f to force the override. But this is a very dangerous operation, if you git push-f, no one will know what you have modified, only on your own unique branch to do so, be sure to use with caution.

The above is all the contents of the article "how to modify the History before git". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report