We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

What Does git push -u origin main Do?

ThePrimeagen
ThePrimeagenEx-Netflix engineer, NeoVim ricer, and Git rebaser

Last published

Table of Contents

The git push -u origin main command pushes your local main branch to the remote named origin and sets origin/main as its upstream. The push sends commits now; the -u saves the relationship for later.

For course material on pushing branches to GitHub, see the "GitHub" chapter of Learn Git.

What Does git push -u origin main Do?

To push your local main branch's commits to the remote origin's main branch and set its upstream at the same time, run:

git push -u origin main

Each part has one job:

  • git push sends commits and updates a branch on a remote repository.
  • -u is short for --set-upstream.
  • origin is the remote's name.
  • main is the local branch being pushed. Unless you specify a different destination, Git uses the same branch name on the remote.

You need to be authenticated with the remote to push changes.

What Does the -u Flag Do?

The -u flag records origin/main as the upstream branch for your local main. An upstream branch is the remote-tracking branch Git associates with your local branch.

After the first push, Git knows where plain git push and git pull should send and retrieve changes:

git push
git pull

Without an upstream, Git may ask you to specify the remote and branch again.

Do You Need -u Every Time?

No. Use -u the first time you push a new local branch and want to remember its remote counterpart:

git switch -c add-classics
git push -u origin add-classics

Later pushes from add-classics can usually be plain git push. Running git push origin main without -u still pushes the commits; it just doesn't set the upstream as part of that command.

Use git branch -vv to see each local branch and its upstream. To compare sending and receiving changes, read Git Push vs Pull. For the shared GitHub workflow around those commands, see Git and GitHub.

Frequently Asked Questions

What does git push -u origin main do?

It pushes local main to the remote named origin and sets origin/main as the upstream branch for local main.

What does the -u flag do in git push?

The -u flag, short for --set-upstream, saves the remote branch as the upstream for the current local branch.

Do I need to use -u every time I push?

No. Use it when establishing an upstream. Later pushes can usually use git push without a remote or branch name.

Does git push origin main set an upstream?

No. It pushes to origin/main, but it does not set the upstream unless you include -u or --set-upstream.

Related Articles