Reputation: 845
I have two remote repositories per project: One is located on a local NAS and the other one on GitHub. I feel safer having my projects stored in two physically different locations. This works out very well within Visual Studio as long as I just push:
I just create two remote repositories, commit local changes and then push to each of the remote repositories.
But how does it work the other way? I need to be able to create branches, work with them, commit to both repos and merge again. How do I create the same new branch in both remote repositories?
Upvotes: 0
Views: 76
Reputation: 489748
How do I create the same new branch in both remote repositories?
Git is, in some ways, kind of cavalier about branch names. What Git really cares about are commits. When you run git push
you have your Git call up another Git on the Internet-phone; your Git hands to that other Git any new commits that you have, that they don't.
In order for their Git to remember those commits by name, your Git also gives their Git some name-updates: "please set this branch name to remember this specific commit." Here, there is an exception to Git's devil-may-care attitude about branch names: when their Git is receiving a git push
, if the name already exists, Git "wants" the push to do a fast-forward operation on the name, based on the new commits coming in.
If the name does not exist yet, their Git just sets the name, and now the name exists. So creating the branch is trivial: just git push
(perhaps with -u
as well if this is the first push to your default remote, usually known as origin
). The receiving Git, which does not have a branch of this name yet, will just create the name that you ask them to create.
To push repeatedly to two different "other Git"s, you probably want to make two different remotes (well, it sounds like you already did). Only one of these remotes can be the (single) upstream for your branch in your own repository, but you can git push remote1 branch:branch
and git push remote2 branch:branch
at any time. It's up to the receiving Git to create or update their branch of the name after the colon—or, perhaps, refuse to update because it's not a fast-forward operation.
If the other Git refuses the push, you can set the force flag and try again: this makes your request, "please set your branch named branch
to commit 1234567...
", into a command, without the polite "please" in front. They can still refuse, but by default, most receiving Gits will obey. (GitHub has web-page-based control knobs for setting whether to obey such commands. On your NAS, you can perhaps ssh in, and create and administer pre-receive
and update
hooks to do the same kind of controlling.)
Upvotes: 1