0

I made multiple commits to a branch to a branch and pushed them to a remote repo. There have been some merge commits in the middle as well. I wish to reset the branch to a known commit before all of these. And I do not wish to rewrite history on the remote.

Basically, I have the commits:

A -> B -> C -> D -> E
F -> G -> H /

and I want to make a commit E -> I that just directly puts the branch in the same state as A. I do not care about reverting individual commits, not least because some of them are merge commits as well.

If it helps, think of this like rolling back a flawed deployment. I do not know and I do not care which individual commits are an issue. I just want to go back to the last known good version.

If it matters, A might be a merge commit as well.

I saw this question but it was about rewriting history. The branch has already been pulled by others so I do not want to mess it all up for everyone.

Kushagra Gupta
  • 514
  • 2
  • 13
  • As a side comment, the arrows should be pointing in the opposite direction. In git, children commits _point to_ their parents, not the other way around. I understand what you meant from reading the question but I could just as easily understood `E` to be the parent of all the other commits. – eftshift0 Oct 01 '22 at 09:51

2 Answers2

3

you need to use git reset with soft

Start from A to get the state you want

git reset --hard <Commit A>

move to E without changing files

git reset --soft <Commit E>

then commit it

git add . && git commit -m "Commit I"
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • Huh. Wow. That makes sense. I didn't think of ever using reset in the other direction. Thanks a lot – Kushagra Gupta Oct 01 '22 at 09:29
  • Upvoted. I would be careful about the first `git reset --hard`. If you are not careful, you could end up moving _the branch_ and then you might have to do additional work to know what `E` was before (not particularly difficult.... just a little more work). It can be avoided by doing a simple checkout: `git checkout A; git reset --soft E; git checkout the-branch-on-E-if-you-are-using-one; git commit -m "Taking it back to how A was"`. – eftshift0 Oct 01 '22 at 09:57
  • @eftshift0 thanks for the warning. It wouldn't be a concern for me in this case though, as the branch was already pushed upstream (hence all the problems) and therefore, I would have the remote branch still pointing to E. – Kushagra Gupta Oct 01 '22 at 12:17
0

Another way is using the kind-of new git restore:

git checkout E
git restore --worktree --staged --source=A .
git commit -m "A commit right after E that takes the project back to what it was like in A"
eftshift0
  • 26,375
  • 3
  • 36
  • 60