huazi

huazi

Analysis of Several Scenarios for Git Branch Merge and Rollback

Today, I encountered a problem when merging branches. The situation is as follows: the current development branch A merged branch B and conflicts were found. So I went to resolve the conflicts and committed the changes. However, at this time, my boss reminded me that another method might be better. So I wanted to go back and make some changes and try again. Actually, I could just reset. But I found an article on the official website that introduces this situation, so I decided to write it down as the article suggests.
Article link: https://git-scm.com/blog/2010/03/02/undoing-merges.html
First, let's talk about some basic knowledge of Git:

  • A Git repository consists of three main components: working directory, staging area, and commit history.
  • Reset moves a branch's pointer to another commit.
  • Checkout switches branches.
  • Revert undoes a commit while creating a new commit.

git revert is a safe method. Compared to git reset, it does not change the current commit history. Therefore, git revert can be used on public branches, while git reset is best used on private branches.

Next, let's talk about the method of undoing a merge introduced in the article:

Reset a Merge:#

Reset to the commit before the merge, and then redo the subsequent operations. However, this requires all collaborators to know how to handle the rolled back head. If this is not a problem, or it is just a local branch, this is a good solution. The method is as follows:

$ git checkout master
git reset --hard [SHA value of the commit to rollback to]

Simple and effective.

Reverting a Merge:#

When there are other operations and changes after the merge, or your collaborators have made some commits after your merge, Git also has a way to undo the merge. You can use the revert command. The method is as follows:

$ git revert -m [number of the merge line to be reverted] [version number before the merge, i.e. the SHA value]
Finished one revert.
[master 88edd6d] Revert "Merge branch 'jk/post-checkout'"
 1 files changed, 0 insertions(+), 2 deletions(-)

This will create a new commit to undo the corresponding merge operation. If you try to merge again, Git will see that the commits on this branch are in the history and assume that you are mistakenly trying to merge something you already have.

$ git merge jk/post-checkout
Already up-to-date.

Reverting the Revert#

$ git revert [version number of the commit submitted when reverting the merge in method 2, here it is 88edd6d]
Finished one revert.
[master 268e243] Revert "Revert "Merge branch 'jk/post-checkout'"" 1 files changed, 2 insertions(+), 0 deletions(-)

Now you can merge normally, but this may result in more conflicts.
Now we have basically reintroduced everything in the branch we rolled back before. Now if we have more work on this branch, we can merge it again.

$ git merge jk/post-checkout
Auto-merging test.txt
Merge made by recursive. test.txt | 1 + 1 files changed, 1 insertion(+), 0 deletions(-)

Conclusion#

After roughly reading it, I found that the methods mentioned later are too complicated and unnecessary. However, the author may be based on the complex situation of making changes on public branches. In any case, it feels more convenient to use reset on local branches, or revert can also be used. Just avoid unnecessary trouble.
Finally, I would like to share an article about git rebase in the Git Community Book. I didn't understand it well before, but I think it is well written: http://gitbook.liuhui998.com/4_2.html

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.