3

I have a git repo with only one commit. In my local master branch, I have accidentally made a git commit --amend so now I get a message saying

On branch master 

Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively. 

How can I discard the amended commit and go back to the original commit? I want to have my local master and origin/master up to date with nothing to commit

fs314
  • 99
  • 1
  • 7
  • 2
    Does this answer your question? [How to revert initial git commit?](https://stackoverflow.com/questions/6632191/how-to-revert-initial-git-commit) – Thirumal Aug 07 '20 at 09:08

3 Answers3

4

You can use the reflog to reset master to the commit it referenced before the current one:

git reset --hard master@{1}

The reflog is a real life saver in situations like these. It's well worth reading up on it in the Git Book:

At some point in your Git journey, you may accidentally lose a commit. Generally, this happens because you force-delete a branch that had work on it, and it turns out you wanted the branch after all; or you hard-reset a branch, thus abandoning commits that you wanted something from. Assuming this happens, how can you get your commits back? [...]

Often, the quickest way is to use a tool called git reflog.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
1

You can reset your current master branch to be the same as the origin one with:

git reset --hard origin/master

Note that this will remove any changes on your current master branch that are not in origin/master.

DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • 1
    While this will likely do the job in OP's case, I wouldn't recommend it as a general way to "undo" your local commits, since you may end up with new commits from the remote branch that you might not be interested in quite yet. – Enrico Campidoglio Aug 07 '20 at 09:23
1

Since, you are on 'origin/master' and wants to reset use below git reset comand.

git reset --hard origin/master

Explanation: From git documentation

git-reset - Reset current HEAD to the specified state

--hard - it is the mode option (optional). hard Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.

Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32