Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find what branch the current branch tracks

Tags:

git

I am trying to figure out what branch will be fetched if I do git fetch in the current branch, and how can I change that (by some variant of git remote or editing .git/config file).

From what remote branch git pull fetches the contents? Is it the same from which git fetch fetches the contents? Is there a git command which can show me all this information?

like image 360
Yogeshwer Sharma Avatar asked Aug 03 '11 04:08

Yogeshwer Sharma


1 Answers

To set up the tracked remote branch for a local branch use

git branch --set-upstream <local_branch> <remote_branch>

So, if you want your local master to track origin/master, type

git branch --set-upstream master origin/master

However, git fetch fetches all branches of the configured remote.

If you have multiple remotes (e.g. origin and other),

git fetch other

will fetch the remote other while

git fetch origin

will fetch origin.

To find out which remote branch is being tracked, open .git/config and search for an entry like

[branch "mybranch"]
  remote = <remote_name>
  merge = <remote_branch>

This tells you that your local branch mybranch has <remote_name> as configured remote and that it tracks <remote_branch> on <remote_name>.

Which branches are fetched from a remote and how they are called in your local repo is defined in the following section of .git/config:

[remote "origin"]
  fetch=+refs/heads/*:refs/remotes/origin/*
  url=<url_of_origin>

This tells you that the branches stored under refs/heads of your origin get fetched and get stored under refs/remotes/origin/ in your local repo.

If you are on mybranch and type git fetch, the revisions of <remote_name> (specified in the [remote <remote_name>] section) will be fetched. If you type git pull, after fetching the revisions of <remote_name> the branch <remote_branch> of <remote_name> will be merged into mybranch.

Additional information can be found on the man pages of git branch, git fetch and git pull.

like image 177
eckes Avatar answered Nov 04 '22 16:11

eckes