Git best practices 2016

38
Git best practices AKA Let's write history

Transcript of Git best practices 2016

Page 1: Git best practices 2016

Git best practicesAKA

Let's write history

Page 2: Git best practices 2016

Git / t/ɡɪ”A silly, incompetent, stupid, annoying or childish person.”

http://en.wiktionary.org/wiki/git

Page 3: Git best practices 2016

"I'm an egotistical bastard, so I name all my projects after myself.

First Linux, now Git”

Linus Torvalds, PC World. 2012-07-14

Page 4: Git best practices 2016

Git popularity according to OpenHub.net

Page 5: Git best practices 2016

Git popularity according to Google Trends

git

svn

Page 6: Git best practices 2016

Why do we use git and version control at all?

Because history matters. In societies, and in software projects too. History teaches where we came from and

where we are going.

Page 7: Git best practices 2016

Software is built patch by patch

Page 8: Git best practices 2016

When a patch is committed to version control, it becomes a permanent part of history.

Understanding the nature of a patch is required to make good git commits.

Page 9: Git best practices 2016

A patch- attributes the author- has a title- may have a message- describes the lines added, removed and modified- the change is complete and self standing- e.g. fixes a defect or extends functionality

Page 10: Git best practices 2016

You must master reading history before you can master writing it. If you think you can master writing history

directly, you did not understand the nature of history at all.

Page 11: Git best practices 2016

Browsing GitHub, Gitlab, git-web et cetera is good, but inspecting your local copy directly is best.

Page 12: Git best practices 2016

git log --oneline

Page 13: Git best practices 2016

git show e413c6e

Page 14: Git best practices 2016

gitk --all

Page 15: Git best practices 2016

gitk --all

Page 16: Git best practices 2016

Note the existing style. You need to write in the same language to be understood.

Some universal rules apply, though.

Page 17: Git best practices 2016

Universal git commit rules:- Start the title line with a capital letter just like in email- Don't end with a dot- Keep title under 72 characters (the Github limit)- Title is followed by one empty line, and then comes the description- The message part contains full sentences with ending dots etc- Use imperative format (Not 'Cleaned' but 'Clean up')

Page 18: Git best practices 2016

Commit titles must look good in 'git log --oneline' and

complete the sentence 'This change will..'

Page 19: Git best practices 2016

Focus on describing WHY the change was made.

The revision history diff will always show WHAT was changed. If your code is reviewed, the reviewer will focus on judging the rationale of the WHY and compare it to

the WHAT to verify that they match.

If it is later discovered that your code had a bug, the contents will be updated (WHAT) but the purpose of what

it does will remain (WHY).

Page 20: Git best practices 2016

The best and permanent place for code documentation is in inline comments. Don't repeat inline comments in the

commit message.

The commit message should focus on why the change was needed, while the inline comment documents the

current version of the code.

Page 21: Git best practices 2016

Is the change related to an issue or discussion available somewhere else?

Include issue numbers and URLs in the commit message. Use trigger words like "Closes #123" to enable Github to

make automatic links and update issues statuses.

Page 22: Git best practices 2016

Work-in-progress commits

Few people write the correct code in one go. Thus writing the correct git commit right away might not be possible.

While developing, title the commits WIP, WIP2, WIP3 etc.

When you are done, rebase and squash the changes into the final commits that constitute logical steps.

git rebase -i master

Page 23: Git best practices 2016

Other important tools to write history:

git cherry-pick e413c6egit citool

git citool --amend

Page 24: Git best practices 2016

My favourite: git citoolYou can also spend all day writing code and at the end of the day save your work with 'git citool', where you can pick the hunks or lines that go

together to form the commits.

Page 25: Git best practices 2016

Are you committing somebody else's code?

Mark authorship correctly:git commit -a --author "Dieter Adriaenssens <[email protected]>"git commit --amend --author="James Cowgill <[email protected]>"git apply --author="Andreas Beckmann <[email protected]>" /tmp/andreas.patch

Page 26: Git best practices 2016

Branches

A series of commits. The purpose of branches is to allow different versions of the software to exists in parallel. Typically there is a development master branch and a

relase branch v1, which can evolve in separate directions.

The master branch may become release v2 while the v1 branch may become release v1.1.

Page 27: Git best practices 2016

All developers branch from master and then merge back.

New releases branch from master.

Support releases branch from release branch.

Image source: http://tleyden.github.io/blog/2014/04/09/a-successful-git-branching-model-with-enterprise-support/

Page 28: Git best practices 2016

The holy master principle: Never break the master branch!

All developers working to create new features or fix bugs use the current master branch as the base of their work.

If the master is already broken, it will be difficult to fix the bug the developer intended to spend time fixing, or a

new feature cannot be made to be functional if the starting point wasn't functional either.

Page 29: Git best practices 2016

Git is a distributed version control system

Every copy of the repository is a branch in itself. To share branches with others before they are merged to master,

label the branch with a separate name.

git checkout mastergit pull mastergit checkout -b feature-automatic-renewalsgit citool...git push origin feature-automatic-renewals

Page 30: Git best practices 2016

Apply the changes made by a collaborator:

git pull http://people.debian.org/~sthibault/tmp/mariadb-10.0 feature-hurd

After this your feature-hurd branch will have the same contents, and you can continue improving it.

Many prefer to publish and pull via GitHub, but any server you can push files on with SSH and pull via SSH or HTTP will do.

Page 31: Git best practices 2016

The holy Quality Assurance principle: all commits on master should go via review

GitHub "Pull Requests" facilitate this.Gerrit and Gerrithub for more formal teams.

Page 32: Git best practices 2016

Review principles:- Give feedback, even nitty gritty, to perfection the code (and documentation!).- Let the submitter fix it themselves, thus keeping authorship and commitment to maintaining the code.- Reviewing as an opportunity to learn. Both for the reviewee and reviewer!

Page 33: Git best practices 2016

Review stalls (often, unfortunately):- Reviewer does not agree on the motivation to the change- Reviewer does not understand the contents of the changes- Poor documentation, unlogical commits- Lack of QA, no testcases, no external verification- Unfit change for the upcoming release, lack of time, wrong reviewer

Page 34: Git best practices 2016

The holy history principle: never force push on master

Except if you are really quick and sure that nobody made a git pull in between.

If the rule is broken, developers and systems will have diverged versions of master, and everybody will need to

do manual merges or dangerous force pulls.

Page 35: Git best practices 2016

Remember: good outcomes require a lot of work

Don't feel frustrated. The devil is often in the details and the nature of high quality code and reliable systems

require diligent review. All big stable systems grew our of very small stable systems.

Do not sacrifice stability for development velocity, otherwise it will just collect debt and require twice as

much work later and grind development velocity to a full stop.

Page 36: Git best practices 2016

The hotfix principle

If you have blinking lights and sirens yelling, you can and should do whatever you need to.

Page 37: Git best practices 2016

Beginner tipsLearn the basics: http://try.github.com/

SSH-keys:https://help.github.com/articles/generating-ssh-keys/

Keep branch name visible in the bash prompt:https://github.com/ottok/tooling/blob/master/bashrc/bashrc.git

Page 38: Git best practices 2016

Contact Seravo if you need experts in Linux, Git or other

open source software

Open seravo.fiSee our blog for in-depth tips