ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between origin and upstream on GitHub?

What is the difference between origin and upstream on GitHub?

When a git branch -a command is executed, some branches it displays have a prefix of origin (remotes/origin/..) while others have a prefix of upstream (remotes/upstream/..).


V
VonC

This should be understood in the context of GitHub forks (where you fork a GitHub repo on GitHub before cloning that fork locally).

upstream generally refers to the original repo that you have forked (see also "Definition of “downstream” and “upstream”" for more on upstream term)

origin is your fork: your own repo on GitHub, clone of the original repo of GitHub

From the GitHub page:

When a repo is cloned, it has a default remote called origin that points to your fork on GitHub, not the original repo it was forked from. To keep track of the original repo, you need to add another remote named upstream

git remote add upstream https://github.com/<aUser>/<aRepo.git>

(with aUser/aRepo the reference for the original creator and repository, that you have forked)

Note: since Sept. 2021, the unauthenticated git protocol (git://...) on port 9418 is no longer supported on GitHub.

You will use upstream to fetch from the original repo (in order to keep your local copy in sync with the project you want to contribute to).

git fetch upstream

(git fetch alone would fetch from origin by default, which is not what is needed here)

You will use origin to pull and push since you can contribute to your own repository.

git pull
git push

(again, without parameters, 'origin' is used by default)

You will contribute back to the upstream repo by making a pull request.

https://i.stack.imgur.com/cEJjT.png


It also helps knowing what upstream is generally: stackoverflow.com/questions/2739376/…
@MaxRydahlAndersen true, but I like using Git without wrapper, so I will keep that convention (upstream vs. origin) for now.
By far the best explanation of how forks work that I have seen. You get my upvote.
Great work on the visual. Very straight forward and understandable answer. This was exactly what I was looking for.
@iamrudra if git remote -v shows the same url for origin and upstream, then yes, you are pushing to the same remote repo.
T
TUSqasi

In a nutshell answer.

origin: the fork

upstream: the forked


J
Jude Ukana

after cloning a fork you have to explicitly add a remote upstream, with git add remote "the original repo you forked from". This becomes your upstream, you mostly fetch and merge from your upstream. Any other business such as pushing from your local to upstream should be done using pull request.


can't do pull request from local, local first has to be uploaded to repository/remote..
N/B - The pull request mentioned in my comment above implies to making a contribution from your forked version on your git to the original repo(in this case the upstream of your local)
I created a repo on github, cloned it to my local, then created a branch(locally), made some changes to code, when I tried to push to remote from the branch newly created, it says fatal: The current branch branchName has no upstream branch. push the current branch and set the remote as upstream, like - git push --set-upstream origin branchName. There is nothing related to fork here, so what is upstream here? Anyone can help?
did you try - "git push -u origin " ?
@Md.HabiburRahman if you have created a new local branch, search for the git syntax that would push your newly crated branch as well as create a new remote branch at the same time. Also to answer your comment on upstream, there is no upstream in this case because you didn't fork the repo.