0

I have two below branches in my remote GitHub repository:

master
test-branch

I did git pull in my test directory but I don't see any code pull in my local directory. Also when I do git branch I don't see any local branch listing here. Not sure why ? But once I do git branch -a, see below remote branches displayed in red:

 remotes/origin/master
 remotes/origin/test-branch

When I do specific branch pull i.e. git pull origin test-branch I see code gets pulled in my test directory but when I do git branch I see below listing:

* master
  remotes/origin/test-branch [displayed in red]

Not sure why it is displaying master here as I pulled test-branch code. Also how I can see which remote branch this master is pointing to?

emilly
  • 10,060
  • 33
  • 97
  • 172
  • If you don‘t have made any changes on this repository local, I would prefer you, to clone the repo new. After cloning you can show all branches with `git branch -a` and then you can `git checkout` the specific branch you want, and execute `git pull` in this branch. – SwissCodeMen May 02 '21 at 08:46
  • For seeing, on which branch master is pointing, you can execute this command on terminal `git log --graph --oneline --all` or you download a Git client, where you can see this pointing very well (I suggest [git-fork](https://git-fork.com/)) – SwissCodeMen May 02 '21 at 08:50
  • When I clone `git clone repo_url` and I do `git branch -a ` I see output as `* master remotes/origin/HEAD -> origin/master remotes/origin/master remotes/origin/test-branch` Now I want to switch to test branch I did `git checkout remotes/origin/test-2May-21` it says you are in detached head state ? – emilly May 02 '21 at 09:17

2 Answers2

1

When you do git pull origin test-branch it will actually pull the changes from origin remote master branch and merge them into the currently checked out branch.

Checkout remote branch locally and setup tracking

git fetch
git checkout --track origin/test-branch

This will basically do the following:

  1. git fetch will update to remote
  2. Create a local branch called test-branch
  3. Setup origin/test-branch as remote tracking branch
  4. Set local branch to origin/test-branch

From the git manual:

-t, --track
When creating a new branch, set up "upstream" configuration. See "--track" in git-branch(1) for details.

If no -b option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping the
initial part up to the "*". This would tell us to use hack as the local branch when branching off of origin/hack (or remotes/origin/hack, or even refs/remotes/origin/hack). If the given name has no slash, or the
above guessing results in an empty name, the guessing is aborted. You can explicitly give a name with -b in such a case.

So this basically means that if your remote branch is called origin/test-branch it will call your locally created branch test-branch.

To show which branches are tracked

git status

will show you in the second line what remote it's tracking if it's tracking

git branch -vv

will show you a list of local branches and which remote they're tracking


Checkout this stackoverflow answer for a more detailed answer.

  • `When you do git pull origin test-branch it will actually pull the changes from origin remote master branch and merge them into the currently checked out branch.` Then what is use of `test-branch` here ? Also there is no currently checked out branch – emilly May 02 '21 at 09:13
  • @emilly I'm not sure what you mean by "what is the use of test-branch here". In the command `git pull origin test-branch` it serves to specify which `origin` branch you want to pull. Also git always has a branch checked out. By default it's the `master` branch (or sometimes `main`, ever since [github's change](https://www.zdnet.com/article/github-to-replace-master-with-main-starting-next-month/). You can check which branch you currently have checked out by running `git status`. – V-Mann_Nick May 02 '21 at 09:18
  • As you said ` In the command git pull origin test-branch it serves to specify which origin branch you want to pull.` so here i have specified `test-branch` so it will pull the changes from remote test-branch not master. Isn't it ? – emilly May 02 '21 at 09:23
  • @emilly Yes, that's correct. It will pull the changes from from `origin/test-branch` and merge them with your currently checked out branch, which probably is `master`. – V-Mann_Nick May 02 '21 at 09:25
  • Once I just do `git pull` after `git init` why I don't see any code in local directory, I see its empty ? I tried `git branch` it does not display any branch ? – emilly May 02 '21 at 09:34
  • @emilly If it's a brand new initialized repository you'll need to [add a remote repository](https://docs.github.com/en/github/getting-started-with-github/managing-remote-repositories) with `git remote add origin `. Now you can do either `git pull origin master` or `git branch -u origin/master` followed by `git pull`. Doing only `git pull origin master` will not setup tracking. – V-Mann_Nick May 02 '21 at 09:42
0

By default, git pull origin xyz runs git fetch origin xyz followed by git merge FETCH_HEAD. It is important to note that the specified remote branch is not checked out but integrated into the local branch.

It displays * master because you actually did not switch to a different branch by running git pull origin xyz. However, the changes of the branch xyz were integrated into your local master branch since a merge was performed after fetching. Probably you want to first switch to the desired branch (git checkout xyz) and then pull the changes (git pull).

You can use git branch -a -vv to show remote and local branches including tracking information as well as the currently checked-out branch:

* xyz                   b7b2f7c [origin/xyz] Commit Message A
  main                  8c4124b [origin/main] Commit Message B
  remotes/origin/HEAD   -> origin/main
  remotes/origin/xyz    b7b2f7c Commit Message A
  remotes/origin/main   8c4124b Commit Message B
Matt
  • 12,848
  • 2
  • 31
  • 53
  • `Probably you want to first switch to the desired branch` before pull I tried to checkout to test-branch with `git checkout test-branch` but it says `error: pathspec 'test-branch' did not match any file(s) known to git` ? – emilly May 02 '21 at 09:31
  • @emilly There are some possible explanations for this: a) the remote is not configured properly b) the branch does not exist on the remote repository c) there is more than 1 remote repository which has a matching branch name (usually a hint is displayed when that is the case). Can you run `git fetch --all` and then add the output of `git branch -vv -a` to your question (commit messages can be removed)? – Matt May 02 '21 at 09:39