开发者

Push to same git remote using two different URLs

I have a bare repository on my home-server to which I push from my laptop for backup. Due to a new router I cannot push anymore from within my home-LAN to the home-server using the global URL (something to do with "NAT loopback") but need to use the local LAN address. So my .git/config now contains two remotes for the same 开发者_运维问答bare repository:

[remote "home1"]
        url = ssh://username@my.url/home/username/git_bare_repos/repo.git
        mirror = true
[remote "home2"]
        url = ssh://username@192.168.1.74/home/username/git_bare_repos/repo.git
        mirror = true

So, questions: Is this ok? Are there potential hazards/pitfalls? And is there a better way to do this, so I wouldn't have to use different commands depending on whether I'm at home or not?


It's ok. They're the same repo after all.

The only difference for you is that depending on your current location you'll have to push/pull/etc from one remote or the other.

Git keeps a reference for every branch, tag, remote-tracking branch, etc. In fact, they are all references to commits, and they have a file representation in .git/refs/....

By comparing these references git knows the status of each branch, tag, etc. For example, if you compare refs/heads/master to refs/remotes/origin/master, and the branches are not sync'd, then these refs will differ. To find which one is outdated, git can just verify if the remote commit exists in the local branch, and if it does, then the remote branch is outdated. This also allows git to tell you how many commits your local branch is ahead of the remote branch, and vice-versa.

A more in-depth explanation can be found here.


Let's say you pull the latest from home1, make some changes, and push them back. Then you do some work from the other location and go to push them to home2. Git doesn't know home1 and home2 are the same repository, but it doesn't matter. Git sees that home2 "somehow" already got the changes you pushed earlier to home1 and just pushes up your new changes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜