fs314
fs314

Reputation: 109

Git - how can I go back to initial commit?

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

Upvotes: 3

Views: 2272

Answers (3)

Swatantra Kumar
Swatantra Kumar

Reputation: 1330

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.

Upvotes: 1

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59885

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.

Upvotes: 4

DanielGibbs
DanielGibbs

Reputation: 10173

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.

Upvotes: 1

Related Questions