Home > Software design >  commit reverted on branch A not present when merged from branch B
commit reverted on branch A not present when merged from branch B

Time:12-25

I accidentally commited to branch dev, instead of feature-branch, and pushed to the remote. Based on the advice from git undo commit on protected branch?, I took the following steps to undo the commit on dev and commit it to feature-branch

on dev, undo the commit locally and stash it, so I can later commit it to feature-branch:

  1. git reset --soft HEAD~1
  2. git restore --staged .
  3. git stash
  4. git pull
  5. git revert SHA-1
  6. git commit -m "reverting commit made to wrong branch"
  7. git push

on feature-branch:

  1. git stash pop
  2. git add ., git commit -m "making commit in the right place"

Great. At this point, dev is back to where it was before I wrongfully made the commit there, and feature-branch has the new changes. I then did some more work and added more commits to feature-branch.

After creating a pull request from feature-branch to dev and merging it in, it seems the commit that was reverted is not present on dev.

What is happening here?

For now I will just create a new branch and manually rewrite the commit, but I don't comprehend why this series of events has led me to see a diff in my IDE (vscode and gitlens), but github is telling me there is no diff?

CodePudding user response:

tl;dr: It appears that you attempted two different solutions for the same problem, and ended up not doing what you think you did.

If you accidentally committed something to dev, and wanted to fix it, you have two (categories of) ways to deal with it:

  1. You can reset back to the previous commit, essentially deleting the most recent commit.
  2. You can revert the commit.

It's not clear whether you had pushed out dev with the errant commit on it, but I suspect that you probably didn't, based on the description. In that case reset would have been the best way to go. Had you already pushed the mistaken commit and others were likely using it already, then revert would have been best. Apparently you tried to do both of these; it looks like you did a reset first followed by a revert.

The first reset "deleted" the commit, and then trying to revert something that isn't there wouldn't work, so you'd be left where you started. (Do you remember seeing any messages to this effect when you tried it?)

Note, had you previously pushed out dev and decided revert was the way to go, after resetting back a commit you wouldn't have been able to do git push without force pushing.

Side note, to move that commit over to feature-branch, you can simply cherry-pick that commit by the hash ID it had on dev, instead of trying to go through the extra work of stashing it, which I suspect is what led you down this path to begin with.

Tip: I highly recommend using git log or git log --graph often. Consider using a UI that displays the branch history and refreshing it after every commit you make so you can see exactly what you're doing, and mistakes will become obvious.

  • Related