1

I have a situation where I didn't understand the updation how it happens internally.

I have 2 remote branches:

master
dev

Now, I want to create 3 local branches out of dev.

Command 1:

git checkout dev

command 2:

git checkout -b "Us1DevBranch" origin/dev

command 3:

git checkout -b "Us2DevBranch" origin/dev

When a git branch command is executed I have,

dev
Us1DevBranch
Us2DevBranch

Scenario:- After 5 days I picked a branch Us1DevBranch and starting adding some files into this branch. In these 5 days, lets suppose 5 modifications were made to the remote dev branch and I wanted to take an update.

Now when I did, git pull origin dev it affected only my local dev branch. The other 2 branches Us1DevBranch & Us2DevBranch didn't get updated.

Questions:-

1) What I read in the documentation and other discussion forum is that, git pull internally does a git fetch and then a git merge to the local branches created out of that branch. But then why the other 2 branches didn't get updated!

2) I was under the impression that checking out a branch with -b option is just to give a different local name to the branch & everything else is same. But there is something much more than that, as when I did git pull, only the branch created out of the option git checkout dev got affected, not the other branches! Why is it so?

bahrep
  • 29,961
  • 12
  • 103
  • 150
Deca
  • 1,155
  • 1
  • 10
  • 19
  • You might have gotten a message when next you checked out `Us1DevBranch`: "Your branch and 'origin/dev' have diverged, and have 1 and 5 different commits, respectively." When you see that it means you need to take some action to update the local branch (usually a `merge` or a `rebase`). – trent Jul 18 '16 at 15:24
  • True..but my question is git pull internally does fetch+merge...so the merge operation is implicitly happening...hence I thought it would update this brnch as well.. – Deca Jul 18 '16 at 16:33
  • `git pull` only merges into the *current branch*. Other branches (i.e., branches that are not currently checked out) cannot be merged because you can only resolve merge conflicts in the current working tree. – trent Jul 18 '16 at 16:37
  • @trentcl , got it....I didn't understnd wat 'git pull --all' does,,,, I mean if say dere are 3 brnches out of dev,,, dev1, dev2 & dev3. If I m inside dev3 and do either "git pull origin dev" or "git pull --all"...It affects only dev3. Other 2 branches r not updated yet even for git pull --all....then wat is the use of --all – Deca Jul 18 '16 at 17:47
  • `--all` says to fetch from all remotes. It's analogous to (I think) `git fetch --all; git merge origin/dev`. `git pull` really doesn't do much. You might find it informative to read the answers and comments on http://stackoverflow.com/questions/292357/what-are-the-differences-between-git-pull-and-git-fetch?rq=1 – trent Jul 18 '16 at 19:23

3 Answers3

3

1) When you create a new branch, it's completely distinct from that point in time. You'll have to manually update all those branches as well.

2) if you just want to give it a different name and keep everything else the same:

git branch -m newbranchname
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • (1) but dev got updated when I did a pull. And what does manually update mean, I thought git pull(git fetch + git merge) is the updation that happens to the local branch (2) What is the difference in the 2 syntax – Deca Jul 18 '16 at 12:59
2

the following command:

git checkout dev

just doing one job: It changes your current branch to dev; It does not create any branch named dev! you can do git checkout dev because you already have it there in your local branch as a result of cloning your remote branch which has a dev branch.

BUT

git checkout -b "Us1DevBranch" origin/dev

does three jobs:

  1. It creates a branch, named Us1DevBranch
  2. It checks out to that branch (i.e. changes your current branch to newly created Us1DevBranch branch
  3. It setup this local branch to track remote origin/dev branch (i.e. every time you are on this branch and do git fetch, git pull, git push it will update against remote origin/dev branch). Without this last arg you need to specify remote and branch every time you git fetch, git pull, git push i.e git pull origin dev to update your local branch with remote dev for example.

So git checkout -b "Us1DevBranch" is equal to git branch "Us1DevBranch" + git checkout "Us1DevBranch" and the last parameter of your command origin/dev will setup tracking for this newly created branch.

Finally you can manually setup tracking for your already existing branches like dev through:

git branch -u origin/dev dev

and there is no need to setup tracking for master because it will automatically sets up to track origin/master as a result of cloning.

You can always check tracking information of branches (i.e My local branches track which remote brach?) by the following command:

git branch -vv
dNitro
  • 5,145
  • 2
  • 20
  • 45
  • Ya u r rite, I am able to do 'git checkout dev', because iy came as parT of my cloning. My Bad! But I have 1 question still.. When I am in the branch 'Us1DevBranch' and I run the command, 'git pull origin dev', it didn't update the current branch Us1DevBranch, only the dev local got affected,, not the Us1DevBranch even though this branch was created out of Dev.. – Deca Jul 18 '16 at 14:55
  • That the *Us1DevBranch* created out of *Dev* branch does not mean anything speciall to their relations with remote branches. *Us1DevBranch* simply contains all the commits of dev branch to this point of divergence. when you run `git pull origin dev` in *Us1DevBranch* branch you simply tell git that apply all changes from *remote dev branch* to this branch that i am currently on and it should not affect local dev atall. have a look at `git log --oneline --graph --decorate --all` and see what you can found out of it. – dNitro Jul 18 '16 at 16:10
1

Aswering the title question. The '-b' option passed to the git checkout is to create a new branch. It's used when you want to create and checkout to the new branch in one line.

Now if you want to fetch/pull all your branches, you could use git fetch --all or git remote update, this are equivalent. Or even git push --all.

J.Adler
  • 1,303
  • 11
  • 19