5

I had a project where I was the only developer. Then a new developer joined the project and created his developer branch.

I would like to create a separate place and download his branch so I can run his code from that new place to avoid any conflict.

I made a new directory on my local computer. And I am thinking to do this command:

git checkout -b branch-name origin/branch_name

But I know it is kind of wrong because I need to check out the new trunk and branch together I guess? I am confused on what the correct practice is for doing this. Could someone please explain to me how this is commonly done?

Thank you!

DonQuijote
  • 47
  • 8
Genadinik
  • 18,153
  • 63
  • 185
  • 284
  • Has he pushed his branch? – Tim May 27 '14 at 12:40
  • @TimCastelijns yes I think so. I can see the commits. – Genadinik May 27 '14 at 12:43
  • If you can see his commits, you've probably working on the same branch already... – user229044 May 27 '14 at 12:45
  • There is no "checkout trunk and branch together." You either switch to his branch, or you don't. His branch was based off some previous commit in your repository, presumably whatever was HEAD at the time he joined the project. – wadesworld May 27 '14 at 12:46
  • While you *don't need* a separate space for that, you *can* have a second space to check out the separate branch by using [worktrees](https://git-scm.com/docs/git-worktree), but that's optional once you've learned how the general options below work. – Joachim Sauer Apr 21 '22 at 13:06

4 Answers4

6

You don't need the second location, if it's the same repository that the other developer pushed their branch to, you can just fetch from that remote and then git checkout <the developer's branch name> to change the working directory to their code.

It's safest to have a clean git status when you try to check it out.

The process would be:

git fetch remotename
git checkout branchname

If the name matches, git will automatically set it to track the branch that the other developer has pushed.

Leigh
  • 12,038
  • 4
  • 28
  • 36
  • Thank you! By remotename, do you mean the repository name? – Genadinik May 27 '14 at 12:49
  • Yep! So the normal default one is `origin`, but the name is whatever you want it to be. Same with the `branchname`; that's just whatever they called their branch. – Leigh May 27 '14 at 12:52
4

If you are on a clean slate, you can simply pull down the other developer's remote branch without any conflicts.

By a clean slate, I mean you have no files uncommited. If you are in the middle of work, either commit your files or stash them. If you choose to stash them, you can unstash them after switching back to your branch.

Check if you're ready to pull the remote branch by doing a git status.

How to pull a remote branch?

This has been discussed many times. First, find the remote branch you want to pull.

git fetch // updates local index
git branch -a // lists branches
git checkout --track origin/daves_branch

Git fetch remote branch

Community
  • 1
  • 1
Sam P
  • 1,821
  • 13
  • 26
  • *you can simply pull down the other developer's remote branch* I think the question really is how he can do that – Tim May 27 '14 at 12:44
  • @TimCastelijns Yes, I am wondering how to do that. And also I am wondering whether doing it in a new directory will help me avoid any conflicts. Thank you for your help :) – Genadinik May 27 '14 at 12:45
2

When you say "created his developer branch", he has done it only on his local. It might be tracking a remote repository that the world can see but unless he explicitly pushes to it, you will not be able to see his branch.

If he is on similar terms with you, you can ask him to push his branch to the remote:

git push origin <co-worker's branch>

Let's say the name of your co-worker's branch is new_branch

Now you will be able to see the new branch with the same name on origin. Thus, origin/new_branch is now a valid remote branch. You can confirm that by doing a git branch -r that will show all the remote branches and see if origin/new_branch is listed.

You can now create a new branch that will track this remote branch. But remember, you too will be doing this on your local.

git checkout -b my_branch origin/new_branch

So now you have a local branch that is tracking origin/new_branch and which has all the contents that your co-worker had in his local when he pushed to origin.

gravetii
  • 9,273
  • 9
  • 56
  • 75
  • thank you - when I did this command: git checkout -b master origin/new_branch_name I got this error message: fatal: Cannot update paths and switch to branch 'master' at the same time. Did you intend to checkout 'origin/new_branch_name' which can not be resolved as commit? ...did I miss some step? – Genadinik May 27 '14 at 13:41
  • That's because you already have a branch called `master` (created by default). You might want to create a new branch instead that tracks this branch, something like `git checkout -b tracker_branch origin/new_branch_name`. Note that git checkout -b creates a new branch and then makes it your current- `git branch new_branch` followed by `git checkout new_branch` = `git checkout -b new_branch`. – gravetii May 27 '14 at 14:28
0

Though answer has already given here. But below steps helps me. Anyone can follow this if needed.

at first for updating local branch

git fetch

to see all branches list

git branch -a

And finally you can switch to branch

git checkout branch_name
Mimu Saha Tishan
  • 2,402
  • 1
  • 21
  • 40