Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to look at another developer's branch using git?

Tags:

git

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!

like image 375
Genadinik Avatar asked May 27 '14 12:05

Genadinik


3 Answers

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.

like image 137
Leigh Avatar answered Dec 07 '22 08:12

Leigh


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

like image 45
Sam P Avatar answered Dec 07 '22 09:12

Sam P


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.

like image 29
gravetii Avatar answered Dec 07 '22 09:12

gravetii