How to set the remote origin in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


In Git, the term "origin" refers to the default remote repository for your project. Setting up the origin is a crucial step when starting to work with repositories in Git, as it defines the primary upstream repository that you will push to and pull from. This guide will walk you through the steps to set and modify the origin in your Git configuration and explain how to push changes with the correct upstream settings.

If you don’t already have a local repository, create one with:

Terminal
git init

Alternatively, you can clone an existing repository, which will automatically set the origin:

Terminal
git clone <repository-url>

This command clones the repository and sets the remote named 'origin' to the URL provided.

If your repository does not have an origin set, or you need to change the existing origin, use the following commands:

If no remote named 'origin' exists, add it with:

Terminal
git remote add origin <repository-url>

Replace <repository-url> with the URL of your remote repository.

If you need to change an existing origin, use:

Terminal
git remote set-url origin <new-repository-url>

This command updates the URL associated with the remote named 'origin'.

After setting or changing the origin, verify it with:

Terminal
git remote -v

This command lists all remotes configured for your repository, showing the URLs associated with fetch and push operations for each remote.

To push changes to the origin, use:

Terminal
git push origin <branch-name>

Replace <branch-name> with the name of your local branch.

When pushing a new branch or if you want to set the tracking relationship between your local branch and a remote branch, use:

Terminal
git push --set-upstream origin <branch-name>

This command pushes your branch to the remote named 'origin' and sets it to track the remote branch of the same name. Subsequent pushes can be done with just git push.

You can configure the default behavior of git push by setting the push.default configuration. For instance, setting it to simple ensures that Git only pushes the current branch to the corresponding remote branch that 'git pull' would pull from:

Terminal
git config --global push.default simple

By correctly configuring and setting the remote origin in Git, you streamline the workflow of pushing to and pulling from the central repository, making your Git experience more efficient. Remember to regularly check and update your remote settings to align with any changes in the repository URLs or project structure.

For more information see this guide on understanding remotes in Git.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2