How to recover from git push -force?

Here's what happened:

I have two remote git branches: master and feature1 . For some reason I have to use git push --force for the feature1 branch, but I did not know when I use git push --force , it also presses the master branch. Then disaster occurred when I moved the local master branch to the remote repository.

Fortunately, my local branch is not too far from the console. Basically, my remote master has two download requests combined before my local master .

So my problem is: can I reopen the retrieval request and retry? I noticed that there is a commit version for the merge request, so I worry if I just make a new pull request, will it mess up something? Ideally, I just want to repeat the merge of the two queries.

Is there any other way to recover from this disaster? I found out that --force is a really, really bad choice. --force

Update, an example of what happened:

I have the following branches:

 master feature1 origin/master origin/feature1 

I am merging two Auto merge pull requests using GitHub Auto merge pull requests . Then I did not get the master branch on my local machine. Thus, I think that my origin/master is the two versions behind the remote master.

Then I accidentally used git -f push , which overwrite the remote branch, and now I lost the commits from the migration requests in the remote repository.

How can I recover from it without ruining the history of other authors?

+25
git github
Sep 24 '12 at 15:55
source share
2 answers

You can always restore the previously observed state of master by dropping it to the old latch and issuing another push -f . The steps are usually as follows:

 # 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 

Note, however, that this restores the remote master to a state recently obtained using git fetch or equivalent. Any commits pushed by others after the last time you receive will be lost. However, these commits will continue to be available in their logs, so they can restore them using the steps described above.

+66
Sep 24
source share

Please note that with Github you can use the API to restore push pushing even if you don’t have local repository storage (i.e. when you don’t have a reflog) or commit sha.

First, you should get the previous commit command, one before forcing it:

 curl -u <username> https://api.github.com/repos/:owner/:repo/events 

Then you can create a branch from this step:

 curl -u <github-username> -X POST -d '{"ref":"refs/heads/<new-branch-name>", "sha":"<sha-from-step-1>"}' https://api.github.com/repos/:owner/:repo/git/refs 

Finally, you can clone the repository locally and force-click the wizard again:

 git clone repo@github git checkout master git reset --hard origin/<new-branch-name> git push -f origin master 

Please note that for two-factor authentication, you need to provide a token (see here for more information).

Credit: Sankara Rameswaran

+2
Apr 7 '17 at 6:54 on
source share



All Articles