1

I'm working on a branch called Feature.

  • I pushed an empty commit 'New' (Commit 1) to indicate the creation of my branch (this is the standard in my company).
  • I then edited an important file and pushed it in a second commit (Commit 2). I realized afterwards that my modification actually resulted in an unknown bug.
  • I ran a reset --hard to go back to the previous, clean commit (Commit 1).
  • I then re-made my modifications in a clean way and made sure it wouldn't cause any bugs. I pushed my modifications in a third commit (Commit 3).

Now when I run git status, it tells me my branch Feature and origin/Feature have diverged and each have one commit. I'm guessing I'm going to have to rebase my branch.

My question is: how am I supposed to go about my commits after a git reset to make sure I don't have a commit 'behind' that eventually results in a branch divergence? How do you keep on committing and pushing after a git reset? Did I miss a step?

My company uses TortoiseGit, so all commands are run through a graphic interface.

enter image description here

  • It would be better to _revert_ commit 2, and then add commit 3. That way your branches remain in sync. – snakecharmerb Nov 27 '22 at 12:31
  • Thank you. I was asked to open a ticket with the glitchy commit's id so other devs could checkout to my branch at this particular commit (2) and test the application to work out where the bug comes from. Wouldn't a 'revert' basically remove the entire commit? – Mr_Knightley Nov 27 '22 at 12:37
  • 1
    Are you being accurate? If the diagram is right, you could not have pushed 3; it would have been rejected. – matt Nov 27 '22 at 12:39
  • 1
    Revert adds a new commit that reverts the reverts the commit that you specify, so commit 2 would still be in the branch. – snakecharmerb Nov 27 '22 at 12:40
  • @matt you're right. I did get an error in TortoiseGit when I tried to push my commit no. 3 ("failed to push some refs") but the way it's being displayed graphically led me to think my commit had been pushed anyway. When I run "git log origin/feature..feature" to see my unpushed commits, the commit no. 3 does appear. – Mr_Knightley Nov 27 '22 at 12:48
  • Hence my answer. :) – matt Nov 27 '22 at 12:52

1 Answers1

2

Let's call your four bullet points steps 1 thru 4. And let's assume that your description of step 4 is incorrect; you made commit 3, but you did not push it, because you can't — which is the problem.


So. You pushed as part of step 1 and again as part of step 2. But then in step 3 you left out a push.

After the reset back to 1, you needed to force push the branch. Otherwise you leave the remote at commit 2, which was the last thing you successfully pushed — resulting in the "diverged branch" situation in your title.

If feature is now on 3 you can solve the problem by force pushing now.


But as you've been told, the right thing in step 3 was was never hard reset, which is messy and violent (and dangerous), and can result in the need to force push, which can be bad for everyone else on the team. You should have just reverted 2, resulting in four commits in a straight line:

1 -- 2 -- undo 2 -- 3

And that way, no divergence would have happened and no force pushing would have been necessary.

--

So, to sum up:

How do you keep on committing and pushing after a git reset?

Ideally you don't reset after pushing. But if you do, you will have to force push in order to carry on.

Did I miss a step?

Yes, you missed the force in force push. But it would be better never to have taken step 3 at all.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you. If I force push my commit no. 3 now, will it remove commit no. 2? Ideally, my commit no. 2 still needs to exist so we can track the bug. – Mr_Knightley Nov 27 '22 at 13:12
  • 1
    "If I force push my commit no. 3 now, will it remove commit no. 2?" Effectively yes. "Ideally, my commit no. 2 still needs to exist so we can track the bug" Well then again you should not have reset hard. That _means_ "erase 2". You could _tag_ origin/feature (which is 2) and push the tag. But ideally it would be so much better to undo the hard reset entirely; that was really a disastrous move. You could undo it and then make the revert and then cherry pick 3, to give the straight line in my diagram. – matt Nov 27 '22 at 13:22