Git Best Practices

Written on:July 30, 2014
Comments
Add One

Some questions have come up about using the master branch or a development branch. So I wanted to give my thoughts on the subject.

The master branch is always used for current development of the next release. You can branch off to develop a new feature or fix an issue and then merge that back to master. When we do a major release you create a release branch from master.

Here is a list of commands and tips to help you get started with Git:

Best GUI Tools

http://www.sourcetreeapp.com/

Start working on a new ticket

git checkout master
git pull origin master

git checkout -b TICKET-NUM origin/master

Branch TICKET-NUM set up to track remote branch master from origin.
Switched to a new branch ‘TICKET-NUM’

Making changes to code

Add all changed files to be commited. NOTE: This will add all new files and all changed files. It is best to always keep your directories clean and do not have any extra just files that you don’t want commited within these directories.

git add -A

Verify that you are only about to commit the changes you want. This can also be done by opening SourceTree and clicking on the local branch.

git status

At the end of the day or if you finish the ticket commit your changes locally

git commit

VIM will open, press i and type the Ticket # and the ticket title, then add any additional details about the commit. Press esc and save “:wq”

Making changes the next day

Make sure you are still on your ticket local branch

git status

Rebase any changes from the github repo

git fetch origin
git rebase -i origin/master

pick your commit and delete any others that you don’t want.
If success then you will see something like:

Successfully rebased and updated refs/heads/TICKET-NUM.

else fix any rebase conflicts, then:

git add -A
git rebase --continue

Make changes and updates to the current branch

At the end of the day or if you finish you need to update your commit

git add -A
git commit --amend

This will open VIM and you should see your ticket and information from the last time you
committed. Add any additional details “:wq” and save

Finishing with the ticket

Move to the master branch

git checkout master

Merge your branch into master, this should be a simple fast forward unless you already merged another local branch into master, but since you should be been rebasing every day it should still be a fast forward.

git merge TICKET-NUM

If you have a large team and it took you time to rebase someone else may of already pushed another commit. You can look at SourceTree and see if there are new commits, if so you need to rebase again. You can also just quickly run the code below without checking SourceTree to make sure you got all the current github changes.

git fetch origin
git rebase -i origin/master

If success then you will see something like

Successfully rebased and updated refs/heads/master.

else fix any rebase conflicts, then:

git add -A
git rebase --continue

Push your change to github

git push origin master

Leave a Comment