78
Best Practices Joaquim Rocha | [email protected] IT-DSS-TD January 24, 2014

Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

  • Upload
    others

  • View
    24

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Best Practices

Joaquim Rocha | [email protected]

IT-DSS-TD

January 24, 2014

Page 2: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

About yours truly

Contributor to/Author of many Open Source projectsMember of the GNOME FoundationFormer member of Igalia and Red HatCurrently at the Data Storage Services group at CERNhttp://www.joaquimrocha.com

Joaquim Rocha Git: Best Practices

Page 3: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

“I’m an egotistical bastard, so I name all my projects aftermyself. First Linux, now git"

Linus Torvalds

Page 4: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

What’s git

Distributed Version Control System (DVCS)

Initially written in C by Linus Torvalds

Replacement for BitKeeper in Linux kernel development

Widely used nowadays, both for new and old projects

Joaquim Rocha Git: Best Practices

Page 5: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for
Page 6: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

What’s git

Page 7: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

It’s the sandbox

Joaquim Rocha Git: Best Practices

Page 8: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Holds the new changes to be committed

Joaquim Rocha Git: Best Practices

Page 9: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Points to the current commit

Joaquim Rocha Git: Best Practices

Page 10: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for
Page 11: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Working Tree with Changes

Joaquim Rocha Git: Best Practices

Page 12: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Joaquim Rocha Git: Best Practices

Page 13: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Index with Changes

Joaquim Rocha Git: Best Practices

Page 14: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Typical beginners’ workflow with git:

1 git clone URL or git init .

2 Do stuff...3 git add --all or git add FILE

4 git commit -m “Update" [or another bad description]5 git push

Joaquim Rocha Git: Best Practices

Page 15: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for
Page 16: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Best Practices

Page 17: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Adding Files

Joaquim Rocha Git: Best Practices

Page 18: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Adding Files

Do NOT:git add *

git add .

git add --all

git add -u .... and unless you’re adding a new (unknown to index) file:git add FILE_PATH

Joaquim Rocha Git: Best Practices

Page 19: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Adding Files

Please Do:

git add --patch

Joaquim Rocha Git: Best Practices

Page 20: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Adding Files

git add --patch or git add -p

asks you which chunks of code should be added and how!means you review your code before adding it!helps you create atomic commits!

Joaquim Rocha Git: Best Practices

Page 21: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Adding Files: Example

[...]

if (delegateAuthLibPath)loadDelegateAuthLib(delegateAuthLibPath);

++ fprintf(stderr, "WHY THE HELL DOESN’T THIS WORK!!");}

AuthChangeFsUid::~AuthChangeFsUid()Stage this hunk \[y,n,q,a,d,/,j,J,g,e,?\]?

Joaquim Rocha Git: Best Practices

Page 22: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Committing

Joaquim Rocha Git: Best Practices

Page 23: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Committing

Do atomic commits!Write useful, descriptive commit messages!Don’t be afraid of committing (you’ll see why later)!

Joaquim Rocha Git: Best Practices

Page 24: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Atomic Commits

Sherlock was investigating why the new versionof his team’s software didn’t work...

Page 25: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Atomic Commits

He quickly looked at his favorite git UI (which uses git log--oneline log) for clues...

1d60cd2 Update for 1.5.0817ee03 Innocent stuff...8341828 Innocent stuff #1...afee627 Innocent stuff #2...d4c29b7 Innocent stuff #3...c3bdb6d Innocent stuff #4...a3e8c35 Innocent stuff #5...fd9f14b Innocent stuff #6...da8a2cf Innocent stuff #7...c2debc0 Innocent stuff #8...283293c Innocent stuff #9......

Joaquim Rocha Git: Best Practices

Page 26: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Atomic Commits

Not finding anything suspicious in the log, Sherlock starts abisect in order to find the problem!

With all the compiling and testing, he spends hours in the bisectbefore finding the culprit:

1d60cd2 Update for 1.5.0

Joaquim Rocha Git: Best Practices

Page 27: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Atomic Commits

Sherlock finds out the commit has more to it than meets theeye...

commit 1d60cd23c6597f1d11288644691b582bd531e704Author: Moriarty <[email protected]>Date: Thu Jun 6 18:06:06 2013 +0100

Update for 1.5.0

Evil stuff because I can!More evil stuff because I can!...

Joaquim Rocha Git: Best Practices

Page 28: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Always write atomiccommits!

Page 29: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Write useful commitmessages

Joaquim Rocha Git: Best Practices

Page 30: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Write useful commit messages

A good commit message:

Imperative tense summary, <= 50 chars

When necessary, more details can come here, until 72 charseach line.A reference to a bug tracker issue can be added as well:

http://bugtracker.earth/issue/1985

Joaquim Rocha Git: Best Practices

Page 31: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Write useful commit messages

Badfixes a crash

Awfulfix

GoodFix crash when performing update

The issue was being caused when the Updater was calledand a network connection that had been used beforeis no longer available.

https://sherlocksbugtracker.co.uk/issue/1985

Joaquim Rocha Git: Best Practices

Page 32: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Know How to UseReferences

Joaquim Rocha Git: Best Practices

Page 33: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Know How to Use References

Specific References:

The current commit: HEADA hash from some commit, e.g.:d3a7c852d2c789f791b11091894cc71387e562e9Also works with just a prefix: d3a7c85A branch, e.g.: master, newfeatureA tag, e.g.: release0.9

Joaquim Rocha Git: Best Practices

Page 34: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Know How to Use References

Relative References:

Referring to previous commits:

Commit 1 position before: ref~1 or ref^Commit 5 position before: ref~5 or ref^^^^^E.g. a branch’s parent commit: newfeature^

Referring to different parents:

1st parent of a commit: ref^12nd parent of a commit: ref^2Nth parent of a commit: ref^N

Joaquim Rocha Git: Best Practices

Page 35: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Use git bisect to trackissues

Joaquim Rocha Git: Best Practices

Page 36: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Use git bisect to track issues

git bisect helps finding a commit that introduced a bug bymaking a binary search

$ git bisect start$ git bisect bad$ git bisect good HEAD~10... [check until finding the culprit]$ git bisect reset... [fix it]

Joaquim Rocha Git: Best Practices

Page 37: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git reset

Joaquim Rocha Git: Best Practices

Page 38: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git reset

Want to undo adding a file (after git add FILE_PATH)?

git reset FILE_PATH

The file is now in the Working Tree (only) againIndex moved back

Joaquim Rocha Git: Best Practices

Page 39: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git reset

Want to undo the previous commit?

git reset HEADˆ

The changes are now only in the Working Tree (you haveto add them again before committing)HEAD and Index moved back one step

Joaquim Rocha Git: Best Practices

Page 40: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git reset

Want to undo the previous commit but keep it ready to commit?

git reset -soft HEADˆ

The changes are now in the Index (ready to be committed)HEAD moved back one step

Joaquim Rocha Git: Best Practices

Page 41: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git reset

Want to delete the previous commit?

git reset -hard HEADˆ

It’s as if the previous commit didn’t happenHEAD, Index and Working Tree move back one step

Joaquim Rocha Git: Best Practices

Page 42: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git reflog

Joaquim Rocha Git: Best Practices

Page 43: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git reflog

Sometimes things don’t go as expected! E.g.:

...$ git commit -m "Very important commit"...$ git reset --hard HEAD^$ git log --onelinee897f7f Other things...61be62b Other things #1......$ NOOOOOOOOOOOOObash: NOOOOOOOOOOOOO: command not found...

Joaquim Rocha Git: Best Practices

Page 44: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git reflog

Luckily, git reflog comes to rescue!

$ git refloge897f7f HEAD@{0}: reset: moving to HEAD^ca33ef2 HEAD@{1}: commit: Very important commite897f7f HEAD@{2}: checkout: moving from masterto importantfeature...

$ git rebase ca33ef2$ git log --onelineca33ef2 Very important commite897f7f Other things...61be62b Other things #1......

Joaquim Rocha Git: Best Practices

Page 45: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Branches

Joaquim Rocha Git: Best Practices

Page 46: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Branches

A branch is simply a pointer to a commit!(unlike other VCS which copied directories...)

Joaquim Rocha Git: Best Practices

Page 47: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Branches

Create a new branch:git branch <new-branch> [<start-point>]

Delete a branch:git branch -d|-D <branch>

Rename a branch:git branch -m <old-name> <new-name>

List branches:git branch [-r|-a]

Move into (checkout) a branch:git checkout <branch>

Create a branch and check it out:git checkout -b <branch>

Joaquim Rocha Git: Best Practices

Page 48: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Rebase a branch

Joaquim Rocha Git: Best Practices

Page 49: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Merge two branches

Joaquim Rocha Git: Best Practices

Page 50: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Branches

Pro tip:

Use one branch per feature/bug (contained development)Only merge with master after you’re doneRemember to rebase your feature branch before merging itto masterSpecify the origin and branch when pushing (might avoidmistakes)A good use of branches should prevent the need of gitcherry-pick

Joaquim Rocha Git: Best Practices

Page 51: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Branches

Proposed workflow:

... [we’re on master]$ git checkout -b newfeature... [make changes]$ git commit -m "New feature"$ git rebase master$ git checkout master$ git merge newfeature$ git push origin master

If push fails:

$ git pull --rebase

Joaquim Rocha Git: Best Practices

Page 52: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Working with Remote Branches

List remote branches:

$ git branch -rremotes/origin/HEAD -> origin/masterremotes/origin/masterremotes/origin/newfeatureremotes/origin/newfeature2remotes/origin/newfeature3

Joaquim Rocha Git: Best Practices

Page 53: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Branches

Push a branch to origin:

$ git checkout newfeature$ git push origin newfeature

Joaquim Rocha Git: Best Practices

Page 54: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Branches

Delete a remote branch in origin:

$ git push origin :newfeature

Joaquim Rocha Git: Best Practices

Page 55: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Branches

Replace a remote branch in origin:

$ git push origin +otherfeature:newfeature

It’s not advisable to replace branches you share with otherpeople (it “breaks” other people’s branch)

Joaquim Rocha Git: Best Practices

Page 56: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git rebase --interactive

Joaquim Rocha Git: Best Practices

Page 57: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git rebase --interactive

git rebase --interactive is a great tool that allows to:

Meld commits togetherRemove commitsEdit commits (it stops the rebase and allows to amend acommit)Reorder commits

Joaquim Rocha Git: Best Practices

Page 58: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git rebase --interactive

$ git rebase -i HEAD~5

pick d3a7c85 New changespick b538761 Other changespick 61be62b Some nice new changespick e897f7f Update for release 0.9.2pick ca33ef2 Commit that should have been in the release

# Rebase 8d9b5a1..ca33ef2 onto 8d9b5a1## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit

Joaquim Rocha Git: Best Practices

Page 59: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git rebase --interactive

pick d3a7c85 New changessquash b538761 Other changespick 61be62b Some nice new changespick ca33ef2 Commit that should have been in the releasepick e897f7f Update for release 0.9.2

# Rebase 8d9b5a1..ca33ef2 onto 8d9b5a1## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit

Joaquim Rocha Git: Best Practices

Page 60: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Name your stash

Joaquim Rocha Git: Best Practices

Page 61: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Name your stash

git stash is great for storing changes away temporarily

Use git stash save “Description of changes"so it’s easy to keep track of what’s what!

Joaquim Rocha Git: Best Practices

Page 62: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Use repos for differentmodules

Joaquim Rocha Git: Best Practices

Page 63: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Use repos for different modules

Sometimes modules shouldn’t be in the same repo

A base + plugins project should live in different repose.g.: GStreamer

Joaquim Rocha Git: Best Practices

Page 64: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Do NOT add every singlefile

Joaquim Rocha Git: Best Practices

Page 65: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Do NOT add every single file

Binary files, distro packages (rpms, debs) and otherauto-generated files should not be included in the repo!Upload them somewhere else, a repo should be for sourcecode.

Joaquim Rocha Git: Best Practices

Page 66: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Don’t break others’ repos

Joaquim Rocha Git: Best Practices

Page 67: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Don’t break others’ repos

Make sure that you do not change history in main branches(shared with other people)!

Be careful when resetting and rebasingDo not force a push to a shared branch

Joaquim Rocha Git: Best Practices

Page 68: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Pull and rebase

Joaquim Rocha Git: Best Practices

Page 69: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Pull and rebase

Use git pull --rebase in order to avoid merges fromupstream commits.

Joaquim Rocha Git: Best Practices

Page 70: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Other (Pro) Tips

Page 71: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Use git hooks to enforce standards

Git hooks can be used to e.g.:

make sure the source honors a certain coding styleforce a commit message to include a bug tracker referenceetc.

Joaquim Rocha Git: Best Practices

Page 72: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

git clean

Be careful with git clean!

git clean -xdf will really delete things, no reflog, nonothing!Always dry-run first!$ git clean -nxdf

Joaquim Rocha Git: Best Practices

Page 73: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Use command aliases and auto-completion

Use git aliases and auto-completion for extra productivity:

git config --global alias.ci ’commit’

git config --global alias.co ’checkout’

git config --global alias.st ’status’

Auto-completion:http://code-worrier.com/blog/autocomplete-git/

Joaquim Rocha Git: Best Practices

Page 74: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Show your HEAD in shell’s prompt

Change your shell’s prompt to show your git branch:http://code-worrier.com/blog/git-branch-in-bash-prompt/

[17:42][~/presentations/git-best-practices(master)]$

Joaquim Rocha Git: Best Practices

Page 75: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Do NOT copy/paste diffs

Create patches by using git format-patch

Patches of the last 2 commits:$ git format-patch HEAD~20001-Some-nice-changes.patch0002-Some-other-nice-changes.patch

You can even send them by email from git using gitsend-email!

Joaquim Rocha Git: Best Practices

Page 76: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

See what your tree looks like

Having a global picture of your tree is important!

Use git lola:git config --global alias.lola "log --graph --decorate

--pretty=oneline --abbrev-commit --all"

... or use tig for an interactive CLI viewer

... or use or a graphical tool like gitg

Joaquim Rocha Git: Best Practices

Page 77: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for
Page 78: Git: Best Practices · git rebase --interactive $ git rebase -i HEAD~5 pick d3a7c85 New changes pick b538761 Other changes pick 61be62b Some nice new changes pick e897f7f Update for

Thank you!

Git’s logo, CC by Jason LongLinus Torvalds picture, CC by The Linux FoundationKid picture, CC by Juhan SoninMonkey picture, CC by Scott MontySlides and diagrams based inGit: The Stupid Content Tracker by Mario Sánchez Prada

Joaquim Rocha Git: Best Practices