70
Workshop Best practices with git The essentials you should know about git to use if efficiently Otto Kekäläinen OpenFest 7.11.2015 Sofia, Bulagaria

Git best practices workshop

Embed Size (px)

Citation preview

Page 1: Git best practices workshop

Workshop

Best practices with gitThe essentials you should know

about git to use if efficiently

Otto Kekäläinen OpenFest 7.11.2015

Sofia, Bulagaria

Page 2: Git best practices workshop

@ottokekalainen● 15+ years in open source● technology & business & advocacy● development with git since 2010

Page 3: Git best practices workshop

The MariaDB FoundationContinuity and open collaboration

● MariaDB.org sponsors include MariaDB.com, Booking.com, Automattic, Odin/Parallels, Visma...

● Employs 6 persons, including ”Monty” Widenius, the creator of MySQL/MariaDB

● Single contact point for collaboration and contributions● The Foundation makes sure all pull requests and patches are

reviewed● All development work done with git at github.com/mariadb

Page 4: Git best practices workshop

Outline1. The story of Git2. Basic commands3. Doing it with Github4. Branches and tags5. Git hooks and CI6. Git internals7. Advanced commands8. Git everywhere?

Follow @ottokekalainento get a link to the slides

Page 5: Git best practices workshop

1. Story of Git

Page 6: Git best practices workshop

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

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

Page 7: Git best practices workshop

"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 8: Git best practices workshop

Linus needed a new source code revision manager for Linux, and none of the available options in 2005 where

good enough, so he wrote his own in.

Kernel 2.6.12 was the first release managed by Git and version 1.0 of Git was released in December 2005.

Page 9: Git best practices workshop

Design goals of Git:● distributed revision management● protection against corruption,

both accidental and hostile● speed

Page 10: Git best practices workshop

Git popularity according to OpenHub.net

Page 11: Git best practices workshop

Git popularity according to Google Trends

git

svn

Page 12: Git best practices workshop

...but adoption would be faster if it was not so difficult to use.

Originally Linus did not intend end users to use Git directly, instead he tried to delegate to somebody else the task of making the actual command line

interface. We are still waiting for it...

Luckily Git has been simplified and documentation has improved over time, but some Git commands

still refer to Git internals that are difficult to grasp.

E.g. git-push: Update remote refs along with associated objects.

Page 13: Git best practices workshop

Git might feel difficult at first, but once you learn it, you never want to go back to anything less

flexible and powerful.

Page 14: Git best practices workshop

Install on Linux:apt-get install git git-gui

Install on Mac/Windows: git-scm.com/downloads

Page 15: Git best practices workshop

2. Basic commands

Page 16: Git best practices workshop

First obstacle: same terms with different meanings

Page 17: Git best practices workshop

git config --global user.name "John Doe"git config --global user.email [email protected]

Define the author of commits

Page 18: Git best practices workshop

git initgit add example.txtgit commit -am “First commit”# edit filesgit commit -am “Second commit”

Start a new repository

Page 19: Git best practices workshop

git pull# edit filesgit commit -am “Implemented X”git push

git loggit show

Typical workflow

Page 20: Git best practices workshop

How to write a good commit message

● Your attitude towards commit messages should be the same as for code: it is written once, but read thousands of times.

● Don't explain how was done, that is visible in the diff anyway. Explain what the intention was and why it was made.

● Use imperative form “Fix typo” (instead of “Fixed typo”)● Keep subject line short and sweet, under 72 chars. Body can be verbose. ● Use proper English. Capital letters. Reference issue identifiers is possible.● Looking for a good example? How about one by Linus himself?

https://github.com/torvalds/linux/commit/fc90888

Page 21: Git best practices workshop

Use git commit -a --amend to you screwed up the commit and want to overwrite the latest

with an improved commit.

Completely delete the last commit:git reset –hard HEAD^

Delete all untracked and modified files:git clean -fdx && git reset --hard

Page 22: Git best practices workshop

3. Doing it with Github

Page 23: Git best practices workshop
Page 24: Git best practices workshop

Visit http://try.github.com/

After making your first repository, set up SSH keyfor convenient authentication:

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

Note: Github is not the only Git hosting site,Gitlab and Bitbucket are popular too.

Page 25: Git best practices workshop

Exercise:- go to https://github.com/ottok/git-training and fork it- look at the fork under your own Github account and clone it to your laptop- edit names.txt and add yours- push changes back to Github- in Github, do a merge request back to me

Page 26: Git best practices workshop

git clone git://github.com/<yourname>/git-training.git

# edit names.txtgit commit -am “Added my name”git push# Open pull request on Github

Page 27: Git best practices workshop

The best part of Github is pull requests, and how they enable

frictionless collaboration among millions of developers

Page 28: Git best practices workshop

4. Branches and tags

Page 29: Git best practices workshop

Basically just labels that point to certain commit IDs.Each commit ID point to its parent, thus forms a chain.

Page 31: Git best practices workshop

Git layout strategy 1/2

● Start working on the master branch only. Don't create other branches until you really need them.

● Every new developer has their own branch (local clone).● Never break master. Push only stuff what works. Other

developers must be able to branch from master at any time and start working on something new, instead of having to deal with fixing a broken state that somebody else left in master.

Page 32: Git best practices workshop

Git layout strategy 2/2

● If you have a need to push your work that is half-way done, that is a good signal that you should create a new branch.

● Typically tree kind of branches:– feature branches

– bugfix branches

– personal branches

● Project layout: split code up in many separate files.– Lower likelihood of conflicting changes when branches merge.

Page 33: Git best practices workshop

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

Page 34: Git best practices workshop

Distributed version control?Example scenarios at

https://git-scm.com/about/distributed

Page 35: Git best practices workshop

5. Git hooks and CI

Page 36: Git best practices workshop

Git commit hook: stop bad code entering repo/.git/hooks$ cat pre-commit

#!/bin/bash

# Really fast check if syntax is at all parseable

for FILE in $(git diff --cached --name-only); do

if [[ "$FILE" =~ \.php$ ]]; then

php -l "$FILE" 1> /dev/null

if [[ $? -ne 0 ]]; then

echo -e "\e[1;33mAborting commit: PHP code contains syntax errors\e[0m" >&2

exit 1

fi

fi

done

Page 37: Git best practices workshop

Want to put a simple shared repository on any SSH capable server? Create a bare .git with no working files:

git init --bare

Want to have notifications when somebody commits?Put a shell script at .git/hooks/post-receive

Page 38: Git best practices workshop

Continuous integration

● Host your own Jenkins?● Travis-CI

Page 39: Git best practices workshop

6. Git internals

Page 40: Git best practices workshop

Folder /.git contains everything,your / is just the working copy.

Page 41: Git best practices workshop

Folder /.git contains everything,your / is just the working copy.

When you commit a 2 MB file /example.png,your / will grow to 4 MB...

Page 42: Git best practices workshop

When you add a file,it goes to the staging area.

The file does not go into theactual history tree until the stage

is committed.

Page 43: Git best practices workshop

Commands push and pull and many other commandsare shortcuts that act with both

your local repository and the remote repositories.

Page 44: Git best practices workshop

Image credits Steve Bennet (http://steveko.wordpress.com/2012/02/24/10-things-i-hate-about-git/)

Page 45: Git best practices workshop
Page 46: Git best practices workshop

Git tracks content, not files!

Git history is immutable as each commit ID is the SHA-1 of the commit data and metadata (including the commit ID of parents). Changing any commit in the history will change the SHA-1 commit IDs of every following commit in the chain.

If you need to change something in the history, you have to rebase and make a new history.

Page 47: Git best practices workshop

Git commit IDs and rebaseOriginal git log –oneline1bf7024 MDEV-8991: bind-address appears twice in default my.cnfb2205c5 MDEV-9011: Redo log encryption does not workcf9e6b2 Fix test failures seen on buildbot.923827e MDEV-7949: Item_field::used_tables() takes 0.29% in OLTP RO239e0c5 MDEV-8551 compilation fails with 10.1.6

After git rebase -i HEAD^^^34350b9 MDEV-8991: bind-address appears twice in default my.cnff5f2dd9 MDEV-9011: Redo log encryption does not work531e1ac Fixed all bugs923827e MDEV-7949: Item_field::used_tables() takes 0.29% in OLTP RO239e0c5 MDEV-8551 compilation fails with 10.1.6

Page 48: Git best practices workshop

Rebasing workflow

git rebase -i HEAD^^^

# pick in editor what commits you want to edit

# rebase automatically makes a checkout at intended commit

# edit the files

git commit -a --amend

git rebase --continue

# repeat until you reach the head of the branch

Page 49: Git best practices workshop

7. Advanced commands

Page 50: Git best practices workshop

Sorry, but default commands not very friendly, so get yourself good cheat cheets and write up your common

commands once you've figured them out..

Image credits Steve Bennet (http://steveko.wordpress.com/2012/02/24/10-things-i-hate-about-git/)

Page 51: Git best practices workshop
Page 52: Git best practices workshop

Example of how fast-forward works 1/2

● Branch “feature-branch-example” forked from master branch “10.1” and has 3 commits

Page 53: Git best practices workshop

Example of how fast-forward works 2/2

● Normal merge defaults to fast-forward in this case●

● Result of no fast-forward (git merge --no-ff)

Page 54: Git best practices workshop

Want to avoid “ugly” merge commits?

● git config pull.ff=only● git config alias.ff=merge --ff-only● Run git rebase master to rebase you work on the master branch

before pushing or making pull request– In MariaDB before submitting a pull request: git rebase 10.1– You changes will be based on current 10.1 head and easy to merge

● Run git merge on when importing changes from remote head only if you really want to merge

Page 55: Git best practices workshop

Cherry-pick as alternative to merge

git cherry-pick 166a2f28487530ead0cf813ce0252baa

The commit with given ID will be applied as a new commit on top of your current branch head.

Page 56: Git best practices workshop

Git bisect – find the commit that broke you app

git bisect bad # mark the checked out commit as bad

git bisect good mariadb-10.1.7 # mark the ref (=commit) good

git bisect run my-test.sh

Git automatically checks out every commit between “bad” and “good” and runs the test script.

Test script is supposed to return 0 for good commits and non-0 for bad commits.

Exceptions are exit codes 255 and 125 that have special meaning to git bisect.

When the run is completed git will tell you the exact commit between “good” and “bad” where the my-test.sh started to fail (exit code not 0).

Page 57: Git best practices workshop

Git stash as alternative to temp branch

git stash

# moves the current uncommited changes to the stash

git stash list

# shows what is stashed

git stash pop

# applies the latest stash on the checked out working tree

Page 58: Git best practices workshop

Compare changes and create diffs

git show 166a2f284875

# standard show in patch format

git diff v1.2.3..HEAD translations/ > new-translations.diff

# diff of all changes in directory translation/ since tag v.1.2.3

git diff branch1 branch2 # compare two branches

git diff branch1..branch2 # same as above

git diff branch1...branch2 # tree dots: changes on branch2 since it diverged

Page 59: Git best practices workshop

GUI tool: gitk● graphical tool to view git history (and also uncommitted changes)● shows all commits, tags, branch names etc in a nice way● with gitk you don’t need to checkout the branch/file to view it● includes search feature for both commit messages and commit contents● run “gitk example/file.h” to see the git log that affects a single file● “git gui blame” can be used to view graphically via the context menu that opens when

clicking on a commit with the secondary mouse button● view diff between any two diffs: click on first commit normally, and then click with

secondary button on the second commit and select from context menu to view the diff● run “gitk mariadb-10.1.4..mariadb-10.1.5” to view diff between two commits● you can also view diff between different branches like this

– gitk 10.1..HEAD vs gitk HEAD..10.1 vs gitk 10.1...HEAD

Page 60: Git best practices workshop

Other GUI tools● git citool – graphical tool to do selective commits easily

– you can click to select the files you want, individual hunks inside files, and even individual lines inside hunks

– you can also write commits inside the tool’s window

● git mergetool – tool for easy merging,– automatically launches the pre-configured 3-way merge tool for

all conflicts (e.g. meld)

Page 61: Git best practices workshop

Compress and free up file space:git repack -ad; git gc --aggressive

Page 62: Git best practices workshop

8. Git everywhere?

Page 63: Git best practices workshop

Git everywhere

● Distributed permanent storage files system: https://ipfs.io/ ● Password manager: https://github.com/IJHack/qtpass

Page 64: Git best practices workshop

Would you like to store all your files in Git?Git-annex

Diff of binary files?Add in .git/config

[diff "odf"] textconv=odt2txt

See also: http://www-verimag.imag.fr/~moy/opendocument/

Page 65: Git best practices workshop

Bug tracker as part of project?http://bugseverywhere.org/

Bug tracker and wiki contents in Git?trac+git

Clone wiki from Github withgit clone https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.wiki.git

Page 66: Git best practices workshop

Publish to the web with one commit?self-hosted Jekyll or

on Github https://pages.github.com/

Open source alternative to Dropbox based on Git?http://sparkleshare.org/

Page 67: Git best practices workshop

Would you like others to contribute to your software?

Provide easy forking (git clone), easy way to develop new feature (git branch),

and an easy way to send them back (merge request).

Page 68: Git best practices workshop

Will Git credits replace developers CV's?

Page 69: Git best practices workshop

Is there anything we can't store and track in Git?

Page 70: Git best practices workshop

© 2015 MariaDB Foundation70

Thanks!

mariadb.org

@[email protected]