1

Pulling from GitHub is slower than pulling from a local machine so I have a mirror set up. Cron keeps it mostly up to date. Dev machine then ran:

git clone git://server/llvm-project.git
git remote set-url --push origin git@github.com:llvm/llvm-project.git

So pushes always go to GitHub and pulls hit locally. Works fine until server is unavailable, e.g. because I don't have a VPN running.

I'd like to detect when server is unavailable, elegantly if possible but I'll just check the local IP if not, and have it fetch from GitHub directly in that case.

How do I configure this, and can it be done in secondary fallback fashion or must I change the upstream then change it back?

Thanks!

Equivalent but maybe simpler - happy with pull from local cache, then regardless of whether that worked, pull from GitHub. That's somewhat better in that it handles server lagging GitHub too.

Jon Chesterfield
  • 2,251
  • 1
  • 20
  • 30
  • 2
    What is "the real one"? The whole point (or, one of the major points) of git is that there is no difference between repos, and the only distinction between them is that you distinguish. I guess the point of my comment is that instead of calling it "the real one", you should call it "the remote". The repo on github is neither more nor less real than the one on your local machine. – William Pursell Nov 19 '22 at 12:31
  • 1
    Is this for something you run manually, or for an automated task? If the former, I would say you should do nothing; when you see that the pull from github fails, either ignore it or rerun to pull from the local. If the latter, it will greatly depend on how you want to determine if "the server is unavailable". Whatever your vpn is, perhaps you can check that robustly, but that won't solve the problem. Probably best is to simply `git pull remote || git pull local` (eg, automatically attempt to pull from the local only if the pull from remote fails, using `git pull` itself to detect the access – William Pursell Nov 19 '22 at 12:35
  • The real one here is llvm-project on GitHub. It's the one changes are pushed to. The non-real one would be the cache sitting on the LAN which I'd like to be less visible. There's thus two remotes, one of them with better uptime. I suppose I'd also prefer to pull from the LAN if GitHub is down. – Jon Chesterfield Nov 19 '22 at 12:35
  • Other comment has the kernel of a solution - I only have one remote configured here but could have both. Unclear how that works when they have the same contents modulo a time lag – Jon Chesterfield Nov 19 '22 at 12:36
  • isn't switching git remote need to be done [manually](https://stackoverflow.com/questions/42830557/git-remote-add-origin-vs-remote-set-url-origin)? there is no git-based solution out of the box. perhaps writing a bash script or something to do the switch can help. – Bagus Tesa Nov 19 '22 at 12:45
  • This seems equivalent but for push, the same invocation may do the right thing for pull https://stackoverflow.com/questions/849308/how-can-i-pull-push-from-multiple-remote-locations – Jon Chesterfield Nov 19 '22 at 12:50
  • @JonChesterfield I just tested and I can't make that solution work for pull. And, I don't know how pull from two remotes for the same `origin` would work: if the two remotes disagreed on where a branch was, which version would you actually fetch? Pushing to two remotes is straightforward, there's one source you're trying to push to two places, but pulling from two remotes brings in all the issues of conflict resolution, so I would not expect any automated support for it, except by setting it up as two independent remotes. – joanis Nov 19 '22 at 14:52
  • @joanis likewise :(. It's well formed if the one remote doesn't rewrite history, the second mirrors from it, and you always pull from the former first - but that's a bit of an edge case – Jon Chesterfield Nov 19 '22 at 15:07
  • Yeah, it's an edge case that could be custom scripted fairly easily, but would indeed require custom scripting. – joanis Nov 19 '22 at 15:25

1 Answers1

0

You can add two separate remotes with different names. For example, you could do git remote add local git://server/llvm-project.git and git remote add gh git@github.com:llvm/llvm-project.git. Then, you can run git pull local or git pull gh, depending on which you want to do.

Note that there is no automatic fallback here. Git doesn't offer that functionality, so you have to fetch or pull by hand, specifying different remotes.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • I'd also suggest adding an alias or similar to `git fetch local; git fetch gh` (and then just avoid `git pull` altogether, which I do anyway, or maybe have the second one use `git pull` if one really wants). – torek Nov 22 '22 at 10:51