In Git, what is the difference between origin/master vs origin master?
Solution 1:
(Note: When this question was originally posted, "master" was the default name for branches in Git. Since "main" is now the default name, this answer has been updated to use "main", in the hope that this will be more natural for people new to Git.)
There are actually three things here: origin main
is two separate things, and origin/main
is one thing. Three things total.
Two branches:
-
main
is a local branch -
origin/main
is a remote tracking branch (which is a local copy of the branch named "main" on the remote named "origin")
One remote:
-
origin
is a remote
Is origin/main remote?
The origin/main
branch is local! Any time you fetch from origin
, origin/main
will get updated. However, origin/main
can be out of date, and it's even possible that main
no longer exists on origin
. You can use the --prune
option (-p
) with git fetch
to automatically delete remote tracking branches if the branch they track is deleted.
The origin/main
branch is not a reference or pointer to the main
branch on origin
. It is a local copy.
Example: pull in two steps
Since origin/main
is a branch, you can merge it. Here's a pull in two steps:
Step one, fetch main
from the remote origin
. The main
branch on origin
will be fetched and the local copy will be named origin/main
.
git fetch origin main
Then you merge origin/main
into main
.
git merge origin/main
Then you can push your new changes in main
back to origin
:
git push origin main
More examples
You can fetch multiple branches by name...
git fetch origin main stable oldstable
You can merge multiple branches...
git merge origin/main hotfix-2275 hotfix-2276 hotfix-2290
Can you use a different name?
My local branch doesn't have to be named main
if I don't want to. It doesn't have to have the same name as the remote branch! Let's say I want to name my branch alice
, but still have it track origin/main
:
I can do that easily enough:
git checkout -b alice --track origin/main
You can see that the local branch is named alice
, but the remote branch is named main
, and the local copy is origin/main
. This is totally OK! It might be a bit confusing, but maybe you already have a different branch named main
, and you need to switch to a different branch to work on a different change.
Solution 2:
origin/master
is an entity (since it is not a physical branch) representing the state of the master
branch on the remote origin
.
origin master
is the branch master
on the remote origin
.
So we have these:
- origin/master ( A representation or a pointer to the remote branch)
- master - (actual branch)
- <Your_local_branch> (actual branch)
- <Your_local_branch2> (actual branch)
- <Your_local_branch3> (actual branch)
Example (in local branch master
):
git fetch # get current state of remote repository
git merge origin/master # merge state of remote master branch into local branch
git push origin master # push local branch master to remote branch master