简体   繁体   中英

Git: pulling from forked repo and pushing to my fork

I have forked a git repo (call it upstream) and I have a forked copy of it on my github (call it origin) , and I have a local copy of the fork on my machine. I want to keep my fork up to date with the original repo.

The project has several branches, and I might be working on any of them in a given day. I check out my local copy of branch, and I want to update it with the latest changes from upstream/branch, and then push those changes to origin/branch so that everything is up to date.

Is there a way to set a branch to pull from remote but push to origin? I can do it now by manually setting the tracked branch to upstream/branch, doing git pull, then manually setting the tracked branch to origin/branch and doing git push. This works, but it seems very clumsy. Is there a cleaner way to do this?

IMO in situations like this I prefer my push and pull statements to be explicit, even if it's a bit more typing.

For example, rather than relying on things like tracking branches, I will use the form:

git push|pull <remote> <branch>

Example:

# pull from remote
git pull remote master

# push to origin
git push origin master

This way, I am confident that I've pulled/pushed from/to the correct location.

If this seems like too much typing, you could always define an alias:

git config --global alias.pullr "pull remote master"
git config --global alias.pusho "push origin master"

and invoke those aliases like this:

git pullr
git pusho

After running those git-config commands, you can edit your aliases in your ~/.gitconfig file.

While I also typically use Jonathan's approach, the way you accomplish what you describe in the question is:

git remote set-url --push REMOTE_NAME URL_OF_ORIGIN_FORK

For example if your remote is named "upstream", you would say:

git remote set-url --push upstream URL_OF_ORIGIN_FORK

This will make git push send changes to "origin" and git pull will grab changes from "upstream".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM