Home > Net >  Consequence of Git soft reset
Consequence of Git soft reset

Time:09-18

I hope someone can help as I'm quite new to git. Lets say I have the following merged PRs on main branch: A->B->C->D->E-F

I need to get it back to the state it was in at commit C because D E F have issues that need fixing. So I do a soft reset back to C to another branch (to revert D,E,F) and then merge that branch back into main. Now I have commit G that reverts D, E and F. A->B->C->D->E-F->G

Two questions:

  1. Can someone else now easily branch off main, do their work and merge back in without issues.
  2. How do I go about fixing the bad commits D E F? Should I make a new branch off master as it is now and redo all the changes?

Any help appreciated.

CodePudding user response:

A number of things. First, chart should be A<-B<-C... because in git children revisions point to their parents, not the other way around. Then, if you did a soft reset (git checkout F; git reset --soft C; git commit -m "reverting"), then that is not correct. You end up with a revision after C that has the same content of F, which is not what you want, right?

Probably what you want to do is rather:

git checkout C
git reset --soft F
git commit -m "Reverting to C" 
# this revision we just created has the content of C and its parent is F....
# in other words, it reverts changes introduced by D, E and F
# feel free to set a branch over here push and merge

Point questions.

1 - Yes. The general rule is that if you do not rewrite history of the branch, people should be able to work on a branch moving forward. It is when people start rewriting shared branches that things can go nuts. Of course.... this implies that people won't be working on things related to D, E and F. If it's related to those revisions, then conflicts are expected to happen later on given that you are taking those changes back.

2 - Let's assume that each one of them is a feature of its own and does not depend on the other 2 broken revisions.... and you want to start working from that first revision.... so, let's say I want to work on E:

git checkout -b branchE E # create a feature branch on E
git commit --amend --no-edit # make git believe that this is something that has not been merged yet
git rebase master # rebase on top of master
# now you can continue working so that you get the change working correctly on this branch
.
.
# when you are done with it, push and create a PR and merge.
  •  Tags:  
  • git
  • Related