Git command line showing how to add an upstream remote

How to Use git remote add upstream in Your Workflow

Adding an upstream remote lets you synchronize your local Git repository with the original source project, which is crucial for managing forks collaboratively. The git remote add upstream command specifically connects your fork to the main repository, making it easier to fetch and merge the latest updates. If you contribute to open-source projects or collaborate on shared codebases, mastering this command keeps your work in sync and avoids merge conflicts down the line.

What does git remote add upstream mean?

The command git remote add upstream registers another repository—usually the original repository you forked from—as a remote called “upstream.” In Git, a remote is just a named reference to a repository’s URL. By default, when you clone or fork a repository, your local repo knows about “origin,” which points to your fork or the repository you cloned from. Adding “upstream” provides a second reference, typically for the main project’s repository, so you can fetch updates without overwriting your own changes.

For example, if you fork a popular open-source project on GitHub, your fork’s URL is set as “origin.” The original project is not tracked by your local repo until you add it as “upstream” using git remote add upstream <original-repo-URL>. This setup allows you to pull changes from the main project and keep your local and remote forks updated.

It’s important to note that “upstream” is just a conventional name. You could use any name, but “upstream” helps clarify the relationship between your fork (origin) and the main source (upstream) for most teams and contributors.

When and why should you add an upstream remote?

You should add an upstream remote whenever you fork a repository and plan to stay up-to-date with ongoing changes in the original project. This is especially valuable if you intend to contribute back or want to reduce friction in merging new features, bug fixes, or security updates from the source project into your fork.

In collaborative workflows, having an upstream remote lets you:

  • Fetch the latest commits from the main repository directly into your local environment.
  • Reduce the risk of merge conflicts by keeping your fork up-to-date with the latest features and fixes.
  • Integrate changes efficiently before submitting pull requests, ensuring your contributions are based on the current codebase.
  • Stay aware of new branches, tags, or significant project changes.

Without an upstream remote, you’d need to manually apply patches or re-clone repositories to stay current, adding unnecessary complexity. The git remote add upstream command streamlines this process and is considered best practice in open-source collaboration and team workflows.

How to add an upstream remote in Git

To add an upstream remote, first copy the main repository’s URL (HTTPS or SSH, depending on your setup). Then, make sure you’re inside your local fork’s directory. Run the following command, replacing the URL with the actual path of the original repository:

git remote add upstream https://github.com/ORIGINAL_OWNER/REPO_NAME.git

After running this command, you can confirm the addition by listing all remotes:

git remote -v

You should see output like:

origin    https://github.com/YOUR_USERNAME/REPO_NAME.git (fetch)
origin    https://github.com/YOUR_USERNAME/REPO_NAME.git (push)
upstream  https://github.com/ORIGINAL_OWNER/REPO_NAME.git (fetch)
upstream  https://github.com/ORIGINAL_OWNER/REPO_NAME.git (push)

If the upstream remote already exists with a different URL, you can update it using:

git remote set-url upstream https://github.com/NEW_OWNER/NEW_REPO.git

This flexibility ensures you always have the correct reference to the main repository and can fetch updates without issues.

Working with upstream remotes: Fetch, merge, and pull

After adding an upstream remote, you can integrate upstream changes into your fork with a few key commands:

  • Fetch upstream updates: git fetch upstream downloads new commits, branches, and tags from upstream into your local repo. It doesn’t affect your working branch until you take further action.
  • Merge changes into your branch: To bring updates from the upstream main branch (often called main or master) into your current branch, run git merge upstream/main (substitute main as needed).
  • Pull and merge in one step: git pull upstream main is a shortcut that fetches and merges upstream’s main branch into your checked-out branch.

For example, to sync your fork’s main branch with upstream, you would:

  1. Checkout your main branch: git checkout main
  2. Fetch upstream changes: git fetch upstream
  3. Merge: git merge upstream/main
  4. Push to your fork if needed: git push origin main

These steps help you keep your fork updated, resolve conflicts early, and contribute changes that align with the project’s latest state. The git remote add upstream command is an essential part of any collaborative Git workflow where forks and ongoing development are involved.