Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Basic Guide to Branching With Git

DZone's Guide to

Basic Guide to Branching With Git

A quick how-to for those who have resolved to start working with Git in the new year.

· Agile Zone ·
Free Resource

Adopting a DevOps practice starts with understanding where you are in the implementation journey. Download the DevOps Transformation Roadmap. Brought to you in partnership with Techtown.

This is the second quick guide I wrote for the devs at work. Here, I cover the simple stuff about branching with Git. The idea of a branch is to provide separation between experimentation and production code. You’ve already seen that the main branch Git uses is called the master branch.

Your workflow should look something like this:

  1. Write code and commit to a feature branch. While it is possible to push those branches to a remote SCM host (like Bitbucket), it's not a good idea. The remote should only have production-ready code.
  2. When you have tested your code and the feature is complete, you would then merge your branch with master and push it.
  3. It might be that you are working on several branches at once, but the procedure is the same. Only merge on to the master branch when you have tested your code and are happy it is production-ready.

To create a new branch from the command line:

git branch my-new-feature

Git will not switch you to the new branch. You have to do that yourself with:

git checkout my-new-feature

It is possible to create a new branch and check it out all in one go:

git checkout -b my-new-feature

If you want a branch to be tracked by the remote SCM (as I said, generally we won’t but you never know… ):

git branch –track my-new-feature

Viewing Branches

You can see what branches you have like this:

git branch

That’ll show you all your local branches. If you add -r you’ll be shown only the remote branches. Adding -a will show you all the branches.

A useful trick is to be able to see which branches have been merged into your current branch. You do this:

git branch --merged

And for completeness, here’s how you see branches that have not been merged:

git branch --no-merged

Finally…

That’s the basics of branching with Git. In the next post, I’ll cover some of the more advanced functionality.

Take Agile to the next level with DevOps. Learn practical tools and techniques in the three-day DevOps Implementation Boot Camp. Brought to you in partnership with Techtown.

Topics:
git ,git flow ,developement

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}