-1

I have mistakenly committed code and pushed it to master from GIT Bash, instead of doing commit new code to new branches, then merge to the master branch.

How can I undo my commit to the master branch. I have the HEAD at 40b66b17

Tried below options:

<MY_ID>@<Machine> MINGW32 /c (master)
$ git reset --hard 40b66b17
HEAD is now at 40b66b1 Add Components draft 



<MY_ID>@<Machine> MINGW32 /c (master)
$ git push -f origin master

Everything up-to-date


<MY_ID>@<Machine> MINGW32 /c (master)
$ git show ORIG_HEAD
commit 40b66b17540f0ac5aa6ec83b8c843b7e9674e063
Author: My Name <My Email Id>
Date:   Wed Feb 17 11:14:17 2016 -0600

    Add Components draft 

diff --git a/File1 b/File2
new file mode 100644

Still when I see the current HEAD pointer, it is showing at the same place. I want to have my master clean without any commits. My apologies with terminology here. As I am new to GIT.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Naga Vemprala
  • 718
  • 3
  • 16
  • 38
  • Possible duplicate of [Git - Undo pushed commits](http://stackoverflow.com/questions/22682870/git-undo-pushed-commits) – Andrew C Feb 20 '16 at 00:33

1 Answers1

7

Well if you have already pushed it, and others may have already pulled since then, I would recommend not doing any history rewriting, such as git reset. However, if you are the only one that uses the repo or you are sure nobody has pulled since your last push, do:

git checkout master
git reset --soft head~
git checkout -b my-feature-branch
git commit -m "My message"
git checkout master
git push -f

This will move your last commit from master to my-feature-branch.

Now, if you are not sure if someone might have already pulled your commit, the safest thing would be to just undo the effects of your commit. Do that with:

git revert HEAD
git push
git checkout -b my-feature-branch
git cherry-pick HEAD~
David Deutsch
  • 17,443
  • 4
  • 47
  • 54
  • when I did $ git fetch origin master, FETCH_HEAD is showing at master. Does PUSH at this point makes my master repository clean? – Naga Vemprala Feb 19 '16 at 18:33
  • When I am doing git reset --soft head~ I am getting error fatal: ambiguous argument 'head~': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]' – Naga Vemprala Feb 19 '16 at 18:34
  • @NagaVemprala, that is really bizarre; the only way I can see that happening is if you were on the very first commit of a repo. How many commits do you see if you do `git log --oneline`? – David Deutsch Feb 19 '16 at 18:48
  • There are two commits. $ git log --oneline b342c4b Revert "Add Components draft" Reverting back commit to master This reverts commit 40b66b17540f0ac5aa6ec83b8c843b7e9674e063. 40b66b1 Add Components draft – Naga Vemprala Feb 19 '16 at 18:54
  • I think the commit b342c4b is added when I typed the first command itself "git revert HEAD" – Naga Vemprala Feb 19 '16 at 18:55
  • @NagaVemprala - So it sounds as if the "Add Components draft" was the very first commit on this repo, you tried the `git reset` solution, got the error, then tried the `git revert` solution, and then did `git log`. Am I correct? – David Deutsch Feb 19 '16 at 19:02
  • Yes David. I got some error with git reset. Then tried git revert HEAD. It asked me to type a comment. Then gave a comment to revert the changes. After that tried git push. Got error again that "You are not allowed to push code to protected branches on this project." – Naga Vemprala Feb 19 '16 at 19:07
  • @NagaVemprala - Hold on, you were able to push `master` before (i.e.when you pushed `40b66b1`) but now you do not have permissions? What changed? – David Deutsch Feb 19 '16 at 19:12
  • I was removed the permission to push it on developer role. Got it back. Now I tried to PUSH . It worked. But for some reason we should not have commits on the master. How to delete the commits on master? The REVERT also created a new commit on master. – Naga Vemprala Feb 19 '16 at 20:49
  • IF nobody has pulled since this all began, and IF those two commits are still the only two commits on master, then delete master on the server with `git push origin :master`. Then on your local machine do `git branch myfeature master~` so that `myfeature` now has your first commit, then delete master locally with `git branch -D master`. – David Deutsch Feb 19 '16 at 20:56