Search code examples
gitgithubrepository

How to sync forked repo with upstream via command line but without cloning locally?


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!


Solution

  • 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