1

I know that a forked repo can be quickly synchronized with the upstream repo [am I using the correct term?] directly on the GitHub web page, using the button "Sync fork".

I'd like to do essentially the same not via some browser, but from the command line, with some kind of git ... command. However, I'd like to avoid "cloning" or "downloading" the fork itself; in other words, I'd like not to have the files form the fork on my local machine. (The reason is that I have the upstream repo already cloned on my machine.)

Is this at all possible? How? Thank you!

pglpm
  • 516
  • 4
  • 14
  • 1
    "*I have the upstream repo already cloned*" So why not update your local clone and then push to fork? You can pull from one repo (upstream) and push to another (your fork). – phd Aug 19 '23 at 12:51
  • @phd I didn't know I can do that! (complete noob about git). That should solve my problem. Please feel free to post your comment as an answer. Cheers! – pglpm Aug 19 '23 at 12:58

1 Answers1

2

You already have an upstream cloned locally so you don't need another clone. Just update your local clone from the upstream and push to the fork. To simplify things name your remote URLs upstream and origin. If your local clone already has origin rename it to upstream and add origin pointing to your fork. Something like this (note placeholders):

cd /path/to/local/clone
git remote rename origin upstream
git remote add origin https://github.com/myuser/myfork.git

This need to be done only once. Now every time you need to update your fork from upstream do

cd /path/to/local/clone
git pull upstream master # or whatever branch you update
git push origin master
phd
  • 82,685
  • 13
  • 120
  • 165
  • This has been very helpful also in directing me towards commands I didn't know existed. Thank you. If I may add a question: suppose nobody is really making changes to the forked repo, and my local copy of the original (upstream) repo is completely up to date. Could I then push to the fork with `git push https://github.com/otheruser/fork.git master`, without renaming? – pglpm Aug 19 '23 at 19:40
  • Edit: looks like it works! – pglpm Aug 19 '23 at 19:50
  • 1
    @pglpm `git push https://github.com/otheruser/fork.git master` works for sure but you will not have remote-tracking branch `origin/master` that should point to the last commit pushed to the fork. It's not required but I find it quite convenient. Also I find convenient to type `git push origin master` — it's simpler, shorter and bash completion works. – phd Aug 19 '23 at 20:42
  • Both good points, thank you! – pglpm Aug 19 '23 at 20:46