2

For some reason, remote git repository was broken and there aren´t backups, but there are a lot of local repositories in developer machines. Every developer machine has a few branches that they use, but anyone has all branches checkout in local repository.

Is recommended use a local repository to rebuild remote repository?

We tried to push all local branches in remotes repository, but there were several branches that no one has in local. For these branches we build a shell script to checkout and push, but I am not sure if it is the correct way.

This was the created shell script:

#!/bin/bash
clear

echo "*****************************************"
echo "Disaster Recovery. "
echo "Script for massive Git checkout and push."
echo "*****************************************"

for i in $(git branch -r)
do 
    find="origin/";
    replace="";
    branch=${i//$find/$replace}

    echo "Init checkout" $branch
    exec git branch -d $branch | git checkout -b $branch --track origin/$branch
    echo "End checkout" $branch 
done; 

echo "Init push -all"
echo exec git push --all
echo "End push -all"

All branches was recovery but some was outdated for long time difference. In this case, the newest local developer branch was searched and pushed. Some branches remained outdated, because no one has recent local branch.

There are others way to rebuild all remote git repository without git server backup? Thanks for your time, community

  • 1
    But what do you mean that **no developer has some branches in local**? They fetched (every single on of them) specifying the exact branches they needed? Because the last person who did the last (general) fetch from the repo should have the latest of all the remote branches/tags from the broken remote. It' s just a matter of pushing those remote branches (as seen on that local) into the new remote. – eftshift0 Aug 02 '19 at 17:07
  • Do you mean branches are missing in `git branch -r` output? Normally they would all be there. – Jonas Berlin Aug 02 '19 at 17:30
  • The branches will indeed still be there. Your pull requests, provided you have them, are not a part of your repository. That is what you will lose. So that should be in a backup in the future. – Michiel Leegwater Aug 02 '19 at 17:57
  • @JonasBerlin no, this command show all branches, but when I consult the server git directories after `git push --all` only local branches are seen – Juan Jiménez Aug 02 '19 at 20:05
  • Yeah.. `push --all` pushes all local branches e.g. branches in `refs/heads/` while the command in my answer pushes local copies of remote branches e.g. branches in `refs/remotes/origin/`. You can list all local branches, local copies of remote branches and tags using the command `git show-ref`. – Jonas Berlin Aug 03 '19 at 13:53

1 Answers1

3

Assuming that git branch -r does indeed show copies of all (or most of) the branches, the command to push one's local copies of remote branches should be like below. This assumes "origin" is the name the user used for the remote repository. If not, please change the command in both places where origin occurs.

git push origin --tags 'refs/remotes/origin/*:refs/heads/*'

Now it is fully possible that you don't have the latest copy of the repo and that it is not clear who has. But assuming the remote repo has default settings i.e. to block so called "non-fast-forwards", all users can run this same command safely, and they will update the branches only if they have newer copies than already on the server.

The --tags makes sure to also push any tags to the remote repo.

If you already pushed branches to your repo, you might end up with some extra branches. If you end up with a mess, you can try renaming the remote repo to something else (to keep as backup in case all else fails) and create a new remote repo and start fresh with my command above.

The git branch -d commands you had in your example are totally wrong, they will just end up deleting your local copies, do not do that! Also the checkout commands you specified are probably not a good way to go either. You should not need anything else than git push commands like the one above to recover the remote repository.

Jonas Berlin
  • 3,344
  • 1
  • 27
  • 33