<h2 id="my-git-workflow">My git workflow</h2>
<p>In this episode of HPR I present the workflow I use to contribute to opensource projects using git. I have no idea if this workflow is something that is commonly used, but it is working for me, so I thought I’d share it with the HPR community.</p>
<p>The first thing I do is fork the project I want to contribute to. This is done on github most of the time, although this workflow can work on gitlab, bitbucket, or even some self hosted git platform.</p>
<p>Once the project is forked, I clone it on my machine :</p>
<pre><code>$ git clone git://server/path/to/myproject.git</code></pre>
<p>Git automatically names my remote project <code>origin</code>.</p>
<p>Then I add a reference to the original project :</p>
<pre><code>$ git remote add upstream https://server/path/to/originalproject.git</code></pre>
<p>Now my local repository references my fork under the name <code>origin</code> and the original project under the name <code>upstream</code>.</p>
<p>In this workflow, I never work on the master branch. So, when I need to fix a bug for example, I create a new branch :</p>
<pre><code>$ git checkout -b bugfix</code></pre>
<p>I can then make changes, test my code, make sure everything is ok, stage and commit my changes :</p>
<pre><code>$ git add .
$ git commit -m &quot;commit message&quot;</code></pre>
<p>Now I need to push this local branch to my repository on github :</p>
<pre><code>$ git push -u origin bugfix</code></pre>
<p>Since I forked the original project, github knows that <code>origin</code> and <code>upstream</code> are linked. If there are no conflicts, github will show me a big green button to create a pull request. Once the pull request is created, I just have to wait for the maintainer to merge it in <code>upstream</code>’s master branch. Then, I need to sync both my local copy and my fork on github with the original project. In order to do that, on my local copy, I checkout my master branch, fetch <code>upstream</code>’s changes, and merge them :</p>
<pre><code>$ git checkout master
$ git fetch upstream
$ git merge upstream/master</code></pre>
<p>Now my local master branch is ahead of <code>origin</code>’s master branch, so I push those changes to github :</p>
<pre><code>$ git push</code></pre>
<p>I don’t need the <code>bugfix</code> branches (the local one and the github one), so I can delete those :</p>
<pre><code>$ git branch -d bugfix
$ git push origin -d bugfix</code></pre>
<p>And now, my local repository is even with both <code>origin</code> and <code>upstream</code>, and I can start again.</p>
<p>To summarize, here’s the complete workflow :</p>
<pre><code>$ git checkout -b myawesomefeature
$ git add .
$ git commit -m &quot;Awesome commit message&quot;
$ git push -u origin myawesomefeature</code></pre>
<p>Create a pull request, wait for the maintainer to merge it.</p>
<pre><code>$ git checkout master
$ git fetch upstream
$ git merge upstream/master
$ git push
$ git branch -d myawesomefeature
$ git push origin -d myawesomefeature</code></pre>
