Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

GonG Panupong
GonG Panupong
14,374 Points

Please help me clear my confusion on Git remote & branch

I am very confuse on Git remote and branch, let's me tells what I understand right now.

From my understanding, Git remote is the branch on the server (e.g. Github, Bitbucket) and Git branch is the local branch on my computer.

Therefore, if the server contains master branch and another branch (let's name it branchOne), then when I clone this repo into my local computer, it will be a local master branch in my computer.

And if I want to push my local master branch onto the branchOne on the server, I have to add remote branchOne on my local master branch first.

Is this correct? If not please correct my understanding.

Thank you for your help, if my question confuse you, I'm very sorry.

2 Answers

Git is tricky. Unlike most version control systems, it doesn't have a centralised server. Instead, the copy on GitHub is identical to the copy on your computer (if they're in sync).

So, that's what a remote is. It's a copy of your repository that's located on a computer that's not your own, but is accessible over the network. When you add a remote to a repository, you're letting Git know where that remote repository is by giving it a link (git remote add nameOfRemote http://address.to.remote/someProject.git).

A branch is a version of your project. Every project most likely has a master branch – it's the main branch. You can use other branches for other versions of the projects, or to create new features without messing in the main codebase.

As remotes are just copies of your local repository, remotes can have branches just like your local repository can have branches. And those branches usually mirror the ones you have in your local repository.

When you do a git push or a git pull git assumes you're pushing to or pulling from the remote called origin and its master branch.

But nothing is stoping you from telling it what remote to use and what branch to use: git push remoteName branchName.

Open up any project on GitHub, and you'll see there's a dropdown menu for changing branches. See? A branch is not just a local thing, a remote repository can have it too (and must have at least one).

GonG Panupong
GonG Panupong
14,374 Points

Thanks for your help. After played with it (I used Bitbucket), seems like I missed some points but still not understand it clear. After I cloned the project from the server, it seems like all branches that existed in the Bitbucket came to my local computer.

Inside this repo, the remote is named origin. After some works, push it back with command git push origin branchName. Looks like I can only push it according to the branchName.

For example, if I done some works in branchOne branch, and I run command git push origin master because I want to change the master branch, it says that everything is up to date. This may be because I didn't change anything in master branch and it the same as the one in the server. Therefore, I need to run git push origin branchOne, then it successed change the branchOne in the server.

If this still not correct, please guide me the right direction. Thanks you very much