How to Add Upstream in Git?
Last Updated :
10 May, 2024
When you fork a repository, you create a copy of it under your own account. However, as the original repository updates with new features, bug fixes, and improvements, you'll want to keep your forked repository up-to-date with these changes. This is where adding an upstream remote in Git comes into play. In this article, we'll explore the process of adding an upstream remote to your forked repository, fetching changes from the original repository, and keeping your fork synchronized with the latest updates.
What is upstream?
The upstream repository is the original project repository from which your repository was forked or cloned. It's the official source where the project is maintained and updated. When you fork a repository on platforms like GitHub, GitLab, or Bitbucket, the original repository becomes your upstream repository.
In Git, a remote is a repository hosted on a server, such as GitHub or GitLab. The term "upstream" refers to the original repository that you forked from. By adding an upstream remote, you establish a connection to the original repository, allowing you to track changes and pull updates into your forked repository.
How to Add Upstream in Git?
Adding an upstream repository in Git involves configuring your local repository to recognize and fetch changes from the original project repository. Here's a step-by-step guide to adding an upstream repository:
Step 1: Clone the forked repository
You can clone you forked repository using `git clone` command:
git clone <link_of_your_fork>
for example purpose, lets clone an open source project Turing.jl:
git clone https://github.com/shravanngoswamii/Turing.jl.git
Git CloneStep 2: Navigate to Cloned Repo
Now navigate to the clone of your forked repository using `cd` command:
cd <your_cloned_repo>
After navigating, check the name of remote repository using:
git remote -v
for example,
cd Turing.jl
git remote -v
Navigating to the repoStep 3: Add Upstream Remote
You can see that the name of the clone of your forked repository is `origin`. So now just add the original repository as an upstream using:
git remote add upstream <upstream_repository_URL>
for example,
git remote add upstream https://github.com/TuringLang/Turing.jl.git
Step 4: Verify Upstream Remote
You can verify that the upstream remote has been added correctly by listing the configured remotes using the git remote -v command:
git remote -v
Upstream RemoteAdd Upstream after Cloning the Original Repo
Step 1: Clone the Original Repository
If you haven't already cloned the original repository, start by cloning it to your local machine using the git clone command. Replace <original_repository_url> with the URL of the original repository:
git clone <original_repository_url>
for example,
git clone https://github.com/TuringLang/Turing.jl.git
Step 2: Navigate to Your Cloned Repo
Change directory into your cloned repository. If you've already cloned original repo, navigate to its directory using the cd command:
cd <your_cloned_directory>
for example,
cd Turing.jl
Now the names of remote repositories using:
git remote -v
Step 3: Add Upstream Remote
When you clone the original repository then the name of your original clone repo is `origin`, so just change the name of your cloned repo to `upstream` using:
git remote rename origin upstream
Now you cloned repository is an upstream but you cannot directly push the changes into that if you are working with any project that you do not have write access to so now you have to add your forked repository as an origin using:
git remote add origin <forked_repo_link>
for example,
git remote add origin https://github.com/shravanngoswamii/Turing.jl.git
Step 4: Verify Upstream Remote
You can verify that the upstream remote has been added correctly by listing the configured remotes using the git remote -v command:
git remote -v
How to Add Upstream in Git?
Similar Reads
How to Set Upstream Branch on Git?
It is important to Setup an Upstream Branch in Git to make the workflow smooth and manage branches efficiently. When you want to clone a new repository or work with various feature branches, you need to know how to work with upstream branches and how you can set them up. In this article, we will exp
4 min read
How to add remote origin in git?
Git, most popular version control system, revolutionized the way developers collaborate and manage code. One important feature of Git is remote repositories, which serve as centralized hubs for code collaboration. In this article, we'll explore the process of adding a remote origin to your Git repos
2 min read
How to Use git-blame?
Understanding the history of changes in a codebase is important for effective collaboration and maintenance. One powerful tool in Git for this purpose is git-blame. This command helps developers identify who made specific changes to a file, which can be invaluable for debugging, understanding the ev
6 min read
How to Add Github to Linkedin?
Adding a GitHub profile to LinkedIn is a great way to showcase our coding projects, contributions, and technical skills. This can enhance our LinkedIn profile by providing potential employers and professional connections with direct access to our work. There are multiple ways to add your GitHub to L
2 min read
What is Git-Ignore and How to Use it?
There are various types of files we might want the git to ignore before committing, for example, the files that are to do with our user settings or any utility setting, private files like passwords and API keys. These files are not of any use to anyone else and we do not want to clutter our git. We
5 min read
How to Add Submodule in Git?
Git is a widely used distributed version control and source code management system. It effectively tracks changes to source code, enabling easy branching, merging, and versioning. In this article, we will explore the concept of Git submodules, their benefits, and best practices for integrating them
4 min read
How to Use Git Shell Commands?
Git is a powerful version control system that is important for managing source code in modern software development. While there are many graphical user interfaces (GUIs) available for Git, mastering Git shell commands can significantly enhance your productivity and control over your repositories. Th
3 min read
How to Use 'git remote add origin' Command?
The `git remote add origin` command is used to add a remote repository to your local Git repository. This allows you to push and pull changes between your local and remote repositories. The term "origin" is an alias that refers to the remote repository URL. By convention, "origin" is used for the pr
2 min read
How to Add All Files in Git ?
Adding all files in Git is a common task when you want to stage all changes for committing. Adding all files in Git involves staging all modifications, additions, and deletions in your working directory for the next commit. This process ensures that all changes are included in the commit history. In
3 min read
How to Rename Branch in Git?
Renaming branches in Git is important for keeping your repository organized and understandable, especially when collaborating with others. Clear and descriptive branch names improve project management and help team members identify the purpose of each branch. Renaming branches also ensures consisten
1 min read