In Git, what is the difference between origin/master vs origin master?
git
Solution
(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.
Problem
I know, origin is a term for the remote repository and master is the branch there. I am purposely omitting the "context" here and I am hoping that the answer should not depend upon the context. So in git command lines, what is the difference between origin/master and origin master. Is there a non-ambiguous way to understand when to use origin/master and when I should use origin master?