Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between local branch, local tracking branch, remote branch and remote tracking branch?

I just started using Git and I got really confused between different branches. Can anyone help me to figure out what the following branch types are?

  • local branches
  • local tracking branches
  • remote branches
  • remote tracking branches

What is the difference between them? And how do they work with each other?

A quick demo code will be really helpful I guess.

like image 700
eded Avatar asked Sep 27 '22 04:09

eded


People also ask

What is local and remote tracking branch in git?

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.

What is a local branch?

Local Branch means a place of business which is not a legal entity in its own right and which functions as part of a financial institution and conducts all or some of the transactions inherent in the business of the same; Sample 1. Local Branch .

What is the difference between local and remote git?

Local repositories reside on the computers of team members. In contrast, remote repositories are hosted on a server that is accessible for all team members - most likely on the internet or on a local network.

What's a remote branch?

Git checkout remote branch is a way for a programmer to access the work of a colleague or collaborator for the purpose of review and collaboration. There is no actual command called “git checkout remote branch.” It's just a way of referring to the action of checking out a remote branch.


2 Answers

Here's the long answer.

Remotes:

If you're using Git collaboratively, you'll probably need to sync your commits with other machines or locations. Each machine or location is called a remote, in Git's terminology, and each one may have one or more branches. Most often, you'll just have one, named origin. To list all the remotes, run git remote:

$ git remote
bitbucket
origin

You can see which locations these remote names are shortcuts for, by running git remote -v:

$ git remote -v
bitbucket [email protected]:flimm/example.git (fetch)
bitbucket [email protected]:flimm/example.git (push)
origin [email protected]:Flimm/example.git (fetch)
origin [email protected]:Flimm/example.git (push)

Each remote has a directory under .git/refs/remotes/:

$ ls -F .git/refs/remotes/
bitbucket/ origin/

Branches on your machine:

TLDR: on your local machine, you've got three types of branches: local non-tracking branches, local tracking branches, and remote-tracking branches. On a remote machine, you've just got one type of branch.

1. Local branches

You can view a list of all the local branches on your machine by running git branch:

$ git branch
master
new-feature

Each local branch has a file under .git/refs/heads/:

$ ls -F .git/refs/heads/
master new-feature

There are two types of local branches on your machine: non-tracking local branches, and tracking local branches.

1.1 Non-tracking local branches

Non-tracking local branches are not associated with any other branch. You create one by running git branch <branchname>.

1.2. Tracking local branches

Tracking local branches are associated with another branch, usually a remote-tracking branch. You create one by running git branch --track <branchname> [<start-point>].

You can view which one of your local branches are tracking branches using git branch -vv:

$ git branch -vv
master      b31f87c85 [origin/master] Example commit message
new-feature b760e04ed Another example commit message

From this command's output, you can see that the local branch master is tracking the remote-tracking branch origin/master, and the local branch new-feature is not tracking anything.

Another way to see which branches are tracking branches is by having a look at .git/config.

Tracking local branches are useful. They allow you to run git pull and git push, without specifying which upstream branch to use. If the branch is not set up to track another branch, you'll get an error like this one:

$ git checkout new-feature
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream new-feature <remote>/<branch>

2. Remote-tracking branches (still on your machine)

You can view a list of all the remote-tracking branches on your machine by running git branch -r:

$ git branch -r
bitbucket/master
origin/master
origin/new-branch

Each remote-tracking branch has a file under .git/refs/remotes/<remote>/:

$ tree -F .git/refs/remotes/
.git/refs/remotes/
├── bitbucket/
│   └── master
└── origin/
    ├── master
    └── new-branch

Think of your remote-tracking branches as your local cache for what the remote machines contain. You can update your remote-tracking branches using git fetch, which git pull uses behind the scenes.

Even though all the data for a remote-tracking branch is stored locally on your machine (like a cache), it's still never called a local branch. (At least, I wouldn't call it that!) It's just called a remote-tracking branch.

Branches on a remote machine:

You can view all the remote branches (that is, the branches on the remote machine), by running git remote show <remote>:

$ git remote show origin
* remote origin
  Fetch URL: [email protected]:Flimm/example.git
  Push  URL: [email protected]:Flimm/example.git
  HEAD branch: master
  Remote branches:
    io-socket-ip            new (next fetch will store in remotes/origin)
    master                  tracked
    new-branch              tracked
  Local ref configured for 'git pull':
    master     merges with remote master
    new-branch merges with remote new-branch
  Local ref configured for 'git push':
    master     pushes to master     (up to date)
    new-branch pushes to new-branch (fast-forwardable)

This git remote command queries the remote machine over the network about its branches. It does not update the remote-tracking branches on your local machine, use git fetch or git pull for that.

From the output, you can see all the branches that exist on the remote machine by looking under the heading "Remote branches" (ignore lines marked as "stale").

If you could log in to the remote machine and find the repository in the filesystem, you could have a look at all its branches under refs/heads/.

Cheat sheet:

  • To delete a local branch, whether tracking or non-tracking, safely:

      git branch -d <branchname>
    
  • To delete a local branch, whether tracking or non-tracking, forcefully:

      git branch -D <branchname>
    
  • To delete a remote-tracking branch:

      git branch -rd <remote>/<branchname>
    
  • To create a new local non-tracking branch:

      git branch <branchname> [<start-point>]
    
  • To create a new local tracking branch: (Note that if <start-point> is specified and is a remote-tracking branch like origin/foobar, then the --track flag is automatically included)

      git branch --track <branchname> [<start-point]
    

    Example:

      git branch --track hello-kitty origin/hello-kitty
    
  • To delete a branch on a remote machine:

      git push --delete <remote> <branchname>
    
  • To delete all remote-tracking branches that are stale, that is, where the corresponding branches on the remote machine no longer exist:

      git remote prune <remote>
    

You may have noticed that in some commands, you use <remote>/<branch>, and other commands, <remote> <branch>. Examples: git branch origin/hello-kitty and git push --delete origin hello-kitty.

It may seem arbitrary, but there is a simple way to remember when to use a slash and when to use a space. When you're using a slash, you're referring to a remote-tracking branch on your own machine, whereas when you're using a space, you're actually dealing with a branch on a remote machine over the network.

like image 231
Flimm Avatar answered Oct 23 '22 03:10

Flimm


A local branch is a branch that only you (the local user) can see. It exists only on your local machine.

git branch myNewBranch        # Create local branch named "myNewBranch"

A remote branch is a branch on a remote location (in most cases origin). You can push the newly created local branch myNewBranch to origin. Now other users can track it.

git push -u origin myNewBranch   # Pushes your newly created local branch "myNewBranch"
                                 # to the remote "origin".
                                 # So now a new branch named "myNewBranch" is
                                 # created on the remote machine named "origin"

A remote tracking branch is a local copy of a remote branch. When myNewBranch is pushed to origin using the command above, a remote tracking branch named origin/myNewBranch is created on your machine. This remote tracking branch tracks the remote branch myNewBranch on origin. You can update your remote tracking branch to be in sync with the remote branch using git fetch or git pull.

git pull origin myNewBranch      # Pulls new commits from branch "myNewBranch" 
                                 # on remote "origin" into remote tracking
                                 # branch on your machine "origin/myNewBranch".
                                 # Here "origin/myNewBranch" is your copy of
                                 # "myNewBranch" on "origin"

A local tracking branch is a local branch that is tracking another branch. This is so that you can push/pull commits to/from the other branch. Local tracking branches in most cases track a remote tracking branch. When you push a local branch to origin using the git push command with a -u option (as shown above), you set up the local branch myNewBranch to track the remote tracking branch origin/myNewBranch. This is needed to use git push and git pull without specifying an upstream to push to or pull from.

git checkout myNewBranch      # Switch to myNewBranch
git pull                      # Updates remote tracking branch "origin/myNewBranch"
                              # to be in sync with the remote branch "myNewBranch"
                              # on "origin".
                              # Pulls these new commits from "origin/myNewBranch"
                              # to local branch "myNewBranch which you just switched to.
like image 165
SNce Avatar answered Oct 23 '22 03:10

SNce