Home > Blockchain >  git push all branches from one remote to another remote iteratively
git push all branches from one remote to another remote iteratively

Time:02-16

I am having a use case where we are migrating from one remote to another for a project.

legacy_remote : The existing remote which has been existing so far. new_remote : The new remote on which all data has to be pushed from legacy remote.

I am using in following commands.

git remote set-url origin <legacy_remote>
git checkout master
git pull
git remote set-url origin <new_remote>
git push --force -u origin --all 
git push origin --tags 

The reason i am using force keyword is that every time i do a merge i don't want to resolve the conflicts in new remote but rather replace the content in there on new remote as its not being used so far.

The problem which is happening is that command :

git push --force -u origin --all 

This command is only syncing master to master but not moving all branches from legacy remote onto the new remote. Can anyone please tell what's missing in command.

Thanks

CodePudding user response:

reason not using mirror is that there are couple of new branches created on new remote which don't want to be overridden

Then try

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

Compared to git push --force -u origin --all, the branches don't have to be checked out first, as mentioned here.

CodePudding user response:

Besides VonC's answer, you can also do:

git push newremote localname1:remotename1 localname2:remotename2 ...

Replace the ... with more localname:remotename pairs as desired. Each local name can be a branch or remote-tracking name: refs/heads/branch or refs/remotes/origin/name, and each remote name should usually be a branch name you'd like to create or update on the remote. Spelling it out in full, as with, e.g., refs/heads/somebranch, is a good idea in general; it's literally required if and only if Git won't do that for you, but it's hard to know in advance when Git will do that for you, so it's better to just go ahead and do it.

You can use --force, or not, as you please.

In a shell script, you can build up the set of names to push:

names=
# use some local branch names
for b in br1 br2 br3; do
    names="$names refs/heads/$b:refs/heads/$b"
done
# and some remote-tracking names
for r in br3 br4 br5; do
    names="$names refs/remotes/origin/$r:refs/heads/$r"
done

# show what we would run
echo git push --force origin $names

Run the script, tweak until you have the right set of names, and then take out the echo and run it one last time.

  •  Tags:  
  • git
  • Related