Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewind remote to a prior commit

Tags:

As a junior git user, I got overwhelmed by a tough merge and must have done something wrong. I ended up committing my conflict resolutions with a whole mess of garbage inside my source files. The commit shows additions of many lines which look like <<<<<<< HEAD and >>>>>>> a7b4de79431c2e73d28621c72c8d14820df1a24b. The commit has been pushed to remote origin already so I unfortunately can't just ammend the commit.

I want to rewind the remote repository to the last good commit, 4a3ba7b0e56cf0be80274c1f879029220a889bde and (if possible) destroy the bad commit d004651972cbc35f70ee5a2145b6e03169c77279.

I tried:

git checkout 4a3ba7 git push -f 

and got: fatal: You are not currently on a branch.

like image 941
Jesse Hallam Avatar asked Feb 18 '13 00:02

Jesse Hallam


People also ask

How do you go back to a prior commit?

Go back to the selected commit on your local environmentUse git checkout & the ID (in the same way you would checkout a branch) to go back: $ git checkout <commit-id> .


2 Answers

checkout moves your current working directory to a previous commit, but it does not modify branch contents. You need to reset the branch back to an old commit, and then push that.

git checkout ... git reset --hard 4a3ba7 git push -f 

that said, if you are already push -fing to change only the most recent commit, you should be able to use --amend.

git checkout ... // Fix the file git commit --amend git push -f 

If there are at least some changes that you want that were committed after 4a3ba7 then you can also do this:

git checkout ... git reset 4a3ba7 git add -p // Use the interactive prompt to choose the parts you want git commit git push -f 

Update

Your error remote: error: denying non-fast-forward refs/heads/master is because the git server that you are using, Assembla, does not allow rewriting history by default. See this answer for fixing that part: Undo git push to Assembla

like image 146
loganfsmyth Avatar answered Oct 25 '22 04:10

loganfsmyth


You don't need to checkout things locally to rewind a remote branch; you can just use

git push -f origin 4a3ba7b0:master 

Of course double check your logs before doing anything, as this push will overwrite remote data.

If you receive permission errors, receive.denyNonFastForwards might be set to true in the remote repository; you have to change that for a rewind to work in any case.

like image 20
Marco Leogrande Avatar answered Oct 25 '22 04:10

Marco Leogrande