'How to look at another developer's branch using 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!

git


Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leigh
Solution 2 Community
Solution 3 gravetii
Solution 4 Mimu Saha Tishan