How do you undo the changes made to the main line from the function branch, but keep the branch for today?

From time to time, we may encounter problems in which we bind the feature branch to our baseline and find that in some situations it can go horribly wrong, so the change needs to be returned without losing any other commits made since then. However, someone should continue to work on the source branch.

I donโ€™t want to use git rebase on the main line to change the history (because it can cause all kinds of problems on its own), but when I do git revert on the main line, then as soon as git merge starts on the branch to raise any other changes, made in the system, then all the work done disappears.

What can I do to keep changes in a branch intact?

+6
source share
3 answers

I assume that you do not want to do any reinstallation and reset, this is a merge method.

Suppose you have two branches master and feature . So, you decided to combine the function in master :

 $ git merge feature 

You now have all of your feature changes on the host computer. I assume the most difficult scenario - commits are made after merging on both branches. But suddenly you see that nothing is working feature , everything is broken and decided to return it. Your story (you can see it with git log --graph feature master ):

 * commit 838f29fda2333bdbfc0d1be44d2387e6119dc4e8 | | f3 | | * commit 58d1e3f0e032cc77d4ab2bbf0bdfe6407b96b7e6 | | | | m3 | | | * commit afb600fe382befe095f9b8a6e9eef98be25c929b | |\ Merge: 5cddc52 8660a59 | |/ |/| | | Merge branch 'feature' | | * | commit 8660a59eeb13f9b3447846ba6977140f7c86c7a4 | | | | f2 | | * | commit 6f76e2288d69808b97981340198fd8044ab47630 | | | | f1 | | | * commit 5cddc528de424a01d1d18552ddea93435618e2f7 | | | | m2 | | | * commit fa6ebb76f9f0308f7dcd1148503534668fa15358 |/ | | m1 | * commit 4e3aa05d6f46eb14bbbc253a42325e9a53a3393e 

Note the f3 and m3 commits that were executed after the merge. So, you need to carefully remove only the changes from feature that appeared in master .

To do this, you can return the merge commit to master while maintaining the main base ( -m1 parameter). To do this, I create a new branch based on the merge point and return to it:

 $ git checkout -b feature-revert afb600fe $ git revert -m1 HEAD 

Now we have the restored changes on the feature-revert branch. Now itโ€™s easy to just put it in a master by merging:

 $ git checkout master $ git merge feature-revert 

So, at the moment, master has nothing of feature . However, if you later want to re-merge the function, all returned changes will be lost in master, because they were merged and returned.

To solve the problem, you must return the reverse (he-he) and merge it back into feature :

 $ git checkout feature-revert $ git revert HEAD $ git checkout feature $ git merge feature-revert 

I suppose we no longer need a branch, since we have already merged it, but you can save it if you want:

 $ git branch -d feature-revert 

You should know that merging it returns all changes from master to feature , which is usually a normal workflow.

Now, after the corrections made to feature , you can merge it into master again as usual at any time.

+5
source

When you git revert your changes, it creates a new commit whose tree does not have these changes.

Changes are not deleted, but their original commit still exists.

So simple:

  • git revert your merge commit on the server (remember to select the correct parent). Now the wizard has no broken code entered from your function branch.
  • fix function branch
  • return the first return to the master. This will re-enter the broken state of the function when you first merged it.
  • merge patch function corrections through

See here for more details (or the same file in local git docs if installed).

Your search history is as follows:

  --- a -- ... -- Mi ... ~Mi ... ~~Mi -- Mx [master] \ / / f1 ... fi -- fj ........ x -- fx [feature] 

where Mi is your original merge from the feature branch, ~Mi is the return when you notice the problem, fj .. fx are subsequent corrections to the feature branches, ~~Mi is the return of ~Mi , etc.

+1
source

Have you tried doing git revert on the property branch and then merging this back to master?

Thus, the baselines remain unchanged, and you can continue to work with your function branch.

0
source

Source: https://habr.com/ru/post/950063/


All Articles