Remote references are references (pointers) in your remote repositories, including branches, tags, and so on.You can get a full list of remote references explicitly with
git ls-remote <remote>, or
git remote show <remote> for remote branches as well as more information.Nevertheless, a more common way is to take advantage of remote-tracking branches.
Remote-tracking branches are references to the state of remote branches.They’re local references that you can’t move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository.Think of them as bookmarks, to remind you where the branches in your remote repositories were the last time you connected to them.
Remote-tracking branch names take the form
<remote>/<branch>.For instance, if you wanted to see what the
master branch on your
origin remote looked like as of the last time you communicated with it, you would check the
origin/master branch.If you were working on an issue with a partner and they pushed up an
iss53 branch, you might have your own local
iss53 branch, but the branch on the server would be represented by the remote-tracking branch
This may be a bit confusing, so let’s look at an example.Let’s say you have a Git server on your network at
git.ourcompany.com.If you clone from this, Git’s
clone command automatically names it
origin for you, pulls down all its data, creates a pointer to where its
master branch is, and names it
origin/master locally.Git also gives you your own local
master branch starting at the same place as origin’s
master branch, so you have something to work from.
“origin” is not special
Just like the branch name “master” does not have any special meaning in Git, neither does “origin”.While “master” is the default name for a starting branch when you run
Figure 30. Server and local repositories after cloning
If you do some work on your local
master branch, and, in the meantime, someone else pushes to
git.ourcompany.com and updates its
master branch, then your histories move forward differently.Also, as long as you stay out of contact with your
origin server, your
origin/master pointer doesn’t move.
Figure 31. Local and remote work can diverge
To synchronize your work with a given remote, you run a
git fetch <remote> command (in our case,
git fetch origin).This command looks up which server “origin” is (in this case, it’s
git.ourcompany.com), fetches any data from it that you don’t yet have, and updates your local database, moving your
origin/master pointer to its new, more up-to-date position.
git fetch updates your remote-tracking branches
To demonstrate having multiple remote servers and what remote branches for those remote projects look like, let’s assume you have another internal Git server that is used only for development by one of your sprint teams.This server is at
git.team1.ourcompany.com.You can add it as a new remote reference to the project you’re currently working on by running the
git remote add command as we covered in Git Basics.Name this remote
teamone, which will be your shortname for that whole URL.
Figure 33. Adding another server as a remote
Now, you can run
git fetch teamone to fetch everything the remote
teamone server has that you don’t have yet.Because that server has a subset of the data your
origin server has right now, Git fetches no data but sets a remote-tracking branch called
teamone/master to point to the commit that
teamone has as its
Figure 34. Remote-tracking branch for
When you want to share a branch with the world, you need to push it up to a remote to which you have write access.Your local branches aren’t automatically synchronized to the remotes you write to — you have to explicitly push the branches you want to share.That way, you can use private branches for work you don’t want to share, and push up only the topic branches you want to collaborate on.
If you have a branch named
serverfix that you want to work on with others, you can push it up the same way you pushed your first branch.Run
git push <remote> <branch>:
$ git push origin serverfixCounting objects: 24, done.Delta compression using up to 8 threads.Compressing objects: 100% (15/15), done.Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done.Total 24 (delta 2), reused 0 (delta 0)To https://github.com/schacon/simplegit * [new branch] serverfix -> serverfix
This is a bit of a shortcut.Git automatically expands the
serverfix branchname out to
refs/heads/serverfix:refs/heads/serverfix, which means, “Take my
serverfix local branch and push it to update the remote’s
serverfix branch.”We’ll go over the
refs/heads/ part in detail in Git Internals, but you can generally leave it off.You can also do
git push origin serverfix:serverfix, which does the same thing — it says, “Take my serverfix and make it the remote’s serverfix.”You can use this format to push a local branch into a remote branch that is named differently.If you didn’t want it to be called
serverfix on the remote, you could instead run
git push origin serverfix:awesomebranch to push your local
serverfix branch to the
awesomebranch branch on the remote project.
Don’t type your password every time
If you’re using an HTTPS URL to push over, the Git server will ask you for your username and password for authentication.By default it will prompt you on the terminal for this information so the server can tell if you’re allowed to push.
If you don’t want to type it every single time you push, you can set up a “credential cache”.The simplest is just to keep it in memory for a few minutes, which you can easily set up by running
For more information on the various credential caching options available, see Credential Storage.
The next time one of your collaborators fetches from the server, they will get a reference to where the server’s version of
serverfix is under the remote branch
$ git fetch originremote: Counting objects: 7, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 3 (delta 0)Unpacking objects: 100% (3/3), done.From https://github.com/schacon/simplegit * [new branch] serverfix -> origin/serverfix
It’s important to note that when you do a fetch that brings down new remote-tracking branches, you don’t automatically have local, editable copies of them.In other words, in this case, you don’t have a new
serverfix branch — you have only an
origin/serverfix pointer that you can’t modify.
To merge this work into your current working branch, you can run
git merge origin/serverfix.If you want your own
serverfix branch that you can work on, you can base it off your remote-tracking branch:
$ git checkout -b serverfix origin/serverfixBranch serverfix set up to track remote branch serverfix from origin.Switched to a new branch 'serverfix'
This gives you a local branch that you can work on that starts where
Checking out a local branch from a remote-tracking branch automatically creates what is called a “tracking branch” (and the branch it tracks is called an “upstream branch”).Tracking branches are local branches that have a direct relationship to a remote branch.If you’re on a tracking branch and type
git pull, Git automatically knows which server to fetch from and which branch to merge in.
When you clone a repository, it generally automatically creates a
master branch that tracks
origin/master.However, you can set up other tracking branches if you wish — ones that track branches on other remotes, or don’t track the
master branch.The simple case is the example you just saw, running
git checkout -b <branch> <remote>/<branch>.This is a common enough operation that Git provides the
$ git checkout --track origin/serverfixBranch serverfix set up to track remote branch serverfix from origin.Switched to a new branch 'serverfix'
In fact, this is so common that there’s even a shortcut for that shortcut.If the branch name you’re trying to checkout (a) doesn’t exist and (b) exactly matches a name on only one remote, Git will create a tracking branch for you:
$ git checkout serverfixBranch serverfix set up to track remote branch serverfix from origin.Switched to a new branch 'serverfix'
To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:
$ git checkout -b sf origin/serverfixBranch sf set up to track remote branch serverfix from origin.Switched to a new branch 'sf'
Now, your local branch
sf will automatically pull from
If you already have a local branch and want to set it to a remote branch you just pulled down, or want to change the upstream branch you’re tracking, you can use the
--set-upstream-to option to
git branch to explicitly set it at any time.
$ git branch -u origin/serverfixBranch serverfix set up to track remote branch serverfix from origin.
When you have a tracking branch set up, you can reference its upstream branch with the
If you want to see what tracking branches you have set up, you can use the
-vv option to
git branch.This will list out your local branches with more information including what each branch is tracking and if your local branch is ahead, behind or both.
$ git branch -vv iss53 7e424c3 [origin/iss53: ahead 2] Add forgotten brackets master 1ae2a45 [origin/master] Deploy index fix* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] This should do it testing 5ea463a Try something new
So here we can see that our
iss53 branch is tracking
origin/iss53 and is “ahead” by two, meaning that we have two commits locally that are not pushed to the server.We can also see that our
master branch is tracking
origin/master and is up to date.Next we can see that our
serverfix branch is tracking the
server-fix-good branch on our
teamone server and is ahead by three and behind by one, meaning that there is one commit on the server we haven’t merged in yet and three commits locally that we haven’t pushed.Finally we can see that our
testing branch is not tracking any remote branch.
It’s important to note that these numbers are only since the last time you fetched from each server.This command does not reach out to the servers, it’s telling you about what it has cached from these servers locally.If you want totally up to date ahead and behind numbers, you’ll need to fetch from all your remotes right before running this.You could do that like this:
$ git fetch --all; git branch -vv
git fetch command will fetch all the changes on the server that you don’t have yet, it will not modify your working directory at all.It will simply get the data for you and let you merge it yourself.However, there is a command called
git pull which is essentially a
git fetch immediately followed by a
git merge in most cases.If you have a tracking branch set up as demonstrated in the last section, either by explicitly setting it or by having it created for you by the
git pull will look up what server and branch your current branch is tracking, fetch from that server and then try to merge in that remote branch.
Generally it’s better to simply use the
merge commands explicitly as the magic of
git pull can often be confusing.
Deleting Remote Branches
Suppose you’re done with a remote branch — say you and your collaborators are finished with a feature and have merged it into your remote’s
master branch (or whatever branch your stable codeline is in).You can delete a remote branch using the
--delete option to
git push.If you want to delete your
serverfix branch from the server, you run the following:
$ git push origin --delete serverfixTo https://github.com/schacon/simplegit - [deleted] serverfix
Basically all this does is to remove the pointer from the server.The Git server will generally keep the data there for a while until a garbage collection runs, so if it was accidentally deleted, it’s often easy to recover.
prev | next
To completely remove a remote branch, you need to use the git push origin command with a -d flag, then specify the name of the remote branch. So the syntax representing the command for removing a remote branch looks like this: git push origin -d branch-name .How to get all commits on remote branch? ›
- Fetch branches. We use the git fetch [remote-name] command to fetch all the branches, commits and files of the remote connection. ...
- View remote branches. To view the remote branches that was fetched we use the git branch -r command. ...
- Fetch and Merge.
- Issue the git push origin –delete branch-name command, or use the vendor's online UI to perform a branch deletion.
- After the remote branch is deleted, then delete the remote tracking branch with the git fetch origin –prune command.
- git branch | grep -v "main" | xargs git branch -D.
- git branch | grep -v " main$" | xargs git branch -D.
- $ cd "C:\Users\nazma\Git\Test_12"
- $ git branch -a.
- $ git remote -v.
- $ git remote update origin --prune.
- $ git branch -a.
There is a command that gives you about all tracking branches. And to know about the pull and push configuration per branch you can use the command git remote show origin. and you can use -sb option for seeing the upstream. Hope this information will help you to find which branch is tracking.How do I see all branches in repository? ›
On GitHub.com, navigate to the main page of the repository. From the file tree view on the left, select the branch dropdown menu, then click View all branches.How do I remove old commits from remote branch? ›
To delete commits from remote, you can use the git reset command if your commits are consecutive from the top or an interactive rebase otherwise. After you delete the commits locally, push those changes to the remote using the git push command with the force option.How do I pull latest changes from remote branch? ›
You may need to fetch this remote branch into your project. You can do this with the git fetch command. The git fetch command goes out to your remote project and pulls down all the data from that remote project that you don't have yet.How do I clone all remote branches? ›
- Step 1: Clone the Remote Repository. ...
- Step 2: Navigate to the Cloned Repository. ...
- Step 3: Fetch All Remote Branches. ...
- Step 4: Create Local Branches for Each Remote Branch. ...
- Step 5: Verify the Cloned Branches.
- Instead of using the git branch command that you use for local branches, you can delete a remote branch with the git push command.
- Then you specify the name of the remote, which in most cases is origin .
- -d is the flag for deleting, an alias for --delete .
The key difference between git fetch and pull is that git pull copies changes from a remote repository directly into your working directory, while git fetch does not. The git fetch command only copies changes into your local Git repo.Can I recover a deleted remote branch in git? ›
There is no retention policy on deleted branches. A deleted Git branch can be restored at any time, regardless of when it was deleted.How do you clean remote branches in source tree? ›
- Check the “Fetch from all remotes” box.
- Check the “Prune tracking branches no longer on remote(s)” box <- this is the key option.
To do that, you use the following command: git push <remote_name> --delete <branch_name>. The branch still exists locally, though. Since it has unmerged changes, you can delete it quickly using git branch -D hotfix after switching back to main.Which can you use to delete a remote branch? ›
Deleting remote branches
To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete.
Prune Obsolete Remote Branches
You can prune by checking the corresponding option in the Fetch dialog. Alternatively, you can right-click the remote in the sidebar and choose Prune Obsolete Remote Branches from the contextual menu.