Data report"State of code review 2024" is now liveRead the full report

Understanding the git command "git push -u origin"

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


The git push -u origin command is an operation used in Git to upload local repository content to a remote repository. Understanding how to use this command effectively is essential for managing branches and collaborating with others via remote repositories like GitHub, GitLab, or Bitbucket.

The command git push -u origin is used to push changes from your local branch to the corresponding branch on the remote repository. The -u flag stands for --set-upstream, which links your local branch to a remote branch. Configuring the upstream branch is useful for future commands like git pull or git push, which will then default to operate on the linked remote branch without requiring you to specify it each time you run a command.

For more information on what an upstream branch is in Git, see this guide on Git remote branches.

The general syntax for this command is:

Terminal
git push -u origin <branch-name>

Where <branch-name> is the name of your local branch that you want to push.

When you create a new branch locally and want to push it to the remote for the first time, you can use:

Terminal
git push -u origin new-feature

This command pushes the new-feature branch to origin and sets it as the upstream branch, making subsequent pushes or pulls automatically sync with this branch on the remote.

For projects using main as the default branch:

Terminal
git push -u origin main

This pushes the local main branch to the remote repository and sets it as the default branch for future git operations.

If you are working in a detached HEAD state or simply want to push the current branch without specifying its name:

Terminal
git push -u origin HEAD

This pushes the current HEAD (or current branch) to the remote repository and tracks it.

The command git push origin -u followed by a branch name is just a syntactic variation of git push -u origin. Both commands perform the same action; the placement of -u is flexible in the command line.

Some users prefer shorter command variations for convenience:

Terminal
git push -u origin main

The -u option is particularly useful when you are setting up a new branch for collaboration. Once the upstream is set, you can simply use git push or git pull without specifying the remote name or branch, simplifying your Git workflow.

For more information on upstream branches in Git, see the official Git documentation.

Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
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