1

I wanted to pull code from a different branch other than master i.e there are two branch master and dummy, the master branch is some commit ahead of dummy branch and dummy contain some commits that are not in the master branch so how do I pull these both branch locally as with there different commits.

I have tried first creating dummy branch and pull that from remote but then it contain commits from master branch

git clone repolink

git checkout -b dummy

git pull upstream dummy

I know about cloning with different branch other than master

git clone --branch dummybranch --single-branch repolink

Is there any way to contain both branch at single local repo with different commits?

phd
  • 82,685
  • 13
  • 120
  • 165
  • `git checkout -b ...` does exactly what had been asked for, i.e. it creates a new branch which points directly to the same HEAD as your current one, presumable *master*. That's why you see master's commits there. You need to checkout differently, e.g. `git checkout -b dummy origin/dummy`. – 0andriy Apr 18 '20 at 18:51
  • Does this answer your question? [pull/push from multiple remote locations](https://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations) – phd Apr 18 '20 at 20:23
  • https://stackoverflow.com/search?q=%5Bgit%5D+pull+push+multiple+remotes – phd Apr 18 '20 at 20:23

1 Answers1

6

git clone has already cloned every branch to your repository. But it hasn't created local branches for all of them, just master. The rest are in remote tracking branches which are prefixed with the name of the remote, origin. You can see them with git branch -r.

The remote's master branch is in the remote tracking branch origin/master. Their dummy is in origin/dummy. Your repo looks something like this.

              [origin/master]
A - B - C - D [master]
     \
      E - F [origin/dummy]

Cloning has created a local master branch, but no local dummy.

By default, when you make a new branch it is off of the current branch. If you had master checked out, which you would right after cloning, then git checkout -b dummy make a branch called dummy off of master.

$ git checkout -b dummy

              [dummy]
              [origin/master]
A - B - C - D [master]
     \
      E - F [origin/dummy]

That's not right. We want it off origin/dummy.

After deleting the incorrect dummy, git branch -d dummy, we can checkout origin/dummy and then make the branch.

git checkout origin/dummy
git checkout -b dummy

              [origin/master]
A - B - C - D [master]
     \
      E - F [origin/dummy]
            [dummy]

Or as a shorthand, we can pass in where we want to branch from

git checkout -b dummy origin/dummy

Or, even shorter, you can simply checkout your non-existent dummy branch and Git will assume it's supposed to be the local branch of origin/dummy. This is controlled by the --guess option, on by default.

git checkout dummy
Branch 'dummy' set up to track remote branch 'dummy' from 'origin'.
Switched to a new branch 'dummy'

See Working With Remotes in Pro Git for more.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Not sure if its old or what, but when I check out a branch that has the same name as a remote branch, it automatically links it to the "origin" branch. – Jerry F Oct 30 '20 at 13:38