Git and Github Guidelines
Contents
- Essentials
- Workflow Overview
- Issues & Milestones
- Pull Requests
- Keeping up to date
- Collaborating on a feature
- Example Flow
Essentials
A short overview of the git guidelines.
Branches and namings
master
- production should be always be deployed from master
- no direct commits should be made to it - always a PR from
develop
branch
develop
- the current stable version of all the completed features
- no direct commits should be made to it - always a PR from a
feature
branch
feature
- you create this when starting on a new feature
- may branch of
develop
- must merge back to
develop
- if you are creating a feature for a separate branch that represents a whole new set of features, for example
v2
branch, orapril-sprint
branch you name the feature branchfeature-april-sprint/<name-of-the-feature>
. feature-<branch-name>/<name-of-the-feature>
for nondevelop
branchesfeature/<name-of-the-feature>
fordevelop
branches
bugfix
orhotfix
- may branch off
master
- their goal is to fix something that is not working on production.
- must be merged to
master
anddevelop
branch. bugfix/<name-of-the-bug-or-fix>
- may branch off
Commit messages
Make multiple commits and name them according to the content of the commit. Each commit is made out of a Summary and a Description.
- We recommend using gitmoji(an emoji guide for commit messages), as it simplifies commit messages and they become more readable.
- Using emojis on commit messages provides an easy way of identifying the purpose or intention of a commit with only looking at the emojis used.
Summary
- Max 50 characters
- Don’t end with a period
- Write in imperative mode, as if you were commanding someone. Start the line with “Fix”, “Add”, “Change” instead of “Fixed”, “Added”, “Changed”
- Capitalize the subject line
Description
- First line should be empty
- Start writing in the second line
- Line break the description (making it readable without having to scroll horizontally)
- Use to explain what & why instead of how
- Wrap up at around 70 characters
Most standard flow
- Working on a feature
- Make a
feature
branch offdevelop
- Work on the feature
- Make a PR to
develop
- Make a
- Fixing production
- Make a
bugfix
branch offmaster
- Fix it
- Make a PR to
develop
&master
- Make a
Repository files
Generally, you only want your repository to just have your code. That is why a good practice is to maintain a .gitignore
file within the repo. This file should exclude when possible:
- Compiled source
- Databases
- Logs
- Packages
- OS/IDE generated (temporary) files
The rule of thumb here is that it is correct to exclude the files which fall into these categories in the .gitignore
of the repository when the files are generated by the code/tools from this repo.
When speaking about the OS and IDE files, it is best to maintain a personalized exclude file. You may check whether a global file is configured for your system by running and edit it.
git config --get core.excludesfile
If that command doesn’t return anything, the common solution is to create a .gitignore_global
in your user directory and set it as global.
For example, on Linux:
touch ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
For a source of inspiration on which files shouldn’t find their way inside your VCS feel free to check out this link.
Workflow Overview
- Each project must have a
master
&develop
branch. - Follow naming conventions and git workflow for making branches and creating Pull Requests (PR).
- If you have difficulties ask your fellow senior developer.
- Each project will have a senior/lead developer. He/She will check the PR before merging/deploying the branch.
The workflow is based on Gitflow, although not all elements may always be necessary.
Pull Requests
Read GitHub’s guide on Pull Requests (PRs) to understand how they work.
PR should be used in two cases:
- You are working on a feature branch and would like some feedback
- You have finished work on your feature branch and it is ready to be reviewed for merging into
develop
Ensure that PR compare the develop
branch (not master
) with your feature branch.
PR must be mergeable, meaning there should not be any merge conflicts.
Naming convention
We should be consistent naming PRs. They should be named similarly to the branch. Let’s take a look at an example.
Branch name: feature/<name-of-the-feature>
.
PR name: Feature/<Name of the feature>
When you are using Jira, please add the ticket ID to the name: Feature/<Jira-ID> - <Name of the feature>
. Doing it that way, Jira will automatically keep a reference to this branch.
Big Pull Requests
We should be encouraged to create small PRs as possible. But we cannot avoid them all the time. In some cases, we need to include some libraries to the project and it can get big and ugly. In such case we should do the following:
- create an Epic branch for that feature, e.g.
epic/<name-of-the-feature>
- create smaller feature or chore branches on top of that Epic branch, e.g.
chore/<name-of-the-library-to-include>
,feature/<name-of-the-feature>
- for each created branch create corresponding PR
- once all PRs inside Epic are reviewed, merge them into an Epic branch and create an Epic PR
- an Epic PR should only have commits from sub PRs
Reviewing Pull Requests
You should have enough context to thoroughly review the committed code just by reading it as a reviewer. However, there are times when the pull request is a bit on the large side, or it is referencing existing code not found in the PR, or you, for some other reason, do not have a good enough context to review anything other than code style and typos. In these cases, we encourage you to approach the PR submitter and go through the pull request together.
As a reviewer, your job is to be the extra pair of eyes so that the code will get twice as good. You will look at things such as code style and structure. You will discuss things you might have done differently, point out possible, more elegant solutions based on something you have learned, and so on. The goal here is to make the whole team better in the process.
Remember that you are reviewing something someone else has worked on hard, so always be respectful. These guidelines for giving feedback are a good reference:
- Familiarise yourself with the context of the issue.
- If you disagree strongly, consider giving it a few minutes before responding; think before you react. Try to empathize.
- Ask, do not tell. (“What do you think about trying…?” rather than “Don’t do…”)
- Explain your reasons why the code should be changed.
- Offer ways to simplify or improve code.
- Avoid using derogatory terms, like “stupid” when referring to the work someone has produced. Do not shame. No one started out knowing.
- Be humble. (“I’m not sure, let’s try…”)
- Respond to comments meant for you so the sender knows you have read and understood them.
- Try to be open and welcoming to criticism.
- Try to use positive language.
- Use emojis to clarify the tone.
This is based on GitHub’s How to write the perfect pull request.
Keeping up to date
When working on a feature branch, it is important to regularly pull everyone else’s work from develop
. Assuming you are working on feature/your-feature-branch
, use the following steps taken from this StackOverflow answer.
First check if there are even any changes on develop
to pull from GitHub.
git fetch origin
If there are changes, switch to develop
and fast forward to the latest.
git checkout develop
git merge --ff origin/develop
--ff
means fast-forward merge. In theory you shouldn’t have made any commits to develop
since you last left it, so you should be able to just fast forward it to the latest commit you’ve pulled down. If this fails, you might have accidentally committed to develop
at some stage. If not, it means someone rebased the remote develop
branch (which is bad…)
Now go back to your feature branch.
git checkout feature/your-feature-branch
The following steps depend on whether your changes are local or have been pushed to GitHub.
[Option 1] If you haven’t yet pushed your changes to GitHub
You can simply rebase from develop
, replaying your changes on top of the changes you just pulled from GitHub
git rebase develop
[Option 2] If you have already pushed your changes to GitHub
Do not rebase, this rewrites the history of the branch, and will cause problems when you try and push back to GitHub. Instead, merge the updates back into your feature branch.
git merge develop
git push origin feature/your-feature-branch
Collaborating on a feature
Feature branches will need to be pushed to GitHub for:
- Collaboration on a feature
- Code review before merging back into
develop
Push your local feature branch to GitHub for the first time
If you have created a local feature branch called feature/cool-feature
, push using:
git push -u origin feature/cool-feature
The -u
argument is not essential, it just tells Git to set up a tracking reference for the remote branch. It means you can do something like git fetch origin; git status;
and it will let you know if you are behind the remote branch. Subsequent pushes do not require the -u
argument.
Clone someone else’s remote feature branch
git fetch
git checkout -b feature/cool-feature origin/feature/cool-feature
This creates a new local branch called feature/cool-feature
that is based on the remote branch called origin/feature/cool-feature
that you just fetched.
Push updated code on a feature branch
If you want to push further commits back to GitHub, use the same push command as before, but without -u
:
git push origin feature/cool-feature
Pull updates on a feature branch
While on feature/cool-feature
:
git pull --rebase origin feature/cool-feature
Example Flow
Get started
Perform the following to clone the repository’s develop
branch:
git clone https://github.com/poviolabs/project-name.git
Start work on a new feature
To work on a new feature you need to create a new feature branch for your changes.
git checkout -b feature/your-feature-branch develop
You can omit develop
if you are already on the develop
branch
Do some work
While on your feature branch, make your changes and commit them as usual.
Finished with your feature
- Ensure you are up to date with the latest version from
develop
(see Keeping up to date) - Write unit tests for any code you have added
- Push your feature branch to GitHub (see Collaborating on a feature)
- Create a pull request that compares
develop
with your branch’s changes
Code Review completed
Once a team member has reviewed your code and everything is okay, one of you should either:
- Use GitHub to automatically merge the pull request’s changes into
develop
or - Manually perform the merge (assuming you have already pulled the most recent changes):
git checkout develop
git merge --no-ff feature/your-feature-branch
git push origin develop
Git Tools
- ZSH - Oh My Zsh is a delightful, open source, community-driven framework for managing your Zsh configuration
- Git Fork - Fast and friendly git client for Mac and Windows
Git Aliases
[alias]
c = "!git add -A && git commit -m "
p = !git push origin $(git rev-parse --abbrev-ref HEAD)
n = !git checkout -b
s = !git status
l = !git log
b = !git branch
ch = !git checkout
m = !git merge
gr = !git reset --soft HEAD^
pull = !git pull origin $(git rev-parse --abbrev-ref HEAD)
Example of usage:
g p -> git push origin <current branch>
g l -> git log
g s -> git status
##Credits Git Workflow Guide