Remote Branches¶
When working with remote repositories, you get so-called remote-tracking branches. Remote-tracking branches are references to the state of remote branches. The names of these branches take the form of <remote>/<branch>
, e.g. origin/development
.
You cannot directly manipulate these branches yourself. Git will automatically do this whenever any network communication occurs, in order to make sure they accurately represent the state of the remote repository.
Info
Think of remote-tracking branches as bookmarks: they remind you what the branches in your remote repositories contained the last time you connected to them.
Visual example of remote-tracking branches in a repository:
Manipulating Remotes¶
When you clone a repository, using git clone
, Git will automatically register the location of the remote. However, you can also manually add or remove remotes:
# Initialise an empty repository for illustration purposes
git init
# Manually add a remote 'origin' for the current repository
git remote add origin git@ssh.dev.azure.com:v3/organisation/subdomain/Placeholder
# Manually remove remote 'origin' for the current repository
git remote rm origin
Example¶
Consider the example below:
origin/main
: Remote-tracking branch of remotemain
branch.origin/feature/algorithm
: Remote-tracking branch of remotefeature/algorithm
branch.main
: Local state ofmain
branch.feature/api
: Local state offeature/api
branch.
%%{init: { 'logLevel': 'debug', 'theme': 'base', 'gitGraph': {'showBranches': true, 'showCommitLabel': false, 'mainBranchName': 'origin/main'}} }%%
gitGraph
commit tag: "0"
branch main order: 2
commit tag: "1"
commit tag: "2"
branch feature/api order: 3
commit tag: "3"
checkout origin/main
commit tag: "4"
branch origin/feature/algorithm order: 1
commit tag: "5"
checkout origin/main
commit tag: "6"
The following has happened here:
- The repository is cloned locally from the remote state at commit tag
0
. - Commit tag
1
&2
:- Two commits are made in
main
(local).
- Two commits are made in
- Commit tag
3
:- A new branch
feature_api
is made frommain
and a commit is made to it (local).
- A new branch
- Commit tag
4
:- A collaborating developer has pushed a commit to the remote
main
branch, reflected in remote-tracking branchorigin/main
.
- A collaborating developer has pushed a commit to the remote
- Commit tag
5
:- A collaborating developer pushed a new remote branch
feature/algorithm
(branched frommain
) with a commit in it, as reflected in remote-tracking branchorigin/feature/algorithm
.
- A collaborating developer pushed a new remote branch
- Commit tag
6
:- A collaborating developer pushed another commit to the remote
main
branch, reflected in remote-tracking branchorigin/main
.
- A collaborating developer pushed another commit to the remote
Note
Note that we do not physically have the changes in the remote repository, as made by the collaborating developers, reflected in our local repository until we explicitly retrieve them. This retrieval can be accomplished using git fetch
. After running this command, the remote-tracking branches will be updated with the latest changes from the remote.
Fetching and pushing changes will be covered in the next section.