git
play

Git Sbastien Cevey <seb@cine7.net> http://bytes.inso.cc/ - PowerPoint PPT Presentation

Git Sbastien Cevey <seb@cine7.net> http://bytes.inso.cc/ Purple Scout presentation (April 17, 2009) Swiss Federal Institute XMMS2 Music Player Playlouder MSP of Technology, Lausanne Outline 1. Introduction 2. Concepts & Local


  1. diff modifs $ git diff diff --git a/src/helloworld.c b/src/helloworld.c index e69de29..54a606d 100644 master --- a/src/helloworld.c +++ b/src/helloworld.c @@ -0,0 +1 @@ +/* End of fil */ Makefile README 54eda77 src |-- Makefile |-- helloworld.c <- edited `-- helloworld.h 04f087b 36

  2. commit $ git commit -a -m “Hasty commit” Created commit 3b5ba16: Hasty commit 1 files changed, 1 insertions(+), 0 deletions(-) master 3b5ba16 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 37

  3. rewind history $ git reset --hard HEAD^ # parent of HEAD HEAD is now at 54eda77 More stuff master $ git status # On branch master nothing to commit (working directory clean) Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 38

  4. rewind history $ git reset HEAD^ # parent of HEAD src/helloworld.c: locally modified master $ git diff diff --git a/src/helloworld.c b/src/helloworld.c index e69de29..54a606d 100644 --- a/src/helloworld.c +++ b/src/helloworld.c @@ -0,0 +1 @@ +/* End of fil */ Makefile README 54eda77 src |-- Makefile |-- helloworld.c <- edited `-- helloworld.h 04f087b 39

  5. new history $ $EDITOR src/helloworld.c $ git commit -a -m "Proper commit" master Created commit 0657c63: Proper commit 1 files changed, 1 insertions(+), 0 deletions(-) 0657c63 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 40

  6. tag $ git tag stable master stable 0657c63 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 41

  7. new branch $ git branch feature-X 54eda77 feature-X master $ git branch feature-X * master stable 0657c63 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 42

  8. switch branch $ git checkout feature-X Switched to branch "feature-X" feature-X master $ git branch * feature-X master stable 0657c63 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 43

  9. commit branch $ $EDITOR src/helloworld.c feature-X master $ git commit -a -m "Work on X" Created commit 6195849: Work on X 1 files changed, 1 insertions(+), 0 deletions(-) stable 0657c63 6195849 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 44

  10. commit branch $ $EDITOR README feature-X master $ git commit -a -m "Edit README for X" Created commit 20fbfdd: Edit README for X 1 files changed, 1 insertions(+), 0 deletions(-) 20fbfdd stable 0657c63 02bbf10 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 45

  11. diff branches $ git checkout master Switched to branch "master" feature-X master $ git diff feature-X diff --git a/README b/README index 29a6a68..579c5b1 100644 --- a/README +++ b/README @@ -1,2 +1 @@ Blah blah -foo [...] 1e4cfe4 stable 0657c63 02bbf10 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 46

  12. merge $ git merge feature-X Merge made by recursive. feature-X README | 1 + master src/helloworld.h | 1 + 2 files changed, 2 insertions(+), 0 deletions(-) 69e105 1e4cfe4 stable 0657c63 02bbf10 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 47

  13. view merge commit $ git show commit 69e105f3d1991c34d74d079197d5f1b1917d3120 feature-X Merge: 0657c63... 20fbfdd... master Author: Sebastien Cevey <seb@cine7.net> Date: Mon Apr 13 22:49:16 2009 +0200 Merge branch 'feature-X' 69e105 1e4cfe4 stable 0657c63 02bbf10 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 48

  14. undo $ git reset --hard stable # undo last merge HEAD is now at 0657c63 Proper commit feature-X master $ git checkout feature-X Switched to branch "feature-X" 20fbfdd stable 0657c63 02bbf10 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 49

  15. rebase $ git rebase master First, rewinding head to replay your work on top of it... feature-X Applying: Work on X master Applying: Edit README for X $ git show-branch * [feature-X] Edit README for X ! [master] Proper commit aeaf0a - - * [feature-X] Edit README for X * [feature-X^] Work on X *+ [master] Proper commit 2e309b stable 0657c63 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 50

  16. trivial merge $ git checkout master Switched to branch "master" feature-X master $ git merge feature-X Updating 0657c63..aeaf0a9 Fast forward README | 1 + src/helloworld.h | 1 + aeaf0a 2 files changed, 2 insertions(+), 0 deletions(-) 2e309b stable 0657c63 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 51

  17. git show-branch 52

  18. gitk 53

  19. gitk 54

  20. 3. Distributed Work 55

  21. public repository fix-segfault master D C E B public repo A 56

  22. clone fix-segfault master (over local filesystem, ssh://, git://, rsync://, ftp://etc) D C E fix-segfault B master public repo A $ git clone $URL Initialized empty Git repository in local/ copy/.git/ D C E B local repo A 57

  23. branch namespace fix-segfault master D C E origin/ fix-segfault B master master public repo A $ git branch --all *master origin/HEAD D origin/fix-segfault C E origin/master B local repo A 58

  24. public repository updated fix-segfault master D F C E origin/ fix-segfault B master master public repo A D C E B local repo A 59

  25. fetch update fix-segfault master D F C E origin/ fix-segfault B master master public repo A $ git fetch origin remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. D F remote: Total 3 (delta 0), reused 0 (delta 0) C E Unpacking objects: 100% (3/3), done. From /public/repo B E..F fix-segfault -> origin/fix-segfault local repo A 60

  26. checkout tracking branch fix-segfault master “topic branch” “tracking branches” D F C E origin/ fix-segfault B master master work public repo A $ git checkout -b work \ origin/fix-segfault D F Branch work set up to track remote branch refs/remotes/origin/fix-segfault. C E Switched to a new branch "work" B local repo A 61

  27. commit topic branch fix-segfault master D F origin/ fix-segfault C E master master work public repo B $ $EDITOR $ git commit -a -m "Another fix" G [...] D F C E local repo B 62

  28. push new commit fix-segfault master G D F origin/ fix-segfault C E master master work public repo B $ git push origin work:fix-segfault Total 0 (delta 0), reused 0 (delta 0) G To /public/repo F..G work -> feature-X D F C E local repo B 63

  29. public repository updated fix-segfault master H G origin/ fix-segfault D F master master work public repo C E G D F C E local repo B 64

  30. fetch update fix-segfault master H G origin/ fix-segfault D F master master work public repo C E $ git fetch origin [...] H From /public/repo G..H fix-segfault -> origin/fix-segfault G D F local repo C 65

  31. pull to merge update fix-segfault master H G origin/ fix-segfault D F master master work public repo C E $ git pull origin Updating G..H H Fast forward README | 1 + G 1 files changed, 1 insertions(+), 0 deletions(-) D F local repo C 66

  32. Basic multi-repo commands • git clone : init a copy of a repository • git fetch : sync remote repo R with local branches R/* • git pull : fetch + merge • git push <repo> [<local-branch>:<remote-branch>] 67

  33. master work pre-merge Real-life layout master dev/ master test buggy-crap dev 68 master experimental fix-segfault anna stable/ master master local repo fix-segfault fix-segfault stable

  34. master work pre-merge Real-life layout git remote add anna $URL master dev/ master test buggy-crap dev anna/ 69 master experimental fix-segfault anna stable/ master master local repo fix-segfault fix-segfault stable

  35. master work pre-merge Real-life layout master dev/ master test git fetch anna buggy-crap dev anna/ 70 master experimental master fix-segfault experimental fix-segfault anna stable/ master master local repo fix-segfault fix-segfault stable

  36. “just a repo, not in .git/” master git --bare init work mine pre-merge Real-life layout master dev/ master test buggy-crap dev anna/ 71 master experimental master fix-segfault experimental fix-segfault anna stable/ master master local repo fix-segfault fix-segfault stable

  37. master work mine pre-merge git remote add mine $URL Real-life layout master dev/ master test buggy-crap dev anna/ 72 master experimental master fix-segfault experimental fix-segfault anna stable/ master master local repo fix-segfault fix-segfault stable

  38. master master work pre-merge mine pre-merge Real-life layout mine/ master master git push mine work:master pre-merge test pre-merge buggy-crap dev 73 dev/ master master experimental anna/ fix-segfault anna master “remotes” experimental master fix-segfault local repo x fix-segfault stable stable/ master

  39. git remote • Help track remote (i.e. other) repositories • Pull from / push to them • Use tracking branches like any branch (merge, rebase, diff, log, show, etc) 74

  40. e.g. XMMS2 trees Official trees stable devel Developer public public public trees theefer tru Local trees Joe Sixpack tru anders theefer user maintainers developer 75

  41. e.g. XMMS2 trees stable devel olof’s server public public olof public theefer tru olof Joe Sixpack tru anders theefer contributer user maintainers developer 76

  42. 4. Advanced Features 77

  43. Between the working directory and the repository Repository Working Directory master IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 78

  44. The Index (or “Staging Area”) Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 79

  45. Committing using the index Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h git add/rm git commit 04f087b 80

  46. Bypassing the Index Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b git commit -a 81

  47. new file Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO <- new file 04f087b 82

  48. $ git status new file # On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # # modified: test.c # modified: TODO Index # no changes added to commit (use "git add" and/or "git commit -a") IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO <- new file 04f087b 83

  49. new file: add to index Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO <- new file IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO git add TODO 04f087b 84

  50. new file: add to index $ git status # On branch master # Changes to be committed: Index # (use "git reset HEAD <file>..." to unstage) # # modified: TODO IDEAS # Makefile Repository README # Changed but not updated: [...] Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO <- new file IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO git add TODO 04f087b 85

  51. new file: commit index Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO git commit 04f087b 86

  52. edited file Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile README <- edited src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO 04f087b 87

  53. edited file: update index Index IDEAS Makefile Repository README <- edited Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO git add README 04f087b 88

  54. edited file: commit index Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile README f4ec56 src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO git commit 04f087b 89

  55. edited file Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO IDEAS <- 3 hunks Makefile README f4ec56 src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO 04f087b 90

  56. add selected hunks to index Index IDEAS <- 1 hunk Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO IDEAS <- 2 hunks Makefile README f4ec56 src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef (also see git add --interactive) test.c <- new file TODO git add -p IDEAS 04f087b 91

  57. add selected hunks to index Index $ git add -p src/xmms/testclient.c [...] @@ -44,7 +44,7 @@ int main (int argc, char **argv) IDEAS <- 1 hunk Makefile Repository fd = xmms_ipc_transport_fd_get (transport); README Working src |-- Makefile - msg = xmms_ipc_msg_string_new (42, "korv!"); Directory master |-- helloworld.c + msg = xmms_ipc_msg_string_new (42, "apan!"); `-- helloworld.h TODO i = 5; IDEAS <- 2 hunks Stage this hunk [y/n/a/d/j/J/?]? Makefile README f4ec56 src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef (also see git add --interactive) test.c <- new file TODO git add -p IDEAS 04f087b 92

  58. diff’ing options Index IDEAS <- 1 hunk Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c git diff --cached `-- helloworld.h TODO IDEAS <- 2 hunks Makefile README f4ec56 src git diff |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO git diff HEAD 04f087b 93

  59. Conflicts: marked in index Index IDEAS <- conflicted Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO IDEAS <- merged Makefile README f4ec56 src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef TODO 04f087b 94

  60. Reset changes: 3 options Index IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile README f4ec56 src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef TODO 04f087b 95

  61. reset --hard reset the repository HEAD, the index Index and the working deadbeef directory. IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h deadbeef TODO IDEAS Makefile README f4ec56 src |-- Makefile |-- helloworld.c `-- helloworld.h git reset --hard deadbeef deadbeef TODO Warning: any uncommitted 04f087b change is lost! 96

  62. reset --soft only reset the repository HEAD. Index f4ec56 IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h f4ec56 TODO IDEAS Makefile README f4ec56 src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef git reset --soft deadbeef TODO Previous commit becomes “changes to be committed”. 04f087b 97

  63. reset --mixed (default) reset the repository HEAD and the Index index, but not the deadbeef working directory. IDEAS Makefile Repository README Working src |-- Makefile Directory master |-- helloworld.c `-- helloworld.h f4ec56 TODO IDEAS Makefile README f4ec56 src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef git reset --mixed deadbeef TODO Keep changes but not marked for commit. 04f087b 98

  64. Using the Index (or not) • “Invisible” area to prepare commits • Interact with: - working directory (add, rm) - repository (reset, commit) • Analyze state (status, diff) • Can usually be bypassed (using -a) • Warning: - Can commit files in inconsistent state (e.g. not tested) 99

  65. Git layers Source: http://osteele.com/archives/2008/05/commit-policies 100

Recommend


More recommend