devroom.io/content/posts/2009-06-08-best-practice-the-git-development-cycle.md
2019-06-05 14:32:16 +02:00

50 lines
2.3 KiB
Markdown

+++
date = "2009-06-08"
title = "Best Practice - The Git Development Cycle"
tags = ["General", "git", "rebase", "merge", "branch", "svn", "best-practice"]
slug = "best-practice-the-git-development-cycle"
+++
Git is quite an awesome version control system. Why? Because it's lightning fast, even for large projects (among other reasons).
But, how do you use Git effectively for development on a daily basis? Let me explain to you.
<strong>Branches</strong>
With git you normally have a 'master' branch. This is also the branch you use to sync your code with other repositories. That is also the reason why you should never code in the 'master' branch. Always create a new branch and develop your code there.
<pre lang="sh">$ git checkout -b new_feature
# add, commit, repeat</pre>
<strong>Rebase</strong>
Now, while you are working hard on your new feature, other developers complete theirs and push their changes to the remote master branch. When you're done with your project, you need to first get the most recent version of the project's code.
<pre lang="sh">$ git checkout master
$ git pull</pre>
Now, to make merging your new feature easy, you should rebase your new_feature_branch. What this does is add all the commits you just pulled in to your new_feature branch. Any conflicts that arise will happen in your new_feature branch as well, leaving your master branch clean and in order.
<pre lang="sh">$ git checkout new_feature
$ git rebase master</pre>
<strong>Merge</strong>
Now, you have resolved all (if any) conflicts with the current code and your new_feature, you can now merge your new_feature into the project's master branch without any problems.
<pre lang="sh">$ git checkout master
$ git merge new_feature</pre>
This will create a new commit, containing your new_feature. Now is also the time to push your changes to the remote repository.
<pre lang="sh">$ git push origin master</pre>
<strong>What's next?</strong>
More often than not, you'll encounter conflicts when running rebase. That's okay, but you'll need to know how to approach a situation like that. I'll spend another blog post on that topic later.
<strong>Need Git training?</strong>
<em>I'm currently available to provide on-location Git training. Please <a href="/contact">contact me</a> for more info.</em>