How Not To GIT
Lucas Gomes <@x8lucas8x>
Agenda
● Who am I?
● GIT Anti-Patterns
○ Manual Merging
○ Copy-Paste Backup
○ Dumb Bug Hunting
○ Dumb Conflict
Resolution
...
Software Engineer
Passionate Hacker
ArchLinux Zealot
FOSS Enthusiast
Startups, Big Data,
AI, NoSQL, Python,
C++/Qt, Git, G...
GIT Anti-Patterns
4
Manual Merging
5
$ git checkout branchA
...
$ git commit
...
$ git pull origin master
...
$ echo ‘No pull requests this ti...
Manual Merging
6
● Problems
○ “To Err is Human”
■ Automate what you can
○ Permission management
■ Higher friction
○ Jeopar...
Manual Merging
7
A’s
Local
B’s
Local
C’s
Local
Central
Remote
Shared
Repository
Manual Merging
8
A’s
Local
B’s
Local
Central
Remote
A’s
Remote
B’s
Remote
Fork
Pull
Manual Merging
9
● Solution
○ Fork & Pull model
■ Use proper tools
○ Avoid manual
merges/rebases
■ Default to pull
requests
Manual Merging
10
● Solution
○ Fork to access
■ Create it first
$ git clone git@server_url:my_fork/remote.git
Manual Merging
11
● Solution
○ For updates
■ Forbid pushes
■ Set up read-only
remote
$ git remote add central https://serv...
Copy-Paste Backup
12
$ git status
# On branch master
# ...
# modified: index.html
#
...
$ cp index.html backup_index.html
...
Copy-Paste Backup
13
● Problems
○ “To Err is Human”
■ Use proper tools
Copy-Paste Backup
14
● Solution
○ Stash it
$ git stash
Saved working directory and index state "WIP on master: 049d078 add...
Copy-Paste Backup
15
● Solution
○ Stash it
$ git status
# On branch master
nothing to commit, working directory clean
$ gi...
Copy-Paste Backup
16
● Solution
○ Stash it
$ git stash apply
# On branch master
# Changes not staged for commit:
# (use "g...
Dumb Bug Hunting
17
$ git commit
...
$ git commit
...
$ git commit
...
$ run app.py
FATAL ERROR
Gonna cry?
...
$ diff HEAD...
Dumb Bug Hunting
18
● Problems
○ Not efficient
○ More changed code
==
More difficulty
Dumb Bug Hunting
19
● Solution
○ Manually Bitsec
$ git bisect start
$ git bisect bad BAD_SHA1_HASH
$ git bisect good GOOD_...
Dumb Bug Hunting
20
● Solution
○ Manually Bitsec
$ git bisect good
Bisecting: 3 revisions left to test after this
[b047b02...
Dumb Bug Hunting
21
● Solution
○ Manually Bitsec
$ git bisect good
b047b02ea83310a70fd603dc8cd7a6cd13d15c04 is first bad c...
Dumb Bug Hunting
22
● Solution
○ Automate Bitsec
$ git bisect start BAD_SHA1_HASH GOOD_SHA1_HASH
$ git bisect run make tes...
Dumb Conflict Resolution
23
$ git pull origin master
...
$ git status
# On branch branchA
# You have unmerged paths.
# ......
Dumb Conflict Resolution
24
● Problems
○ Not efficient
○ Prone to typing errors
Dumb Conflict Resolution
25
● Solution
○ Use mergetool
$ git config --global merge.tool YOUR_DIFF_VIEWER
Dumb Conflict Resolution
26
● Solution
○ Use mergetool
$ git mergetool
Merging the files: README
Normal merge conflict for...
Dumb Conflict Resolution
27
● Solution
○ Use mergetool (KDiff3)
Dumb Conflict Resolution
28
● Solution
○ Use mergetool (Meld)
Dumb Conflict Resolution
29
● Solution
○ Use mergetool
■ emerge
■ gvimdiff
■ vimdiff
Jumbo Commit
30
$ git add src/views/.
...
$ git add src/models/.
...
$ git add src/delegates/.
...
$ git add images/.
...
...
Jumbo Commit
31
● Problems
○ Long-term results
■ Team
○ Review procrastination
■ Higher cognitive load
○ Pulls are more pr...
Jumbo Commit
32
● Solution
○ Divide and conquer
■ Create granular
commits
Messing with History
33
$ git pull origin master
...
$ git log
...
$ git commit --amend
...
$ git push origin master
...
#...
Messing with History
34
● Problems
○ Cannot push
○ If forced (push -f)
■ Break others
history
■ Lose commits
Messing with History
35
● Solution
○ Avoid messing with
pushed history
■ On shared
repositories only
Messing with History
36
● Solution
○ Use Git Revert
$ git revert DESIRED_SHA1_HASH
Divergent Master
37
$ git checkout master
...
$ git rebase branchA
...
$ git status
...
# Your branch and 'origin/master'
...
Divergent Master
38
● Problems
○ Prone to mix different
branches commits
■ Master is the
ramification point
Divergent Master
39
● Solution
○ Use master as a carbon copy
$ git checkout -b branchA
Switched to a new branch 'branchA'
...
Divergent Master
40
● Solution
○ Use master as a carbon copy
$ git branch -a
branchA
remotes/central/master
$ git checkout...
Divergent Master
41
● Solution
○ Use master as a carbon copy
$ git checkout -b master
Switched to a new branch ‘master’
Self Reviewing
42
$ git push origin branchA
...
$ echo “Lets create a pull request!”
...
Self Reviewing
43
● Problems
○ Team specialisation
■ “Not my code"
○ Less opportunity
■ Learning
■ Training
Self Reviewing
44
● Solution
○ Assign pull requests
to others
References
1. https://www.git-scm.com/
2. http://www.atlassian.com/git/tutorials/
45
How Not To GIT
Lucas Gomes <@x8lucas8x>
Próximos SlideShares
Carregando em…5
×

How Not to GIT

697 visualizações

Publicada em

Some set of problems that bad practices using GIT can lead to and how to remedy them.

Publicada em: Software
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

How Not to GIT

  1. 1. How Not To GIT Lucas Gomes <@x8lucas8x>
  2. 2. Agenda ● Who am I? ● GIT Anti-Patterns ○ Manual Merging ○ Copy-Paste Backup ○ Dumb Bug Hunting ○ Dumb Conflict Resolution ○ Jumbo Commit 2 ○ Messing with History ○ Divergent Master ○ Self Reviewing ● References ● Q & A
  3. 3. Software Engineer Passionate Hacker ArchLinux Zealot FOSS Enthusiast Startups, Big Data, AI, NoSQL, Python, C++/Qt, Git, Groovy, Cloud, IoT, and all that jazz. Lucas Lira Gomes 3 x8lucas8x@gmail.com linkedin.com/in/x8lucas8x facebook.com/x8lucas8x youtube.com/X80lucas08X twitter.com/x8lucas8x last.fm/user/x8lucas8x github.com/x8lucas8x
  4. 4. GIT Anti-Patterns 4
  5. 5. Manual Merging 5 $ git checkout branchA ... $ git commit ... $ git pull origin master ... $ echo ‘No pull requests this time!’ ... $ git checkout master ... $ git rebase branchA ... $ git push origin master ...
  6. 6. Manual Merging 6 ● Problems ○ “To Err is Human” ■ Automate what you can ○ Permission management ■ Higher friction ○ Jeopardise collaboration ■ Less code-review
  7. 7. Manual Merging 7 A’s Local B’s Local C’s Local Central Remote Shared Repository
  8. 8. Manual Merging 8 A’s Local B’s Local Central Remote A’s Remote B’s Remote Fork Pull
  9. 9. Manual Merging 9 ● Solution ○ Fork & Pull model ■ Use proper tools ○ Avoid manual merges/rebases ■ Default to pull requests
  10. 10. Manual Merging 10 ● Solution ○ Fork to access ■ Create it first $ git clone git@server_url:my_fork/remote.git
  11. 11. Manual Merging 11 ● Solution ○ For updates ■ Forbid pushes ■ Set up read-only remote $ git remote add central https://server_url/central/remote.git $ git remote set-url central --push "You shall NOT push!!!"
  12. 12. Copy-Paste Backup 12 $ git status # On branch master # ... # modified: index.html # ... $ cp index.html backup_index.html ... $ git checkout -- index.html ... $ git checkout branchB Switched to a new branch 'branchB'
  13. 13. Copy-Paste Backup 13 ● Problems ○ “To Err is Human” ■ Use proper tools
  14. 14. Copy-Paste Backup 14 ● Solution ○ Stash it $ git stash Saved working directory and index state "WIP on master: 049d078 added the index file" HEAD is now at 049d078 added the index file (To restore them type "git stash apply")
  15. 15. Copy-Paste Backup 15 ● Solution ○ Stash it $ git status # On branch master nothing to commit, working directory clean $ git stash list stash@{0}: WIP on master: 049d078 added the index file
  16. 16. Copy-Paste Backup 16 ● Solution ○ Stash it $ git stash apply # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # # modified: index.html $ git stash drop stash@{0} Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)
  17. 17. Dumb Bug Hunting 17 $ git commit ... $ git commit ... $ git commit ... $ run app.py FATAL ERROR Gonna cry? ... $ diff HEAD~12 ... $ vim app.py ...
  18. 18. Dumb Bug Hunting 18 ● Problems ○ Not efficient ○ More changed code == More difficulty
  19. 19. Dumb Bug Hunting 19 ● Solution ○ Manually Bitsec $ git bisect start $ git bisect bad BAD_SHA1_HASH $ git bisect good GOOD_SHA1_HASH Bisecting: 6 revisions left to test after this [b047b02ea83310a70fd603dc8cd7a6cd13d15c04] secure this thing
  20. 20. Dumb Bug Hunting 20 ● Solution ○ Manually Bitsec $ git bisect good Bisecting: 3 revisions left to test after this [b047b02ea83310a70fd603dc8cd7a6cd13d15c04] secure this thing $ git bisect bad Bisecting: 1 revisions left to test after this [f71ce38690acf49c1f3c9bea38e09d82a5ce6014] drop exceptions table
  21. 21. Dumb Bug Hunting 21 ● Solution ○ Manually Bitsec $ git bisect good b047b02ea83310a70fd603dc8cd7a6cd13d15c04 is first bad commit commit b047b02ea83310a70fd603dc8cd7a6cd13d15c04 Author: User1 <user1@example.com> Date: Tue Jan 27 14:48:32 2015 -0800 ...
  22. 22. Dumb Bug Hunting 22 ● Solution ○ Automate Bitsec $ git bisect start BAD_SHA1_HASH GOOD_SHA1_HASH $ git bisect run make test arguments ...
  23. 23. Dumb Conflict Resolution 23 $ git pull origin master ... $ git status # On branch branchA # You have unmerged paths. # ... # both modified: README.md # ... $ vim README.md ...
  24. 24. Dumb Conflict Resolution 24 ● Problems ○ Not efficient ○ Prone to typing errors
  25. 25. Dumb Conflict Resolution 25 ● Solution ○ Use mergetool $ git config --global merge.tool YOUR_DIFF_VIEWER
  26. 26. Dumb Conflict Resolution 26 ● Solution ○ Use mergetool $ git mergetool Merging the files: README Normal merge conflict for 'README': {local}: modified {remote}: modified Hit return to start merge resolution tool (kdiff3):
  27. 27. Dumb Conflict Resolution 27 ● Solution ○ Use mergetool (KDiff3)
  28. 28. Dumb Conflict Resolution 28 ● Solution ○ Use mergetool (Meld)
  29. 29. Dumb Conflict Resolution 29 ● Solution ○ Use mergetool ■ emerge ■ gvimdiff ■ vimdiff
  30. 30. Jumbo Commit 30 $ git add src/views/. ... $ git add src/models/. ... $ git add src/delegates/. ... $ git add images/. ... $ git commit -a ‘Biggest commit ever!’ ...
  31. 31. Jumbo Commit 31 ● Problems ○ Long-term results ■ Team ○ Review procrastination ■ Higher cognitive load ○ Pulls are more prone to conflict
  32. 32. Jumbo Commit 32 ● Solution ○ Divide and conquer ■ Create granular commits
  33. 33. Messing with History 33 $ git pull origin master ... $ git log ... $ git commit --amend ... $ git push origin master ... # ! [rejected] master -> master (non-fast-forward) ...
  34. 34. Messing with History 34 ● Problems ○ Cannot push ○ If forced (push -f) ■ Break others history ■ Lose commits
  35. 35. Messing with History 35 ● Solution ○ Avoid messing with pushed history ■ On shared repositories only
  36. 36. Messing with History 36 ● Solution ○ Use Git Revert $ git revert DESIRED_SHA1_HASH
  37. 37. Divergent Master 37 $ git checkout master ... $ git rebase branchA ... $ git status ... # Your branch and 'origin/master' have diverged ...
  38. 38. Divergent Master 38 ● Problems ○ Prone to mix different branches commits ■ Master is the ramification point
  39. 39. Divergent Master 39 ● Solution ○ Use master as a carbon copy $ git checkout -b branchA Switched to a new branch 'branchA' $ git branch -D master ...
  40. 40. Divergent Master 40 ● Solution ○ Use master as a carbon copy $ git branch -a branchA remotes/central/master $ git checkout remotes/central/master ...
  41. 41. Divergent Master 41 ● Solution ○ Use master as a carbon copy $ git checkout -b master Switched to a new branch ‘master’
  42. 42. Self Reviewing 42 $ git push origin branchA ... $ echo “Lets create a pull request!” ...
  43. 43. Self Reviewing 43 ● Problems ○ Team specialisation ■ “Not my code" ○ Less opportunity ■ Learning ■ Training
  44. 44. Self Reviewing 44 ● Solution ○ Assign pull requests to others
  45. 45. References 1. https://www.git-scm.com/ 2. http://www.atlassian.com/git/tutorials/ 45
  46. 46. How Not To GIT Lucas Gomes <@x8lucas8x>

×