If you have worked with Git, you’ve probably run into commands like
git push origin main
git push -u origin main`
But what exactly is… origin
? And what does the -u
flag do?
What is origin
?
Whenever you clone a repository, Git will automatically create an alias to that repo. So it bookmarks the origin of that repo, and it happens to be that the default name of this bookmark is called origin
. However it could be called anything you want.
For example. If you clone a repository:
git clone https://github.com/user/project.git
Git will set up a bookmark called origin
which points to
https://github.com/user/project.git
You can verify this yourself by running
git remote --verbose
What does -u
mean?
The -u
flag is short for --set-upstream
. It’s most useful when you’re pushing a branch for the first time.
For example:
git push -u origin main
This command does two things:
- Pushes your local
main
branch toorigin/main
aka it pushes your local branch to the link behind the bookmark calledorigin
. And as we saw before, the link of the bookmark points to your GitHub repository. - It links to your local
main
branch with the remote branchorigin/main
After you’ve set this up, once you can just run.
git pull
git push
Without having to repeat. origin main
Every time.
Why does this matter?
Without -u
You would always need to type out git push origin your-branch-name
.