4

I was going to test something in dev branch (just a version number 0.1 --> 0.2), so I committed the change but I don't have the permission to push it to dev directly,then I created a feature branch and I found the number is already 0.2, then I changed it to 0.3 and pushed this branch, then created a merge request to merge it into dev.

I'm new to git, just wondering how can I prevent this kind of messy in the future? How can I revert my commit after the push being rejected?

wawawa
  • 2,835
  • 6
  • 44
  • 105
  • Does this answer your question? [Git - Undo pushed commits](https://stackoverflow.com/questions/22682870/git-undo-pushed-commits) – SwissCodeMen Sep 16 '20 at 20:13
  • @SwissCodeMen: that's for the case where the `git push` *succeeded*, and then you realize that, oops, that was a bad idea after all. – torek Sep 17 '20 at 01:32
  • I'm not going to mark this a duplicate (yet?) but see [How do I undo the most recent local commits in Git?](https://stackoverflow.com/q/927358/1256452) – torek Sep 17 '20 at 01:34

3 Answers3

8

You could've solved it by reverting the last commit using the command

git reset HEAD~

which would undo your last commit and get your changes of the last commit back to uncommitted state. You can then checkout a feature branch, make the commit and push the feature branch to your remote repo.

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
1

You can simply drop the commit from your Git history.

If the commit hasn't been pushed, the following steps can work -

  1. Get on the commit you wanna remove.

  2. Run git rebase -i HEAD~2

  3. Before the commit entry in the file that opens up, change pick to drop.

  4. Exit the editor. You should now see the last commit gone.

If you already had pushed the unwanted commit, you'd need to force push to remote. But, force push is only advisable for the feature branch - never force push to the master!

roshnet
  • 1,695
  • 18
  • 22
0

Similar to roshnet's answer, I was able to do this with the GUI app "Git Extensions", by

  1. right-clicking on the commit I wanted to reset/rebase back to,
  2. choosing option "Rebase current branch on >" with sub-option "selected commit interactively..."

(click to see image, as my rep is to low to embed them)
right-click menu for undoing a commit with an interactive rebase

This opens up an editor window for the rebasing git command, which lets you choose how handle removing the commits above the rebase commit, and also has an glossary explaining what each command does.

(click to see image, as my rep is to low to embed them)
Initial editor window

For me, I wanted to undo a commit which added several new files, which I already had backups of in a external folder. So I changed 'pick' to 'drop', like so:

(click to see image, as my rep is to low to embed them)
3. pop-up editor window

  1. Then I clicked the 'save' button and closed the editor window, and click 'OK' on the command pop-up window, and it was complete.

My un-pushed commits are now gone and my local repo is now reset back to the same commit Head as the Origin repo. My staging and working directory were also emptied. But thankfully I had backed up my new files and could re-commit them to a separate branch that I do have push permissions on.

Further documentation on rebasing with Git Extensions (a free git gui app) can be found here: Git Extensions - How To: Squash and Rebase your changes