21

On a git/github project I am working on a branch. Upon a push, it said the following:

git push
To [email protected]:...
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:...'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.

I tried to fix this problem and upon Googleing I came up with this line:

git push --mirror

I issued the following command and now it seems that I have deleted a lot of branches from the server.

Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:...
 - [deleted]         develop
 + 797beee...bafbc50 master -> master (forced update)
 - [deleted]         milestone
 - [deleted]         robot
 - [deleted]         strategy
 * [new branch]      origin/HEAD -> origin/HEAD
 * [new branch]      origin/develop -> origin/develop
 * [new branch]      origin/master -> origin/master
 * [new branch]      origin/milestone -> origin/milestone
 * [new branch]      origin/robot -> origin/robot
 * [new branch]      origin/robot_simulator -> origin/robot_simulator
 * [new branch]      origin/strategy -> origin/strategy
 * [new branch]      origin/vision -> origin/vision

Can you tell me what has happened and how can I undo the change I made? (in case I deleted those branches)

2
  • You know, if you look at git push --help (which is just man git-push), in the section it mentions, it explains about the "problem", and describes the primary ways to deal with it...
    – Cascabel
    Feb 7, 2012 at 21:50
  • 6
    This question deserves points, but the author deserves punishment. Dec 8, 2014 at 16:04

3 Answers 3

14

You pushed to the default push target, [email protected]. This means, that [email protected] was a remote in you source repo.

That implies, that the refs deleted from the server would still be in the remote locally after the push. Do not update the remotes (!).

Verify this by doing

git branch -a

on the side you pushed from (local).

It will probably show the refs that were deleted from the remote server.

[to be continued]

You could do something like:

for-each-ref refs/remotes/origin | while read sha type name
do 
    git branch "rescue_$(basename "$name")" "$sha"
done

to recover the branches locally. They will be named prefixed with rescue_ just as a precaution (in case you get funny or conflicting ref names).

Replace origin with the name of your remote


Test script

In case you want to test the procedure in a controlled environment, here is my approach condensed to minimum steps (execute in an empty dir, e.g. /tmp/work)

git init A; (cd A; touch test; git add test; git commit -m initial; git branch test1; git branch test2; git branch test3)
git clone A B
(cd B; git push --mirror origin; git branch -a)
cd A
git for-each-ref refs/remotes/origin | while read sha type name; do git branch "rescue_$(basename "$name")" "$sha"; done
git branch -a

Note how in this version, I cd into A - which would be your github repo. You could git clone --mirror [email protected]:... local_rescue in order to get a suitable local version of that.

I recommend you play around getting to terms with the procedure before trying it out. It never hurts to backup your repositories along the way.

6
  • git push --mirror cleans up the remote branches. There won't be anything in refs/remotes/origin that's not on the remote. It's a good suggestion, and it'd be nice if it worked, but unfortunately it doesn't.
    – Cascabel
    Feb 7, 2012 at 21:58
  • @Jefromi: you didn't just test that, because I did. git version 1.7.4.1
    – sehe
    Feb 7, 2012 at 22:01
  • Er, so did I... hrm. I created a toy repo with two branches, made a bare clone, added the bare clone as a remote in the original, deleted one of the branches, did a mirror push, and the branch is gone from refs/remotes and logs/refs/remotes. Git version 1.7.7.3. Let's hope that either I somehow botched it, or the OP has your version.
    – Cascabel
    Feb 7, 2012 at 22:01
  • Funny, could it be that you remotes wasn't up-to-date before you pushed? I pushed from a single repo that just cloned a single branch from a 4-branch repo. After git push --mirror origin the clone still contained the other branch refs in remotes/origin/ (as did the push target, since it became a mirror).
    – sehe
    Feb 7, 2012 at 22:04
  • Never mind, I did botch it, oops. This is good. (You already had my +1.) Funny how cleaning up code makes you immediately notice the mistake!
    – Cascabel
    Feb 7, 2012 at 22:06
4

You were in a situation like this:

- x - A - B - C (origin/master)
   \
    D - E - F (master)

You wanted to do one of two things, both of which are described in the documentation that Git suggested you read:

  • Pull, then push, giving you this:

    - x - A - B - C
       \           \
        D - E - F - M (master, origin/master)
    
  • Force push (git push --force), giving you this:

    - x - D - E - F (master, origin/master)
    

Instead, with git push --mirror you basically did the equivalent of force-pushing everything, making the remote repository into a mirror of your local one. This means, just like it reported, deleting everything on the remote that wasn't in your repository.

Edit: sehe's answer describes how to recover. If you have run git remote update, which would have deleted the remote branches it uses to recover from, then the following could be useful. Otherwise, you're all set.

Your best bet is to find someone else who has cloned from the remote repository (or if you're lucky, a separate clone that you'd made), tell them not to pull/fetch/remote update, and follow sehe's instructions from that repo.

Failing that, recovering is going to be really tricky. If you've interacted with any of the remote branches recently, it's possible there will be traces in HEAD's reflog:

git reflog show

Otherwise, if the deleted refs refer to commits which are ancestors of any remaining branches, you can maybe recreate them quickly. If they refer to things which aren't ancestors of remaining branches, you can find dangling commits:

git fsck

and perhaps figure out which ones had branches pointed to them.

2
  • I have an other computer where 'git branch -a' still lists all the deleted branches. What would be the best way to recover from it?
    – hyperknot
    Feb 7, 2012 at 22:06
  • @zsero: Look at sehe's answer.
    – Cascabel
    Feb 7, 2012 at 22:11
-1

You need to run

git branch -a

enter image description here Then you can see all the deleted branches there.

Afterwards checkout to each entry and create a new branch from it.

Like

git checkout remotes/origin/endpoint
git checkout -b res_endpoint

Note: do it for all the entries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.