0
0
GitComparisonBeginner · 4 min read

Origin vs Upstream in Git: Key Differences and Usage

In Git, origin is the default name for the remote repository you clone from, while upstream usually refers to the original repository you forked from. origin points to your copy, and upstream points to the source repository to keep your fork updated.
⚖️

Quick Comparison

Here is a quick table comparing origin and upstream in Git to highlight their main differences.

Factororiginupstream
DefinitionDefault remote for your cloned repoRemote for the original source repo
Common UsePush and pull your own changesFetch updates from the original repo
Set Automatically?Yes, on cloneNo, added manually if forked
Typical URLYour fork or personal repo URLOriginal project repo URL
PurposeManage your copy of the repoKeep your fork synced with source
Command Examplegit push origin maingit fetch upstream
⚖️

Key Differences

origin is the default name Git gives to the remote repository you clone from. When you clone a repo, Git automatically sets origin to point to that remote URL. You use origin to push your changes and pull updates from your own copy of the repository.

upstream is not set automatically. It is a conventional name used when you fork a repository. Your fork is your origin, and the original repository you forked from is called upstream. You add upstream manually to fetch changes from the original source to keep your fork updated.

In summary, origin points to your personal or team remote repo, while upstream points to the original project repo you want to track and sync with. This setup is common in open source workflows where you fork a project, work on your fork (origin), and regularly pull updates from the main project (upstream).

⚖️

Code Comparison

Here is how you work with origin to push and pull changes from your own remote repository.

bash
git clone https://github.com/yourusername/yourrepo.git
cd yourrepo
git push origin main
git pull origin main
Output
Cloning into 'yourrepo'... remote: Counting objects: 10, done. remote: Compressing objects: 100% (5/5), done. Receiving objects: 100% (10/10), done. To https://github.com/yourusername/yourrepo.git abc1234..def5678 main -> main Already up to date.
↔️

Upstream Equivalent

Here is how you add and use upstream to fetch updates from the original repository you forked from.

bash
git remote add upstream https://github.com/originalowner/originalrepo.git
git fetch upstream
git merge upstream/main
Output
Fetching upstream From https://github.com/originalowner/originalrepo * branch main -> FETCH_HEAD Updating abc1234..fedcba9 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
🎯

When to Use Which

Choose origin when: You want to push your changes or pull updates from your own remote repository, such as your fork or team repo.

Choose upstream when: You forked a repository and want to fetch and merge changes from the original source to keep your fork up to date.

This distinction helps you manage your own work separately from the original project and collaborate smoothly.

Key Takeaways

origin is your default remote pointing to your cloned or forked repo.
upstream is a manually added remote pointing to the original source repo.
Use origin to push your changes and pull from your copy.
Use upstream to fetch and merge updates from the original project.
This setup is essential for managing forks and staying synced with the main repository.