126

I just committed the wrong source to my project using --force option.

Is it possible to revert? I understand that all previous branches have been overwritten using -f option, so I may have screwed up my previous revisions.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
David van Dugteren
  • 3,879
  • 9
  • 33
  • 48
  • possible duplicate of [Is there anyway to undo git push -f?](http://stackoverflow.com/questions/14476236/is-there-anyway-to-undo-git-push-f) – cmbuckley Jul 23 '13 at 18:42

10 Answers10

68

Git generally doesn't throw anything away, but recovering from this may still be tricky.

If you have the correct source then you could just push it into the remote with the --force option. Git won't have deleted any branches unless you told it to. If you have actually lost commits then take a look at this useful guide to recovering commits. If you know the SHA-1 of the commits you want then you're probably OK.

Best thing to do: Back everything up and see what is still in your local repository. Do the same on the remote if possible. Use git fsck to see if you can recover things, and above all DO NOT run git gc.

Above above all, never use the --force option unless you really, really mean it.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
  • 82
    You can very likely just look at the reflogs to determine where the remote branches originally were. For example, `git reflog show remotes/origin/master`. You should be able to see your push in there; the commit in the previous line is where it was before you messed it up. You can then just push that revision (with `--force`) to origin, and be back where you were! – Cascabel Oct 20 '10 at 01:31
  • @David: Oh. You didn't mention in your question that you didn't have the repo. (This is of course something you never want to do.) If you have filesystem access where you pushed to, though, you could still do all of this there. – Cascabel Oct 20 '10 at 01:34
  • 1
    @David: Yikes. Always good to have your current directory as part of your prompt to avoid that sort of thing. – Cascabel Oct 20 '10 at 01:35
  • 1
    @Jefromi I think what you said there is the actual anwer: Even with an old version (not having `git fetch`ed for a long time) you can display the reflog of GitHub's side and recover! – nh2 Sep 13 '12 at 19:12
  • Of course, if they come online and pull, their repo will be screwed, so better to reach out to them in advance, for future reference. That's what I'd do if I got myself in this situation, look at the committers and try to get in touch with one likely to have the latest sets of commits. – Kzqai Jul 09 '13 at 17:17
  • @Jefromi has the right answer. I used his way to recover the changes. – Jason Kim Feb 20 '15 at 20:10
  • A github specific solution: http://stackoverflow.com/a/43271529/3638234 – GHugo Apr 07 '17 at 06:57
  • You should use `git --force-with-lease` instead of `git --force` – Moebius Jan 31 '18 at 14:59
  • 4
    Where is this answer by @Jefromi? I don't see that user mentioned on this page outside of this comment thread. – Don McCurdy Jun 18 '18 at 16:25
60

If you know the commit hash, it's easy, just recreate your branch.

5794458...b459f069 master -> master (forced update)

Delete the remote branch:

git push origin :master

then recreate your branch with the following commands:

git checkout 5794458
git branch master
git push origin master
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
user1094125
  • 659
  • 5
  • 3
58

The solution is already mentioned here

# work on local master
git checkout master

# reset to the previous state of origin/master, as recorded by reflog
git reset --hard origin/master@{1}

# at this point verify that this is indeed the desired commit.
# (if necessary, use git reflog to find the right one, and
# git reset --hard to that one)

# finally, push the master branch (and only the master branch) to the server
git push -f origin master
Community
  • 1
  • 1
Abdelhafid
  • 799
  • 7
  • 13
37

Yes you can recover commits after git push -f your_branch

Text from Doc:

Prune entries older than the specified time. If this option is not specified, the expiration time is taken from the configuration setting gc.reflogExpire, which in turn defaults to 90 days. --expire=all prunes entries regardless of their age; --expire=never turns off pruning of reachable entries (but see --expire-unreachable).

So you can do:

1- git reflog

enter image description here

2- you choose Head_Number does you want recover with git reset –hard HEAD@{HEAD-NUMBER}

enter image description here

3- you can see all commits on this head by git cherry -v branch_name

4- in the end you should force push git push -f branch_name

OR

1- get the number of SHA from your GIT client (interface)

git reset --hard commit_SHA

2- force push

git push -f your_branch

Hope this helps

يعقوب
  • 1,008
  • 13
  • 14
10

If you are not on that local repo where the forced push came from, at origin/master level there is no way to recover. But if you are lucky enough to use GitHub or GitHub for Enterprise, you can have a look to the REST API and retrieve lost commit as patch, example:

  1. List events and find the commit sha1 long format

https://api.github.com/repos/apache/logging-log4j2/events

  1. Download the lost commit and retrieve the related patch in the json path .files[]/patch

https://api.github.com/repos/apache/logging-log4j2/commits/889232e28f3863d2a17392c06c1dd8cac68485de

  1. Apply locally and push again

git apply patch.patch && git commit -m "restored commit" && git push origin master

5

Another way to recover the lost commit or even to figure out what commits were lost, if the previous push came not from your local repo, is to look at your CI machine.

If you have a job which tests the master branch after every commit (or series of consecutive commits), which you should have, you can have a look what it was testing last. That is the commit you need to restore.

The CI machine may even keep a local clone of the repo, from which you may be able to perform this recovery.

Source: probably Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation (Addison-Wesley Signature Series (Fowler))

user7610
  • 25,267
  • 15
  • 124
  • 150
2

I did the same thing while undoing a last push for only one file. Ended up going to back to original state of the repository. I was using git commands from Linus as I had the local copy on Linux. Luckily that copy was still intact.

All I did was (after frantically making few more copies of the local repo):

git add .
git status

(it said that origin/master was ahead by 68 commits, fine ... those were all the commits I deleted)

git remote set-url origin <GIT_SSH_URL>
git push

And everything got restored the way it was before I did forceful push. The most important thing to remember is never to do a git checkout . after you had forcefully pushed. But the best practice is to disable push option. I am never using it ever again. Learnt my lesson!!

Fabio Mora
  • 5,339
  • 2
  • 20
  • 30
Pran
  • 21
  • 2
2

For people in really bad situations like I was (for example, if you're getting bad object errors when running git reset --hard):

I wrote a script called treesaver that pulls all your files from the GitHub API as a last resort. Here's how to use it:

  1. Clone the treesaver script and cd to it.
  2. Find the SHA string of the tree you'd like to restore by accessing https://api.github.com/repos/<your_username_or_org>/<repo>/events.
  3. In the payload property corresponding to your push event, find the commit you'd like to revert to and click on its url.
  4. Under commit.tree, copy the tree's url.
  5. Run python3 main.py <tree_url> <path_to_save_to>.

For example, in my case, I would run:

python3 main.py https://api.github.com/repos/anthonykrivonos/my-repo/git/trees/1234567 .

Of course, PRs welcome.

Anthony Krivonos
  • 4,596
  • 4
  • 16
  • 31
0

Here you can read decisions https://evilmartians.com/chronicles/git-push---force-and-how-to-deal-with-it

The second one helped me. I did wrong these commands

1) (some-branch) git pull -> correct command was git pull origin some-branch

2) (some-branch) git push -f origin some-branch

After these commands I lost three commits. To recover them I looked to terminal where I did wrongly 'git pull' and have seen there output like

60223bf...0b258eb some-branch -> origin/some-branch

The second hash 0b258eb was exactly what I needed. So, I took this hash and produce command

git push --force origin 0b258eb:some-branch
Andrey
  • 191
  • 2
  • 6
0

I tried as below. Hope someone might benefit.

5794458...b459f069 dev_branch -> dev_branch (forced update)

Go to GitLab -> Repository menu -> Branches option -> New Branch

Enter Branch Name, Create From as first COMMIT_ID from above message and Enter -> Create Branch.

This method solved my case and restored all the changes into new branch.

Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42