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/..
).
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/..
).
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 forkeddownstream
” and “upstream
”" for more on upstream
term)origin
is your fork: your own repo on GitHub, clone of the original repo of GitHubFrom 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 namedupstream
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.
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.
In the context of GitHub, "origin" and "upstream" refer to two different repositories.
"Origin" typically refers to your own fork of a repository. When you fork a repository on GitHub, you create a copy of it in your own account. This copy is called a fork, and the original repository is called the upstream repository. When you clone your fork to your local machine, Git automatically sets up a remote called "origin" that points to your fork on GitHub.
"Upstream" refers to the original repository that you forked from. This is the repository that you originally copied when you created your fork. You can set up a remote called "upstream" that points to this repository, which allows you to keep your fork up to date with any changes that are made to the upstream repository.
"origin" refers to your own fork of a repository, while "upstream" refers to the original repository that you forked from.
Let's say you want to contribute to an open-source project on GitHub called "example-project" that is owned by another user. To contribute to this project, you would typically fork the repository to your own account, clone it to your local machine, make changes, and then submit a pull request to the original repository.
Here's how the "origin" and "upstream" repositories come into play:
Fork the repository: You go to the "example-project" repository on GitHub and click the "Fork" button. This creates a copy of the repository in your own account, which is now called "your-username/example-project".
Clone the repository: You clone your fork of the repository to your local machine using the git clone command. This sets up a local copy of the repository on your machine.
Set up "origin" remote: When you clone your fork, Git automatically sets up a remote called "origin" that points to your fork on GitHub. This allows you to push changes to your fork using the git push command.
Set up "upstream" remote: To keep your fork up to date with any changes that are made to the original repository, you can set up a remote called "upstream" that points to the original repository. You can do this using the git remote add command. For example:
git remote add upstream https://github.com/original-user/example-project.git
This sets up a remote called "upstream" that points to the original repository.
Fetch changes from "upstream": To get any changes that have been made to the original repository, you can run the git fetch command with the "upstream" remote. For example:
git fetch upstream
This fetches any changes that have been made to the original repository.
Merge changes into your fork: Once you've fetched changes from the original repository, you can merge them into your fork using the git merge command. For example:
git merge upstream/main
This merges any changes that have been made to the "main" branch of the original repository into your local copy of the repository.
Push changes to "origin": Once you've made changes to your local copy of the repository, you can push them to your fork on GitHub using the git push
command. For example:
git push origin main
This pushes any changes that you've made to the "main" branch of your fork to your fork on GitHub.
In summary, "origin" refers to your own fork of the repository, while "upstream" refers to the original repository that you forked from. By setting up "upstream" as a remote, you can keep your fork up to date with any changes that are made to the original repository.