For  the first time, after many years of programming here and there with no  real project experience, I took on a project to develop the back end  system of a new platform for a company. For the first time, I’ve had to  work with a front-end developer. Because of this, I’ve decided to  document everything I learn. I had to learn how to collaborate on Git so  I did some research and ended up with the following workflow.

The  following pieces of code require that you have initialized a Git  repository or cloned one. If you haven’t, please read a beginner’s  tutorial on doing so (this one, or this easy one).

Creating Branches

To collaborate with Git on new features, branches are necessary. By default, the branch ‘Master’ is created with every initial Git repository. The branch ‘Master’ should be the only one in sync with the production server. We need to  create another branch to build, fix and test features and bugs. We will  call this branch ‘Dev’ (Develop).

  1. Checkout to the Master branch
    git checkout master
  2. Create a new branch based on the Master branch
    git checkout -b dev master
  3. Switch to the Dev branch
    git checkout dev
  4. Create a feature, bug or anything on top of the Dev branch
    git checkout -b bug1 dev

The most commonly used command after this would be the creation of new branches:

git checkout -b bug1 dev

bug1 is the new sub-branch and dev is the main branch. The included -b flag will create the new branch and switch to it automatically so that you can start creating things immediately.

And switching branches:
git checkout bug1

Merging Branches

After you are satisfied with what you have created, or a bug has been fixed, you will need to merge it to the Dev branch:

Change back to the Dev branch
git checkout dev
Merge the feature or bug branch with Dev
git merge bug1

Now if we want to merge the Dev branch with Master, we first need to go to the Master branch:
git checkout master
Then merge Dev. Be cautious of the --no-ff flag!
git merge --no-ff dev
--no-ff means merge with no fast forward. What that means is that it will  create a commit node which keeps track of who performed each merge.

You can delete a merged local branch with:

git branch -d branchname

If it’s not merged, use:

git branch -D branchname

Sources