References:
https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes
https://git-scm.com/book/id/v2/Git-Branching-Remote-Branches
Most Git users are used to working with commands like push
and pull
(and maybe fetch
) which interact with a server - called a remote
in Git. Commonly, this remote is called origin
.
Inside a single Git repo, you can have local branches and remote branches. Local branches are the ones that show up when you run git branch
. Some of these branches (like master
) are probably also on the server, but you can have local branches that you haven’t ever pushed to the server. Likewise, the server might have branches that you haven’t made local copies of.
The branches in the server repo are called “remote branches”, and you can see these with git branch -r
. They will begin with the name of the remote, e.g. you will probably have at minimum origin/master
.
You can checkout remote branches just like you can local branches - e.g. git checkout origin/master
. This will bring you to what the server thinks is master
. However, you can’t directly commit to remote branches. Instead, you can make a local copy of the remote branch with git checkout --track remotename/branchname
to make a local branch branchname
tracking the remote branch. Then you can push (or PR) that branch to the remote branch if you want to have your changes reflected on the remote branch.
However, there’s no reason you can’t have more than one remote. In this case, you could set origin
to be your personal fork, and make a new remote upstream
that contains the official repo. To do this:
git remote set-url origin git@github.com:myusername/myreponame
git remote add upstream https://github.com/keyboardio/reponame
git fetch --all
This last command gets the latest info from all your remotes.
Now you can interact with the upstream
remote just like the origin
remote. In particular, you can checkout upstream branches with git checkout upstream/branchname
. You can make local copies of upstream branches. Etc.
You can even add more remotes. If I wanted to check out the awesome work @algernon is doing in his personal fork of something, just git remote add algernon https://github.com/algernon/reponame
and git fetch algernon
. Then I can check out algernon/master
or any other branch that algernon has published for that repo.
Each local branch can be (optionally) configured as a “remote tracking branch” which means it’s designed to be (roughly) in sync with a particular branch on a particular remote. Your master
branch is probably set up as a remote tracking branch for origin/master
. For remote tracking branches, you can use git push
and git pull
without any other arguments, and Git knows which server and which branch to interact with.
You can set up branches to be remote tracking branches either for any remote you want. git checkout --track remotename/branchname
does this for existing remote branches. Alternately, if you have an existing local branch you want to make a remote branch for (i.e. push to the public repo), you can use git push -u remotename branchname
. After setting a branch up as a remote tracking branch, git push
and git pull
will work on that branch too, without any further arguments.