How Not to GIT
-
Upload
lucas-gomes -
Category
Software
-
view
66 -
download
2
Transcript of How Not to GIT
How Not To GITLucas Gomes <@x8lucas8x>
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
Software EngineerPassionate Hacker ArchLinux ZealotFOSS Enthusiast
Startups, Big Data,AI, NoSQL, Python,C++/Qt, Git, Groovy,Cloud, IoT, and allthat jazz.
Lucas Lira Gomes
3
linkedin.com/in/x8lucas8x
facebook.com/x8lucas8x
youtube.com/X80lucas08X
twitter.com/x8lucas8x
last.fm/user/x8lucas8x
github.com/x8lucas8x
GIT Anti-Patterns4
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...
Manual Merging
6
● Problems○ “To Err is Human”
■ Automate what you can○ Permission management
■ Higher friction○ Jeopardise collaboration
■ Less code-review
Manual Merging
7
A’sLocal
B’sLocal
C’sLocal
CentralRemote
Shared Repository
Manual Merging
8
A’sLocal
B’sLocal
CentralRemote
A’sRemote
B’sRemote
ForkPull
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://server_url/central/remote.git$ git remote set-url central --push "You shall NOT push!!!"
Copy-Paste Backup
12
$ git status# On branch master# ...# modified: index.html#...$ cp index.html backup_index.html...$ git checkout -- index.html...$ git checkout branchBSwitched to a new branch 'branchB'
Copy-Paste Backup
13
● Problems○ “To Err is Human”
■ Use proper tools
Copy-Paste Backup
14
● Solution○ Stash it
$ git stashSaved 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")
Copy-Paste Backup
15
● Solution○ Stash it
$ git status# On branch masternothing to commit, working directory clean
$ git stash liststash@{0}: WIP on master: 049d078 added the index file
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)
Dumb Bug Hunting
17
$ git commit...$ git commit...$ git commit...$ run app.py FATAL ERRORGonna cry?...$ diff HEAD~12...$ vim app.py...
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_SHA1_HASHBisecting: 6 revisions left to test after this[b047b02ea83310a70fd603dc8cd7a6cd13d15c04] secure this thing
Dumb Bug Hunting
20
● Solution○ Manually Bitsec
$ git bisect goodBisecting: 3 revisions left to test after this[b047b02ea83310a70fd603dc8cd7a6cd13d15c04] secure this thing
$ git bisect badBisecting: 1 revisions left to test after this[f71ce38690acf49c1f3c9bea38e09d82a5ce6014] drop exceptions table
Dumb Bug Hunting
21
● Solution○ Manually Bitsec
$ git bisect goodb047b02ea83310a70fd603dc8cd7a6cd13d15c04 is first bad commitcommit b047b02ea83310a70fd603dc8cd7a6cd13d15c04Author: User1 <[email protected]>Date: Tue Jan 27 14:48:32 2015 -0800...
Dumb Bug Hunting
22
● Solution○ Automate Bitsec
$ git bisect start BAD_SHA1_HASH GOOD_SHA1_HASH$ git bisect run make test arguments...
Dumb Conflict Resolution
23
$ git pull origin master...$ git status# On branch branchA# You have unmerged paths.# ...# both modified: README.md#...$ vim README.md...
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 mergetoolMerging the files: README
Normal merge conflict for 'README': {local}: modified {remote}: modified
Hit return to start merge resolution tool (kdiff3):
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/....$ git commit -a ‘Biggest commit ever!’...
Jumbo Commit
31
● Problems○ Long-term results
■ Team○ Review procrastination
■ Higher cognitive load○ Pulls are more prone to
conflict
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...# ! [rejected] master -> master (non-fast-forward)...
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' have diverged...
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 branchASwitched to a new branch 'branchA'
$ git branch -D master...
Divergent Master
40
● Solution○ Use master as a carbon copy
$ git branch -abranchAremotes/central/master
$ git checkout remotes/central/master...
Divergent Master
41
● Solution○ Use master as a carbon copy
$ git checkout -b masterSwitched 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
References1. https://www.git-scm.com/2. http://www.atlassian.com/git/tutorials/
45
How Not To GITLucas Gomes <@x8lucas8x>