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

Git for beginners

215e0166d4d8265395c5d9076da73c70?s=47 Mike McQuaid
February 29, 2012

Git for beginners

An overview of the essentials required to use Git.
Presented at DunDDD, RIM and ConFoo.ca in 2012.

215e0166d4d8265395c5d9076da73c70?s=128

Mike McQuaid

February 29, 2012
Tweet

Transcript

  1. git for beginners Mike McQuaid

  2. me

  3. @MikeMcQuaid mike@mikemcquaid.com http://mikemcquaid.com

  4. install create commit history remote fix branch rewrite tools help

  5. install

  6.  Xcode 4 or brew install git

  7. apt-get install git-core

  8.  msysgit

  9. git config

  10. $ git config --global user.name "Your Name" $ git config

    --global user.email me@example.com
  11. create

  12. git init

  13. $ git init test Initialized empty Git repository in /Users/you/test/.git/

  14. git status

  15. $ git status # On branch master # # Initial

    commit # nothing to commit (create/copy files and use "git add" to track)
  16. commit

  17. git add

  18. $ git status # On branch master # # Initial

    commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # ReadMe.txt nothing added to commit but untracked files present (use "git add" to track)
  19. $ git add ReadMe.txt

  20. $ git status # On branch master # # Initial

    commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: ReadMe.txt #
  21. None
  22. git commit

  23. $ git commit -m "Initial commit" [master (root-commit) ba9d588] Initial

    commit 0 files changed create mode 100644 ReadMe.txt
  24. history

  25. git log

  26. $ git log commit ba9d5884d2ea8946e9bf3b6c151d61c8cd952cb1 Author: Your Name <me@example.com> Date:

    Mon Feb 27 21:32:41 2012 +0000 Initial commit
  27. gitx gitk

  28. None
  29. git diff

  30. $ git status # On branch master # Changes not

    staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: ReadMe.txt # no changes added to commit (use "git add" and/or "git commit -a")
  31. $ git diff diff --git a/ReadMe.txt b/ReadMe.txt index e69de29..cd08755 100644

    --- a/ReadMe.txt +++ b/ReadMe.txt @@ -0,0 +1 @@ +Hello world!
  32. $ git status # On branch master # Changes not

    staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: ReadMe.txt # no changes added to commit (use "git add" and/or "git commit -a")
  33. $ git add ReadMe.txt

  34. $ git diff

  35. $ git diff --staged diff --git a/ReadMe.txt b/ReadMe.txt index e69de29..cd08755

    100644 --- a/ReadMe.txt +++ b/ReadMe.txt @@ -0,0 +1 @@ +Hello world!
  36. $ git diff master diff --git a/ReadMe.txt b/ReadMe.txt index e69de29..cd08755

    100644 --- a/ReadMe.txt +++ b/ReadMe.txt @@ -0,0 +1 @@ +Hello world!
  37. $ git diff HEAD diff --git a/ReadMe.txt b/ReadMe.txt index e69de29..cd08755

    100644 --- a/ReadMe.txt +++ b/ReadMe.txt @@ -0,0 +1 @@ +Hello world!
  38. $ git commit -m "Add ReadMe" [master ffc8706] Add ReadMe

    1 file changed, 1 insertion(+)
  39. $ git log commit ffc8706e41abb387993c294d7d46070a4bbd76ba Author: Your Name <me@example.com> Date:

    Mon Feb 27 21:38:38 2012 +0000 Add ReadMe commit ba9d5884d2ea8946e9bf3b6c151d61c8cd952cb1 Author: Your Name <me@example.com> Date: Mon Feb 27 21:32:41 2012 +0000 Initial commit
  40. None
  41. remote

  42. git clone

  43. $ git clone git://github.com/mxcl/homebrew.git Cloning into 'homebrew'... remote: Counting objects:

    58026, done. remote: Compressing objects: 100% (24408/24408), done. remote: Total 58026 (delta 37820), reused 50148 (delta 32895) Receiving objects: 100% (58026/58026), 8.69 MiB | 59 KiB/s, done. Resolving deltas: 100% (37820/37820), done.
  44. $ git log --stat 29d85 commit 29d85578e75170a6c0eaebda4d701b46f1acf446 Author: Max Howell

    <max@methylblue.com> Date: Thu May 21 00:04:11 2009 +0100 I'll start with a rare Belgian yeast and Sussex hops README | 137 ++++++++++++++++++++++++++ 1 file changed, 137 insertions(+)
  45. git push

  46. $ git push Counting objects: 9, done. Delta compression using

    up to 8 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (5/5), 485 bytes, done. Total 5 (delta 4), reused 0 (delta 0) To git@github.com:mxcl/homebrew.git 4ab1f7a..753dde9 master -> master
  47. git fetch

  48. $ git fetch remote: Counting objects: 140, done. remote: Compressing

    objects: 100% (65/65), done. remote: Total 93 (delta 79), reused 42 (delta 28) Unpacking objects: 100% (93/93), done. From git://github.com/mxcl/homebrew 753dde9..3c180ba master -> origin/master
  49. git pull

  50. git pull = git fetch && git merge

  51. git pull --rebase = git fetch && git rebase

  52. $ git pull remote: Counting objects: 9, done. remote: Compressing

    objects: 100% (1/1), done. remote: Total 5 (delta 4), reused 5 (delta 4) Unpacking objects: 100% (5/5), done. From git://github.com/mxcl/homebrew 4ab1f7a..753dde9 master -> origin/master First, rewinding head to replay your work on top of it... Fast-forwarded master to 753dde9c2cf66848183f3f787f8501dbf5a7e28f.
  53. None
  54. fix

  55. git mv

  56. $ git mv ReadMe.txt ReadMe.md

  57. $ git status # On branch master # Changes to

    be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: ReadMe.txt -> ReadMe.md #
  58. $ git commit -m "Markdown ReadMe" [master a385898] Markdown ReadMe

    1 file changed, 0 insertions(+), 0 deletions(-) rename ReadMe.txt => ReadMe.md (100%)
  59. git rm

  60. $ git rm ReadMe.md rm 'ReadMe.md'

  61. $ git status # On branch master # Changes to

    be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: ReadMe.md #
  62. $ git commit -m "Delete ReadMe" [master d84ef55] Delete ReadMe

    1 file changed, 1 deletion(-) delete mode 100644 ReadMe.md
  63. None
  64. git revert

  65. $ git revert d84ef55 [master f286fb4] Revert "Delete ReadMe" 1

    file changed, 1 insertion(+) create mode 100644 ReadMe.md
  66. $ git log HEAD~2..HEAD commit f286fb49f622b1292e8560b5c02bb8a33a115221 Author: Your Name <me@example.com>

    Date: Mon Feb 27 21:49:16 2012 +0000 Revert "Delete ReadMe" This reverts commit d84ef5556bdbd952030d19449ac6cf4af7a1b975. commit d84ef5556bdbd952030d19449ac6cf4af7a1b975 Author: Your Name <me@example.com> Date: Mon Feb 27 21:48:53 2012 +0000 Delete ReadMe
  67. None
  68. git clean

  69. $ git status # On branch master # Untracked files:

    # (use "git add <file>..." to include in what will be committed) # # ReadMe.old nothing added to commit but untracked files present (use "git add" to track)
  70. $ git clean -f Removing ReadMe.old

  71. $ git status # On branch master nothing to commit

    (working directory clean)
  72. branch

  73. git branch

  74. $ git branch * master

  75. $ git branch testing

  76. $ git branch * master testing

  77. git checkout

  78. $ git checkout testing Switched to branch 'testing'

  79. $ git branch master * testing

  80. $ git add ReadMe.md

  81. $ git commit -m "Add header" [testing 85200e2] Add header

    1 file changed, 1 insertion(+)
  82. git merge

  83. $ git checkout master Switched to branch 'master'

  84. $ git diff testing diff --git a/ReadMe.md b/ReadMe.md index 717157d..cd08755

    100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,2 +1 @@ Hello world! -============
  85. $ git add License.md

  86. $ git commit -m "Add license" [master 8e7d2b6] Add license

    1 file changed, 1 insertion(+) create mode 100644 License.md
  87. $ git diff testing diff --git a/License.md b/License.md new file

    mode 100644 index 0000000..85265b0 --- /dev/null +++ b/License.md @@ -0,0 +1 @@ +Public domain diff --git a/ReadMe.md b/ReadMe.md index 717157d..cd08755 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,2 +1 @@ Hello world! -============
  88. None
  89. $ git merge testing Merge made by the 'recursive' strategy.

    ReadMe.md | 1 + 1 file changed, 1 insertion(+)
  90. None
  91. $ git branch -d testing Deleted branch testing (was 85200e2).

  92. None
  93. git tag

  94. $ git tag v0.1

  95. None
  96. git stash

  97. $ git diff diff --git a/License.md b/License.md index 85265b0..a22a2da 100644

    --- a/License.md +++ b/License.md @@ -1 +1 @@ -Public domain +MIT
  98. $ git stash Saved working directory and index state WIP

    on master: d13413e Merge branch 'testing' HEAD is now at d13413e Merge branch 'testing'
  99. $ git diff

  100. None
  101. $ git stash pop # On branch master # Changes

    not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: License.md # no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (a3b0d391ffa40e7483e8c1194c05e628dc13f2fe)
  102. $ git diff diff --git a/License.md b/License.md index 85265b0..a22a2da 100644

    --- a/License.md +++ b/License.md @@ -1 +1 @@ -Public domain +MIT
  103. None
  104. $ git commit -m "Change license" [master 836f9b7] Change license

    1 file changed, 1 insertion(+), 1 deletion(-)
  105. rewrite

  106. git reset

  107. None
  108. $ git reset HEAD^ Unstaged changes after reset: M License.md

  109. $ git diff diff --git a/License.md b/License.md index 85265b0..a22a2da 100644

    --- a/License.md +++ b/License.md @@ -1 +1 @@ -Public domain +MIT
  110. None
  111. $ git commit -m "Change license" [master 6459532] Change license

    1 file changed, 1 insertion(+), 1 deletion(-)
  112. None
  113. $ git reset --hard HEAD^ HEAD is now at d13413e

    Merge branch 'testing'
  114. None
  115. $ git diff

  116. git reflog

  117. $ git reflog d13413e HEAD@{0}: reset: moving to HEAD^ 6459532

    HEAD@{1}: commit: Change license d13413e HEAD@{2}: reset: moving to HEAD^ 836f9b7 HEAD@{3}: commit: Change license d13413e HEAD@{4}: merge testing: Merge made by the 'recursive' strategy.
  118. None
  119. $ git reset --hard 6459532 HEAD is now at 6459532

    Change license
  120. None
  121. git rebase

  122. $ git checkout -b testing Switched to a new branch

    'testing'
  123. $ git reset --hard 85200e2 HEAD is now at 85200e2

    Add header
  124. $ git add ReadMe.md

  125. $ git commit -m "First point" [testing 9fa6296] First point

    1 file changed, 1 insertion(+)
  126. None
  127. $ git rebase master First, rewinding head to replay your

    work on top of it... Applying: First point
  128. None
  129. tools

  130. git describe

  131. $ git describe --tags v0.1-2-g18a2fde

  132. git blame

  133. $ git blame ReadMe.md f286fb49 (Your Name 2012-02-27 21:49:16 +0000

    1) Hello world! 85200e28 (Your Name 2012-02-28 14:30:47 +0000 2) ============ 18a2fded (Your Name 2012-02-28 12:03:40 -0500 3) * First point
  134. git svn

  135. git bisect

  136. GitHub

  137. git help

  138. $ git help diff

  139. $ git diff --help

  140. http://progit.org http://help.github.com

  141. install create commit history remote fix branch rewrite tools help

  142. https://github.com/ mikemcquaid/ GitForBeginnersDemo

  143. @MikeMcQuaid mike@mikemcquaid.com http://mikemcquaid.com

  144. questions?