Git branch

I want to create a new functionality, but this new functionality may be different what other developers are developing and may damage the development of other developers.

What is the best thing to do when you are working with git? Creating a branch?
I want to create a git branch. That is easy to do with `git branch [NAME]` .

But if I do git push origin than I push to the Master right? So I should than do git push origin ./[NAME] right?

Another question: why would one use checkout over a branch?
I want to create a new functionality, but this new functionality may be different what other developers are developing and may damage the development of other developers.

What is the best thing to do when you are working with git? Creating a branch?
I want to create a git branch. That is easy to do with `git branch [NAME]` .

The first thing to do is discuss it with your fellow developers on the project. They're not going to want any nasty surprises, and they may well have some good advice on how to make the changes you want in such a way that it's less disruptive to the work they're doing. Communication is important!

Secondly, when making changes, you should make them in your own branch. You should create a branch for the work you're doing - this is commonly called a "feature branch". Then, when your work is complete, the work on that branch can get merged with whatever the main development branch is, following whatever review procedures are in place for that project.

Having said that, different projects use git in different ways. The single most important thing here is to find out what practises and procedures the project is already using. Understand how the other developers on the project want you to use git, and do the same.

But if I do git push origin than I push to the Master right? So I should than do git push origin ./[NAME] right?

When you push to the upstream repository, you specify which branch on the repository you push to. To make this easier, you can specify in your own working copy, which remote branch your local branch tracks, so that when ever you pull or push, it does it from/to that remote branch.

You can set this up the first time you push, using the --set-upstream option:

git push --set-upstream origin <branch-name>

Typically, you'd want to use the same branch name locally and remotely. A shortcut for doing this is:

git push --set-upstream origin HEAD

HEAD is an alias for the branch you currently have checked out, so if you have your feature branch checked out, it will set up that branch to push to a branch on origin with exactly the same name.

But, again, this project may have it's own way of doing things. Find out how your fellow developers want you to use git.

Another question: why would one use checkout over a branch?


git branch is used for manupulating branches - e.g. creating them, renaming them, deleting them.

git checkout is what you use to change your working copy to be the versions that are in a particular branch. In other words, it's what you use to change the branch you're working on at the moment. It will change the contents of the files in your working copy to match that branch. (Don't worry, it won't overwrite any changes you've made and not committed. There are all sorts of safety checks built in.) The branch you have checked out is the one you're working in - when you commit changes, they will get committed to the branch you have currently checked out.

So, using git branch will create the feature branch for you, but if you actually want to work in that branch, you use checkout.
Last edited on
Topic archived. No new replies allowed.