Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git in a Nutshell

Dan Chen
December 13, 2011

Git in a Nutshell

從 Git 開始,進入版本控制的世界
2011/12/14 @ 師大資工系 B1 演講廳

採 CC BY-NC 3.0 授權,歡迎散發 :)

-
Git in a Nutshell
2011/12/14 @ B1 Lecture Room, Dept. of CSIE, NTNU

Feel free to distribute under CC BY-NC 3.0 :)

Dan Chen

December 13, 2011
Tweet

More Decks by Dan Chen

Other Decks in Technology

Transcript

  1. [/tmp/foo] $ ls -lah total 12K drwxr-xr-x+ 1 Dan None

    0 Feb 17 11:46 . drwxrwxrwt+ 1 Dan root 8.0K Feb 17 11:46 .. drwxr-xr-x+ 1 Dan None 4.0K Feb 17 11:19 .git -rw-r--r-- 1 Dan None 0 Feb 17 11:46 Hello.txt -rw-r--r-- 1 Dan None 0 Feb 17 11:46 README .git/
  2. [/tmp/foo] $ ls -lh .git -rw-r--r-- 1 Dan None 111

    Feb 17 11:19 config -rw-r--r-- 1 Dan None 73 Feb 17 11:19 description -rw-r--r-- 1 Dan None 23 Feb 17 11:19 HEAD drwxr-xr-x+ 1 Dan None 4.0K Feb 17 11:19 hooks drwxr-xr-x+ 1 Dan None 0 Feb 17 11:19 info drwxr-xr-x+ 1 Dan None 0 Feb 17 11:19 objects drwxr-xr-x+ 1 Dan None 0 Feb 17 11:19 refs
  3. [/tmp/foo] $ git status # On branch master # Changed

    but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # modified: README # # Untracked files: # (use "git add <file>..." to include in what will be committed) # Hello no changes added to commit (use "git add" and/or "git commit -a")
  4. [/tmp/foo] $ git status # On branch master # Changed

    but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # modified: README # # Untracked files: # (use "git add <file>..." to include in what will be committed) # Hello no changes added to commit (use "git add" and/or "git commit -a")
  5. [/tmp/foo] $ git status # On branch master # Changed

    but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # modified: README # # Untracked files: # (use "git add <file>..." to include in what will be committed) # Hello no changes added to commit (use "git add" and/or "git commit -a")
  6. [/tmp/foo] $ git status # On branch master # Changed

    but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # modified: README # # Untracked files: # (use "git add <file>..." to include in what will be committed) # Hello no changes added to commit (use "git add" and/or "git commit -a") [/tmp/foo] $ git add .
  7. [/tmp/foo] $ git status # On branch master # Changed

    but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # modified: README # # Untracked files: # (use "git add <file>..." to include in what will be committed) # Hello no changes added to commit (use "git add" and/or "git commit -a") [/tmp/foo] $ git add . [/tmp/foo] $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # new file: Hello # modified: README
  8. [/tmp/foo] $ git status # On branch master # Changed

    but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # modified: README # # Untracked files: # (use "git add <file>..." to include in what will be committed) # Hello no changes added to commit (use "git add" and/or "git commit -a") [/tmp/foo] $ git add . [/tmp/foo] $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # new file: Hello # modified: README git commit
  9. [/tmp/foo] $ git add . [/tmp/foo] $ git status #

    On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # new file: Hello # modified: README [/tmp/foo] $ git commit
  10. [/tmp/foo] $ git add . [/tmp/foo] $ git status #

    On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # new file: Hello # modified: README [/tmp/foo] $ git commit -m 'add hello, refactor README'
  11. [/tmp/foo] $ git add . [/tmp/foo] $ git status #

    On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # new file: Hello # modified: README [/tmp/foo] $ git commit -m 'add hello, refactor README' [master 356bbef] add hello, refactor README 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 Hello [/tmp/foo] $ git status # On branch master nothing to commit (working directory clean)
  12. [/tmp/git] $ git log commit 5673d695fcce217b26d1a5956c1184ff62dc74f1 Author: Junio C Hamano

    <[email protected]> Date: Wed Feb 16 14:33:22 2011 -0800 Merge branch 'maint' * maint: parse_tag_buffer(): do not prefixcmp() out of range commit 759e84f07fd0fba2f3466b11b74146173d42cb6b Author: Junio C Hamano <[email protected]> Date: Wed Feb 16 14:33:11 2011 -0800 Merge branch 'maint-1.7.3' into maint * maint-1.7.3:
  13. [/tmp/git] $ git log commit 5673d695fcce217b26d1a5956c1184ff62dc74f1 Author: Junio C Hamano

    <[email protected]> Date: Wed Feb 16 14:33:22 2011 -0800 Merge branch 'maint' * maint: parse_tag_buffer(): do not prefixcmp() out of range commit 759e84f07fd0fba2f3466b11b74146173d42cb6b Author: Junio C Hamano <[email protected]> Date: Wed Feb 16 14:33:11 2011 -0800 Merge branch 'maint-1.7.3' into maint * maint-1.7.3:
  14. [/tmp/git] $ git log commit 5673d695fcce217b26d1a5956c1184ff62dc74f1 Author: Junio C Hamano

    <[email protected]> Date: Wed Feb 16 14:33:22 2011 -0800 Merge branch 'maint' * maint: parse_tag_buffer(): do not prefixcmp() out of range commit 759e84f07fd0fba2f3466b11b74146173d42cb6b Author: Junio C Hamano <[email protected]> Date: Wed Feb 16 14:33:11 2011 -0800 Merge branch 'maint-1.7.3' into maint * maint-1.7.3:
  15. [/tmp/git] $ git log commit 5673d695fcce217b26d1a5956c1184ff62dc74f1 Author: Junio C Hamano

    <[email protected]> Date: Wed Feb 16 14:33:22 2011 -0800 Merge branch 'maint' * maint: parse_tag_buffer(): do not prefixcmp() out of range commit 759e84f07fd0fba2f3466b11b74146173d42cb6b Author: Junio C Hamano <[email protected]> Date: Wed Feb 16 14:33:11 2011 -0800 Merge branch 'maint-1.7.3' into maint * maint-1.7.3:
  16. [/tmp/git] $ git log commit 5673d695fcce217b26d1a5956c1184ff62dc74f1 Author: Junio C Hamano

    <[email protected]> Date: Wed Feb 16 14:33:22 2011 -0800 Merge branch 'maint' * maint: parse_tag_buffer(): do not prefixcmp() out of range commit 759e84f07fd0fba2f3466b11b74146173d42cb6b Author: Junio C Hamano <[email protected]> Date: Wed Feb 16 14:33:11 2011 -0800 Merge branch 'maint-1.7.3' into maint * maint-1.7.3:
  17. [/tmp/git] $ git log commit 5673d695fcce217b26d1a5956c1184ff62dc74f1 Author: Junio C Hamano

    <[email protected]> Date: Wed Feb 16 14:33:22 2011 -0800 Merge branch 'maint' * maint: parse_tag_buffer(): do not prefixcmp() out of range commit 759e84f07fd0fba2f3466b11b74146173d42cb6b Author: Junio C Hamano <[email protected]> Date: Wed Feb 16 14:33:11 2011 -0800 Merge branch 'maint-1.7.3' into maint * maint-1.7.3:
  18. [/tmp/git] $ git show HEAD commit ebbc1b128825df6f7500f5e34836390b24ca4966 Author: Dannvix Chen

    <[email protected]> Date: Thu Feb 17 14:15:23 2011 +0800 improve README user experience diff --git a/README b/README index edb17cc..a40afab 100644 --- a/README +++ b/README @@ -1 +1,2 @@ Git test +Balah balah
  19. [/tmp/git] $ $ git blame git.c 85023577 (Junio C Hamano

    2006-12-19 14:34:12 -0800 1) #include "builtin.h" 2b11e317 (Johannes Schindel 2006-06-05 19:43:52 +0200 2) #include "cache.h" fd5c363d (Thiago Farina 2010-08-31 23:29:08 -0300 3) #include "exec_cmd.h" fd5c363d (Thiago Farina 2010-08-31 23:29:08 -0300 4) #include "help.h" 575ba9d6 (Matthias Lederhof 2006-06-25 15:56:18 +0200 5) #include "quote.h" d8e96fd8 (Jeff King 2009-01-28 02:38:14 -0500 6) #include "run-command.h" 8e49d503 (Andreas Ericsson 2005-11-16 00:31:25 +0100 7) 4e10738a (Jeff King 2008-07-03 07:46:57 -0400 19) static int use_pager=-1; 4e10738a (Jeff King 2008-07-03 07:46:57 -0400 20) struct pager_config { 4e10738a (Jeff King 2008-07-03 07:46:57 -0400 21) const char *cmd; 9bad7233 (Jeff King 2010-11-17 12:04:12 -0500 22) int want; 9bad7233 (Jeff King 2010-11-17 12:04:12 -0500 23) char *value; 4e10738a (Jeff King 2008-07-03 07:46:57 -0400 24) }; 4e10738a (Jeff King 2008-07-03 07:46:57 -0400 25)
  20. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea
  21. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea git branch newidea git checkout newidea
  22. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea [/tmp/foo] $ … vim newidea … [/tmp/foo] $ git commit –a –m 'improve README user experience'
  23. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea [/tmp/foo] $ … vim newidea … [/tmp/foo] $ git commit –a –m 'improve README user experience' [/tmp/foo] $ git checkout master [/tmp/foo] $ git merge newidea Updating 356bbef..ebbc1b1 Fast-forward README | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
  24. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea [/tmp/foo] $ … vim newidea … [/tmp/foo] $ git commit –a –m 'improve README user experience' [/tmp/foo] $ git checkout master [/tmp/foo] $ git merge newidea Updating 356bbef..ebbc1b1 Fast-forward README | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
  25. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea [/tmp/foo] $ … vim newidea … [/tmp/foo] $ git commit –a –m 'improve README user experience' [/tmp/foo] $ git checkout master [/tmp/foo] $ git merge newidea Updating 356bbef..ebbc1b1 Fast-forward README | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
  26. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea [/tmp/foo] $ … vim newidea … [/tmp/foo] $ git commit –a –m 'improve README user experience' [/tmp/foo] $ git checkout master [/tmp/foo] $ git merge newidea Updating 356bbef..ebbc1b1 Fast-forward README | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
  27. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea [/tmp/foo] $ … vim newidea … [/tmp/foo] $ git commit –a –m 'improve README user experience' [/tmp/foo] $ git checkout master [/tmp/foo] $ git merge newidea Updating 356bbef..ebbc1b1 Fast-forward README | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
  28. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea [/tmp/foo] $ … vim newidea … [/tmp/foo] $ git commit –a –m 'improve README user experience' [/tmp/foo] $ git checkout master [/tmp/foo] $ git merge newidea Updating 356bbef..ebbc1b1 Fast-forward README | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
  29. [/tmp/foo] $ git branch * master [/tmp/foo] $ git checkout

    –b newidea Switched to a new branch 'newidea' [/tmp/foo] $ git branch master * newidea [/tmp/foo] $ … vim newidea … [/tmp/foo] $ git commit –a –m 'improve README user experience‘ [/tmp/foo] $ git checkout master [/tmp/foo] $ git merge newidea Updating 356bbef..ebbc1b1 Fast-forward README | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) [/tmp/foo] $ git branch –d newidea
  30. [/tmp/foo] $ git clone git://git.kernel.org/pub/scm/git/git.git Initialized empty Git repository in

    /tmp/git/.git/ remote: Counting objects: 133887, done. remote: Compressing objects: 100% (32547/32547), done. remote: Total 133887 (delta 100281), reused 132921 (delta 99480) Receiving objects: 100% (133887/133887), 27.30 MiB | 76 KiB/s, done. Resolving deltas: 100% (100281/100281), done. [/tmp/foo] $ git push origin master
  31. [/tmp/foo] $ git clone git://git.kernel.org/pub/scm/git/git.git Initialized empty Git repository in

    /tmp/git/.git/ remote: Counting objects: 133887, done. remote: Compressing objects: 100% (32547/32547), done. remote: Total 133887 (delta 100281), reused 132921 (delta 99480) Receiving objects: 100% (133887/133887), 27.30 MiB | 76 KiB/s, done. Resolving deltas: 100% (100281/100281), done. [/tmp/foo] $ git push origin master
  32. • – git config – git init – git add

    – git commit – git status – git log – git tag • – git checkout – git branch – git merge – git rebase • – git remove – git fetch – git pull – git clone – git push • – git diff – git apply – git format-patch – git am
  33. [~/Dropbox/Repos] $ mkdir test.git [~/Dropbox/Repos] $ cd test.git [~/Dropbox/Repos/test.git] $

    git init --bare Initialized empty Git repository in ~/Dropbox/Repos/test.git/ [~/Dropbox/Repos/test.git] $ cd /tmp [~/Code] $ git clone file:///Users/Dan/Dropbox/Repos/test.git
  34. [~/Dropbox/Repos] $ mkdir test.git [~/Dropbox/Repos] $ cd test.git [~/Dropbox/Repos/test.git] $

    git init --bare Initialized empty Git repository in ~/Dropbox/Repos/test.git/ [~/Dropbox/Repos/test.git] $ cd /tmp [~/Code] $ git clone file:///Users/Dan/Dropbox/Repos/test.git [~/Code] $ cd test [~/Code/test] $ touch README [~/Code/test] $ git add . [~/Code/test] $ git commit –m 'initial commit‘ [master (root-commit) 17d28f0] initial commit 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 README
  35. [~/Dropbox/Repos] $ mkdir test.git [~/Dropbox/Repos] $ cd test.git [~/Dropbox/Repos/test.git] $

    git init --bare Initialized empty Git repository in ~/Dropbox/Repos/test.git/ [~/Dropbox/Repos/test.git] $ cd /tmp [~/Code] $ git clone file:///Users/Dan/Dropbox/Repos/test.git [~/Code] $ cd test [~/Code/test] $ touch README [~/Code/test] $ git add . [~/Code/test] $ git commit –m 'initial commit‘ [master (root-commit) 17d28f0] initial commit 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 README [~/Code/test] $ git push origin master