Reputation: 21
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
Upvotes: 2
Views: 179
Reputation: 3482
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.
Upvotes: 3