0
Git
                          Sébastien Cevey <seb@cine7.net>
                                    http://bytes.inso.cc/

 ...
Outline

1. Introduction
2. Concepts & Local Workflow
3. Distributed Work
4. Advanced Features
5. Conclusion



           ...
1. Introduction



       3
Version Control Systems

• Record history
• Facilitate team work
• Expose source code
• Branch/merge development paths

  ...
Centralized VCS
                                        permissions
Master                                  branches
     ...
Distributed VCS
                                                                Repository
                               ...
Centralized vs Distributed
•                            •
    synchronous                  asynchronous
    - unique refer...
Centralized vs Distributed
•                            •
    synchronous                  asynchronous
    - unique refer...
Git History
• Linus Torvalds needed a replacement for
  BitKeeper to maintain the Linux kernel
• Requirements:
   • Distri...
Why “Git” ?
git (plural gits)
      2. (British, slang, pejorative) A silly,
      incompetent, stupid, annoying or childi...
Git vs. other DVCS
    (Mercurial, Darcs, Bazaar, Monotone, etc)

+ Fast, scalable (execution and setup)
+ Powerful small ...
Git vs. other DVCS
    (Mercurial, Darcs, Bazaar, Monotone, etc)

+ Fast, scalable (execution and setup)
+ Powerful small ...
Why Git is better than X?
 • Cheap Local Branching
 • Everything is Local
 • Git is Fast
 • Git is Small
 • The Staging Ar...
2. Concepts
       &
Local Workflow


      14
Git Repository
                repository = object database




.git/objects/
                             15
Git Repository
                id(object) = SHA1(object content)



                 335e   a23f   6679


                ...
Git Repository
content is stored in blob objects
                 File

                                                  ...
Git Repository
structure is stored in tree objects
               Directory

                                             ...
Git Repository
                          history is stored in commit objects
                           Authenticated hier...
Git Repository
references are stored as tag objects
         (Signed) symbolic link

                                     ...
Git Repository
practical repository
entry points                                                    point to a
           ...
History Graph
                            branches




                                       feature-X
                  ...
Git workflow
$ find
.
./src
./src/Makefile
./src/helloworld.c
./src/helloworld.h
./Makefile
./IDEAS
./README




IDEAS
Makefil...
initialize
$ git init
Initialized empty Git repository in /path/example/.git/




IDEAS
Makefile
README
src
|-- Makefile
|...
check status
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use quot;git add <file>...quot; to i...
add files
$ git add .




IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h

                      26
check status
$ git status
#   On branch master
#
#   Initial commit
#
#   Changes to be committed:
#    (use quot;git rm -...
commit
$ git commit -a -m quot;First importquot;
Created initial commit 04f087b: First import
0 files changed, 0 insertions...
hack
$ echo quot;Blah blahquot; >> README




                                       master
$ rm IDEAS




Makefile
README...
check status
$ git status
# On branch master
# Changed but not updated:




                                              ...
commit
$ git commit -a -m quot;More stuffquot;
Created commit 54eda77: More stuff
1 files changed, 1 insertions(+), 0 delet...
view commit
$ git show
commit 54eda77a85b0e5d1891653a36ff08faf133df030
Author: Sebastien Cevey <seb@cine7.net>




       ...
view commit
$ git show --pretty=raw
commit 54eda77a85b0e5d1891653a36ff08faf133df030
tree 524f0cfe293064d18ac0c12a55baaf2b3...
view history
$ git log
commit 54eda77a85b0e5d1891653a36ff08faf133df030
Author: Sebastien Cevey <seb@cine7.net>




       ...
hack again
$ echo quot;/* End of fil */quot; >> src/helloworld.c




                                                master...
diff modifs
$ git diff
diff --git a/src/helloworld.c b/src/helloworld.c
index e69de29..54a606d 100644




                ...
commit
$ git commit -a -m “Hasty commit”
Created commit 3b5ba16: Hasty commit
1 files changed, 1 insertions(+), 0 deletions...
rewind history
$ git reset --hard HEAD^ # parent of HEAD
HEAD is now at 54eda77 More stuff




                           ...
rewind history
$ git reset HEAD^ # parent of HEAD
src/helloworld.c: locally modified




                                  ...
new history
$ $EDITOR src/helloworld.c

$ git commit -a -m quot;Proper commitquot;




                                   ...
tag
$ git tag stable




                                   master
                         stable   0657c63

Makefile
REA...
new branch
$ git branch feature-X 54eda77




                                                         feature-X
         ...
switch branch
$ git checkout feature-X
Switched to branch quot;feature-Xquot;




                                        ...
commit branch
$ $EDITOR src/helloworld.c




                                                                           fe...
commit branch
$ $EDITOR README




                                                                           feature-X
  ...
diff branches
$ git checkout master
Switched to branch quot;masterquot;




                                              ...
merge
$ git merge feature-X
Merge made by recursive.




                                                                 ...
view merge commit
$ git show
commit 69e105f3d1991c34d74d079197d5f1b1917d3120




                                         ...
undo
$ git reset --hard stable # undo last merge
HEAD is now at 0657c63 Proper commit




                                ...
rebase
$ git rebase master
First, rewinding head to replay your work on top of it...




                                 ...
trivial merge
$ git checkout master
Switched to branch quot;masterquot;




                                              ...
git show-branch




       52
gitk




 53
gitk




 54
3. Distributed Work



         55
public repository




                              t
                             aul
                    r
             ...
clone




                                                                      t
                                        ...
branch namespace




                                                               t
                                    ...
public repository updated




                                                      t
                                    ...
fetch update




                                                             t
                                          ...
checkout tracking branch




                                                                             t
              ...
commit topic branch




                                                                        t
                        ...
push new commit




                                                                        t
                            ...
public repository updated




                                                            t
                              ...
fetch update




                                                                     t
                                  ...
pull to merge update




                                                                    t
                           ...
Basic multi-repo commands
• git clone: init a copy of a repository
• git fetch: sync remote repo R with
              loca...
ma
                        ste
                              r

                    wo
                       rk

        ...
ma
                        ste
                              r

                    wo
                       rk

        ...
ma
                           ste
                                 r

                        wo
                         ...
ma
                           ste
                                 r
                                                     ...
ma
                           ste
                                 r

                        wo
                         ...
ma
                           ste
                                 r
                                                     ...
git remote

• Help track remote (i.e. other) repositories
• Pull from / push to them
• Use tracking branches like any bran...
e.g. XMMS2 trees
Official trees             stable              devel




Developer                                        ...
e.g. XMMS2 trees
                          stable              devel


olof’s server

   public
                          ...
4. Advanced Features



         77
Between the working
directory and the repository
                        Repository
  Working




                        ...
The Index (or “Staging Area”)
                             Index
                   IDEAS
                   Makefile
    ...
Committing using the index
                                 Index
                   IDEAS
                   Makefile
   ...
Bypassing the Index
                             Index
                   IDEAS
                   Makefile
              ...
new file
                               Index
                     IDEAS
                     Makefile
                    ...
$ git status


                     new file
                                        # On branch master
                   ...
new file: add to index
                                Index
                     IDEAS
                     Makefile
     ...
new file: add to index
                                          $ git status
                                          #  ...
new file: commit index
                               Index
                     IDEAS
                     Makefile
      ...
edited file
                                Index
                      IDEAS
                      Makefile
              ...
edited file: update index
                                 Index
                      IDEAS
                      Makefile...
edited file: commit index
                               Index
                     IDEAS
                     Makefile
   ...
edited file
                                 Index
                       IDEAS
                       Makefile
           ...
add selected hunks to index
                                    Index
                       IDEAS    <- 1 hunk
          ...
add selected hunks to index
                                                   Index
    $ git add -p src/xmms/testclient....
diff’ing options
                                           Index
                                 IDEAS    <- 1 hunk
    ...
Conflicts: marked in index
                               Index
                     IDEAS    <- conflicted
               ...
Reset changes: 3 options
                             Index
                   IDEAS
                   Makefile
         ...
reset --hard
  reset the repository
  HEAD, the index
                                       Index
  and the working      ...
reset --soft
 only reset the
 repository HEAD.
                                    Index
                       f4ec56
   ...
reset --mixed (default)
 reset the repository
 HEAD and the
                                        Index
 index, but not ...
Using the Index (or not)
• “Invisible” area to prepare commits
• Interact with:
  - working directory (add, rm)
  - reposi...
Git layers




  Source: http://osteele.com/archives/2008/05/commit-policies
         100
Rewrite history!
• Changing the history = point to a new history
• Correct mistakes
• Cleanup messy iterative development
...
Rewrite history! (1)
• git commit --amend




                                                 r
                         ...
Rewrite history! (1)
• git commit --amend




                                             r
                             ...
Rewrite history! (2)
• git rebase --interactive




                                          r
                          ...
Rewrite history! (2)
• git rebase --interactive




                                          r
                          ...
Rewrite history! (3)
• git filter-branch
• Rewrite history using functions/scripts
• Edit author infos, commit message,
  d...
git bisect
• Useful to find the commit that
  introduces a problem




                                           r
       ...
git bisect
• Useful to find the commit that
  introduces a problem




                                           r
       ...
git bisect
• Useful to find the commit that
  introduces a problem




                                           r
       ...
git bisect
• Useful to find the commit that
  introduces a problem




                                           r
       ...
git blame
• git blame D <file>




                                   r
                                ste
               ...
git stash
• git stash [save [message]]




                                          r
                                   ...
git stash
• git stash [save [message]]




                                               {0}
                            ...
git stash
• git stash [save [message]]




                                               {0}
                            ...
git stash
• git stash [save [message]]




                                                   {0}
                        ...
Git Presentation - Purple Scout AB Malmö
Git Presentation - Purple Scout AB Malmö
Git Presentation - Purple Scout AB Malmö
Git Presentation - Purple Scout AB Malmö
Git Presentation - Purple Scout AB Malmö
Git Presentation - Purple Scout AB Malmö
Git Presentation - Purple Scout AB Malmö
Git Presentation - Purple Scout AB Malmö
Git Presentation - Purple Scout AB Malmö
Git Presentation - Purple Scout AB Malmö
Upcoming SlideShare
Loading in...5
×

Git Presentation - Purple Scout AB Malmö

3,267

Published on

Sébastien Cevey gives a very nice presentation about the Git project at Purple Scout AB in April 2009.

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,267
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
133
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Transcript of "Git Presentation - Purple Scout AB Malmö"

  1. 1. Git Sébastien 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
  2. 2. Outline 1. Introduction 2. Concepts & Local Workflow 3. Distributed Work 4. Advanced Features 5. Conclusion 2
  3. 3. 1. Introduction 3
  4. 4. Version Control Systems • Record history • Facilitate team work • Expose source code • Branch/merge development paths 4
  5. 5. Centralized VCS permissions Master branches Repository commit checkout update Copies Working Working Working Copy Copy Copy Alice Bob Charlie 5
  6. 6. Distributed VCS Repository Repository Remote (public/private) push Repository clone pull Working Working Working Copy Copy Copy Local Repository Repository Repository Alice Bob Charlie 6
  7. 7. Centralized vs Distributed • • synchronous asynchronous - unique reference - copy = repository - fixed hierarchy - loose hierarchy • • centralized access offline: control - history - commit - branches • • simpler more powerful 7
  8. 8. Centralized vs Distributed • • synchronous asynchronous - unique reference - copy = repository - fixed hierarchy - loose hierarchy • • centralized access offline: control - history - commit - branches • • simpler more powerful • • tråkig... ;_; fun! ^_^ 8
  9. 9. Git History • Linus Torvalds needed a replacement for BitKeeper to maintain the Linux kernel • Requirements: • Distributed • Non-linear, custom workflows • Scalable & Fast • Talk over HTTP, SSH, FTP, rsync, email, etc • Integrity guarantee • First prototype on April 7, 2005 • Now used by many FOSS projects: Linux kernel, Freedesktop (X.org), Ruby on Rails, XMMS2, Android, Gnome, GCC, etc. 9
  10. 10. Why “Git” ? git (plural gits) 2. (British, slang, pejorative) A silly, incompetent, stupid, annoying or childish person. “Jacko is a git.” Linus Torvalds: “I'm an egotistical bastard, and I name all my projects after myself. First Linux, now git.” Alternatively: “Global Information Tracker” 10
  11. 11. Git vs. other DVCS (Mercurial, Darcs, Bazaar, Monotone, etc) + Fast, scalable (execution and setup) + Powerful small tools (Unix spirit) + Lively development + Linus (smart design) 11
  12. 12. Git vs. other DVCS (Mercurial, Darcs, Bazaar, Monotone, etc) + Fast, scalable (execution and setup) + Powerful small tools (Unix spirit) + Lively development + Linus (smart design) - Learning curve (grasp concepts) - Imperfect on MS Windows - Linus (no library) 12
  13. 13. Why Git is better than X? • Cheap Local Branching • Everything is Local • Git is Fast • Git is Small • The Staging Area • Distributed • Any Workflow • Easy to Learn Source: http://whygitisbetterthanx.com/ 13
  14. 14. 2. Concepts & Local Workflow 14
  15. 15. Git Repository repository = object database .git/objects/ 15
  16. 16. Git Repository id(object) = SHA1(object content) 335e a23f 6679 601a 5592 34fe cca6 3347 efa4 dd85 57bc cc39 3e93 2211 5390 8e9b 1798 332e 23a7 1432 1260 93a4 cc5e f943 eab4 b3ca .git/objects/8e/9b... 16
  17. 17. Git Repository content is stored in blob objects File #include <stdio.h> 335e a23f 6679 int main () { printf(”Hello World”); 601a 5592 return 0; } 34fe cca6 3347 efa4 dd85 57bc cc39 3e93 2211 5390 8e9b 1798 git cat-file -p 93a4 332e 23a7 1432 1260 93a4 cc5e f943 eab4 b3ca 17
  18. 18. Git Repository structure is stored in tree objects Directory 040000 tree cc5e src 100644 blob 93a4 hw.c 335e a23f 6679 601a 5592 34fe cca6 3347 efa4 dd85 57bc cc39 3e93 2211 5390 8e9b 1798 git cat-file -p 1260 332e 23a7 1432 1260 93a4 cc5e f943 eab4 b3ca 18
  19. 19. Git Repository history is stored in commit objects Authenticated hierarchical snapshots tree 1260... parent f943... 335e a23f 6679 author Tom <tom@...> 1204666883 +0100 committer Max 601a 5592 <max@...> 1204666883 +0100 34fe cca6 Note: identity is a 3347 efa4 function of the whole Fixed a major bug in dd85 57bc history! Hello World. cc39 3e93 i.e. build-in integrity 2211 5390 check 8e9b 1798 git cat-file -p 1432 332e 23a7 1432 1260 93a4 cc5e f943 eab4 b3ca 19
  20. 20. Git Repository references are stored as tag objects (Signed) symbolic link object a23f... type commit tag v1.0.7 335e a23f 6679 tagger Jack <jack@...> 1136523576 -0800 601a 5592 GIT 1.0.7 -----BEGIN PGP SIGNATURE----- 34fe cca6 3347 efa4 Version: GnuPG v1.4.2 (GNU/Linux) dd85 57bc iD8D… cc39 3e93 -----END PGP SIGNATURE----- 2211 5390 8e9b 1798 332e 23a7 git cat-file -p 335e 1432 1260 93a4 cc5e f943 eab4 b3ca 20
  21. 21. Git Repository practical repository entry points point to a branches feature-X master commit in git branch symbolic links the graph 335e a23f 6679 tags 601a 5592 34fe cca6 v1.0 3347 efa4 dd85 57bc alpha-3 cc39 3e93 2211 5390 point to an 8e9b 1798 object (usually a 332e 23a7 commit) 1432 1260 93a4 cc5e f943 eab4 git tag b3ca 21
  22. 22. History Graph branches feature-X master HEAD a23f tags 601a v1.0 3347 alpha-3 cc39 8e9b 1432 f943 22
  23. 23. Git workflow $ find . ./src ./src/Makefile ./src/helloworld.c ./src/helloworld.h ./Makefile ./IDEAS ./README IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 23
  24. 24. initialize $ git init Initialized empty Git repository in /path/example/.git/ IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 24
  25. 25. check status $ git status # On branch master # # Initial commit # # Untracked files: # (use quot;git add <file>...quot; to include in what will be committed) # # IDEAS # Makefile # README # src/ nothing added to commit but untracked files present (use quot;git addquot; to track) IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 25
  26. 26. add files $ git add . IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 26
  27. 27. check status $ git status # On branch master # # Initial commit # # Changes to be committed: # (use quot;git rm --cached <file>...quot; to unstage) # # new file: IDEAS # new file: Makefile # new file: README # new file: src/Makefile # new file: src/helloworld.c # new file: src/helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 27
  28. 28. commit $ git commit -a -m quot;First importquot; Created initial commit 04f087b: First import 0 files changed, 0 insertions(+), 0 deletions(-) master create mode 100644 IDEAS create mode 100644 Makefile create mode 100644 README create mode 100644 src/Makefile create mode 100644 src/helloworld.c create mode 100644 src/helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c 04f087b `-- helloworld.h 28
  29. 29. hack $ echo quot;Blah blahquot; >> README master $ rm IDEAS Makefile README <- edited src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 29
  30. 30. check status $ git status # On branch master # Changed but not updated: master # (use quot;git add/rm <file>...quot; to update what will be committed) # # deleted: IDEAS # modified: README # no changes added to commit (use quot;git addquot; and/or quot;git commit -aquot;) Makefile README <- edited src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 30
  31. 31. commit $ git commit -a -m quot;More stuffquot; Created commit 54eda77: More stuff 1 files changed, 1 insertions(+), 0 deletions(-) master delete mode 100644 IDEAS Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 31
  32. 32. view commit $ git show commit 54eda77a85b0e5d1891653a36ff08faf133df030 Author: Sebastien Cevey <seb@cine7.net> master Date: Mon Apr 13 21:23:02 2009 +0200 More stuff diff --git a/IDEAS b/IDEAS deleted file mode 100644 index e69de29..0000000 diff --git a/README b/README index e69de29..579c5b1 100644 --- a/README +++ b/README @@ -0,0 +1 @@ +Blah blah Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 32
  33. 33. view commit $ git show --pretty=raw commit 54eda77a85b0e5d1891653a36ff08faf133df030 tree 524f0cfe293064d18ac0c12a55baaf2b34d42e8d master parent 04f087b23ce96c2b24f9f6b5f7cf437af33e19c6 author Sebastien Cevey <seb@cine7.net> 1239650582 +0200 committer Sebastien Cevey <seb@cine7.net> 1239650582 +0200 More stuff [...] Makefile README 54eda77 524f0c src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 33
  34. 34. view history $ git log commit 54eda77a85b0e5d1891653a36ff08faf133df030 Author: Sebastien Cevey <seb@cine7.net> master Date: Mon Apr 13 21:23:02 2009 +0200 More stuff commit 04f087b23ce96c2b24f9f6b5f7cf437af33e19c6 Author: Sebastien Cevey <seb@cine7.net> Date: Mon Apr 13 21:11:58 2009 +0200 First import Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 34
  35. 35. hack again $ echo quot;/* End of fil */quot; >> src/helloworld.c master Makefile README 54eda77 src |-- Makefile |-- helloworld.c <- edited `-- helloworld.h 04f087b 35
  36. 36. 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
  37. 37. 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
  38. 38. 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
  39. 39. 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
  40. 40. new history $ $EDITOR src/helloworld.c $ git commit -a -m quot;Proper commitquot; 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
  41. 41. tag $ git tag stable master stable 0657c63 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 41
  42. 42. 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
  43. 43. switch branch $ git checkout feature-X Switched to branch quot;feature-Xquot; feature-X master $ git branch * feature-X master stable 0657c63 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 43
  44. 44. commit branch $ $EDITOR src/helloworld.c feature-X master $ git commit -a -m quot;Work on Xquot; 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
  45. 45. commit branch $ $EDITOR README feature-X master $ git commit -a -m quot;Edit README for Xquot; 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
  46. 46. diff branches $ git checkout master Switched to branch quot;masterquot; 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
  47. 47. 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
  48. 48. 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
  49. 49. 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 quot;feature-Xquot; 20fbfdd stable 0657c63 02bbf10 Makefile README 54eda77 src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 49
  50. 50. 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
  51. 51. trivial merge $ git checkout master Switched to branch quot;masterquot; 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
  52. 52. git show-branch 52
  53. 53. gitk 53
  54. 54. gitk 54
  55. 55. 3. Distributed Work 55
  56. 56. public repository t aul r ste f seg ma fix- D C E B public repo A 56
  57. 57. clone t aul r ste f seg ma fix- (over local filesystem, ssh://, git://, rsync://, ftp://etc) D C E B lt fau r ste public repo A seg ma fix- $ git clone $URL Initialized empty Git repository in local/ copy/.git/ D C E B local repo A 57
  58. 58. branch namespace t aul r ste f seg ma fix- D C E origin/ B lt fau r r ste ste public repo A seg ma ma fix- $ git branch --all *master origin/HEAD D origin/fix-segfault C E origin/master B local repo A 58
  59. 59. public repository updated t aul r ste f seg ma fix- D F C E origin/ B lt fau r r ste ste public repo A seg ma ma fix- D C E B local repo A 59
  60. 60. fetch update t aul r ste f seg ma fix- D F C E origin/ B lt fau r r ste ste public repo A seg ma ma fix- $ 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
  61. 61. checkout tracking branch t aul r ste f seg ma fix- “topic branch” “tracking branches” D F C E origin/ B lt fau r r rk ste ste public repo A seg wo ma ma fix- $ 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 quot;workquot; B local repo A 61
  62. 62. commit topic branch t aul r ste f seg ma fix- D F origin/ C E lt fau r r rk ste ste public repo B seg wo ma ma fix- $ $EDITOR $ git commit -a -m quot;Another fixquot; G [...] D F C E local repo B 62
  63. 63. push new commit t aul r ste f seg ma fix- G D F origin/ C E lt fau r r rk ste ste public repo B seg wo ma ma fix- $ 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
  64. 64. public repository updated t aul r ste f seg ma fix- H G origin/ D F lt fau r r rk ste ste public repo C E seg wo ma ma fix- G D F C E local repo B 64
  65. 65. fetch update t aul r ste f seg ma fix- H G origin/ D F lt fau r r rk ste ste public repo C E seg wo ma ma fix- $ git fetch origin [...] H From /public/repo G..H fix-segfault -> origin/fix-segfault G D F local repo C 65
  66. 66. pull to merge update t aul r ste f seg ma fix- H G origin/ D F lt fau r r rk ste ste public repo C E seg wo ma ma fix- $ 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
  67. 67. 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
  68. 68. ma ste r wo rk pre -m e rge ma ste dev/ r ma ste r tes t bug gy- cra dev p 68 ma ste r exp eri me nta l fix- seg f aul anna t Real-life layout stable/ ma ste r ma ste r fix- seg fau lt fix- seg f local repo aul t stable
  69. 69. ma ste r wo rk pre -m e rge ma ste dev/ r ma ste r tes t bug gy- cra dev p git remote add anna $URL anna/ 69 ma ste r exp eri me nta l fix- seg f aul anna t Real-life layout stable/ ma ste r ma ste r fix- seg fau lt fix- seg f local repo aul t stable
  70. 70. ma ste r wo rk pre -m e rge ma ste dev/ r ma ste r tes t bug gy- cra dev p anna/ 70 ma git fetch anna ste r exp eri ma me ste nt r al fix- exp seg eri fau lt me nta l fix- seg f aul anna t Real-life layout stable/ ma ste r ma ste r fix- seg fau lt fix- seg f local repo aul t stable
  71. 71. ma ste r not in .git/” “just a repo, wo git --bare init rk pre -m mine e rge ma ste dev/ r ma ste r tes t bug gy- cra dev p anna/ 71 ma ste r exp eri ma me ste nt r al fix- exp seg eri fau lt me nta l fix- seg f aul anna t Real-life layout stable/ ma ste r ma ste r fix- seg fau lt fix- seg f local repo aul t stable
  72. 72. ma ste r wo rk pre -m mine e rge ma ste dev/ r ma ste r tes t git remote add mine $URL bug gy- cra dev p anna/ 72 ma ste r exp eri ma me ste nt r al fix- exp seg eri fau lt me nta l fix- seg f aul anna t Real-life layout stable/ ma ste r ma ste r fix- seg fau lt fix- seg f local repo aul t stable
  73. 73. ma ste r ma ste r wo rk pre -m erg e pre -m mine e rge ma mine/ ste ma r ste r tes t pre -m e rge bug gy- cra dev p 73 dev/ ma ma ste r ste r exp eri me git push mine work:master pre-merge nta l fix- anna/ seg f ma aul anna t Real-life layout ste r exp eri me nt al ma fix- ste “remotes” r seg fau lt fix- seg f aul t stable stable/ local repo ma ste r
  74. 74. 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
  75. 75. 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
  76. 76. 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
  77. 77. 4. Advanced Features 77
  78. 78. Between the working directory and the repository Repository Working master Directory IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 78
  79. 79. The Index (or “Staging Area”) Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b 79
  80. 80. Committing using the index Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h git add/rm git commit 04f087b 80
  81. 81. Bypassing the Index Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h 04f087b git commit -a 81
  82. 82. new file Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO <- new file 04f087b 82
  83. 83. $ git status new file # On branch master # Changed but not updated: # (use quot;git add <file>...quot; to update what will be committed) # # modified: test.c # modified: TODO Index # no changes added to commit (use quot;git addquot; and/or quot;git commit -aquot;) IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO <- new file 04f087b 83
  84. 84. new file: add to index Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- 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
  85. 85. new file: add to index $ git status # On branch master # Changes to be committed: Index # (use quot;git reset HEAD <file>...quot; to unstage) # # modified: TODO IDEAS # Makefile Repository README # Changed but not updated: [...] Working src |-- Makefile master Directory |-- 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
  86. 86. new file: commit index Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO git commit 04f087b 86
  87. 87. edited file Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile README <- edited src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO 04f087b 87
  88. 88. edited file: update index Index IDEAS Makefile Repository README <- edited Working src |-- Makefile master Directory |-- 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
  89. 89. edited file: commit index Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile f4ec56 README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO git commit 04f087b 89
  90. 90. edited file Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h TODO IDEAS <- 3 hunks Makefile f4ec56 README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO 04f087b 90
  91. 91. add selected hunks to index Index IDEAS <- 1 hunk Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h TODO IDEAS <- 2 hunks Makefile f4ec56 README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef (also see git add --interactive) test.c <- new file TODO git add -p IDEAS 04f087b 91
  92. 92. add selected hunks to index Index $ git add -p src/xmms/testclient.c [...] @@ -44,7 +44,7 @@ int main (int argc, char **argv) 1 hunk IDEAS <- Makefile Repository fd = xmms_ipc_transport_fd_get (transport); README Working src |-- Makefile - msg = xmms_ipc_msg_string_new (42, quot;korv!quot;); master Directory |-- helloworld.c + msg = xmms_ipc_msg_string_new (42, quot;apan!quot;); `-- helloworld.h TODO i = 5; IDEAS <- 2 hunks Makefilethis hunk [y/n/a/d/j/J/?]? Stage f4ec56 README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef (also see git add --interactive) test.c <- new file TODO git add -p IDEAS 04f087b 92
  93. 93. diff’ing options Index IDEAS <- 1 hunk Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c git diff --cached `-- helloworld.h TODO IDEAS <- 2 hunks Makefile f4ec56 README src git diff |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef test.c <- new file TODO git diff HEAD 04f087b 93
  94. 94. Conflicts: marked in index Index IDEAS <- conflicted Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h TODO IDEAS <- merged Makefile f4ec56 README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef TODO 04f087b 94
  95. 95. Reset changes: 3 options Index IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile f4ec56 README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef TODO 04f087b 95
  96. 96. reset --hard reset the repository HEAD, the index Index and the working deadbeef directory. IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h deadbeef TODO IDEAS Makefile f4ec56 README src |-- Makefile |-- helloworld.c `-- helloworld.h git reset --hard deadbeef deadbeef TODO Warning: any uncommitted 04f087b change is lost! 96
  97. 97. reset --soft only reset the repository HEAD. Index f4ec56 IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h f4ec56 TODO IDEAS Makefile f4ec56 README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef git reset --soft deadbeef TODO Previous commit becomes 04f087b “changes to be committed”. 97
  98. 98. reset --mixed (default) reset the repository HEAD and the Index index, but not the deadbeef working directory. IDEAS Makefile Repository README Working src |-- Makefile master Directory |-- helloworld.c `-- helloworld.h f4ec56 TODO IDEAS Makefile f4ec56 README src |-- Makefile |-- helloworld.c `-- helloworld.h deadbeef git reset --mixed deadbeef TODO Keep changes but not 04f087b marked for commit. 98
  99. 99. 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
  100. 100. Git layers Source: http://osteele.com/archives/2008/05/commit-policies 100
  101. 101. Rewrite history! • Changing the history = point to a new history • Correct mistakes • Cleanup messy iterative development • Warning: - Changes commit ids - (Possibly) creates conflicts if when people merge the rewritten branch - Best used locally, not on an important tree 101
  102. 102. Rewrite history! (1) • git commit --amend r ste ma • “Replace the latest commit by the current state of the index” K D • Allows to keep or edit the C commit message B A 102
  103. 103. Rewrite history! (1) • git commit --amend r ste ma • “Replace the latest commit by the current state of the index” D K • Allows to keep or edit the C commit message B A 103
  104. 104. Rewrite history! (2) • git rebase --interactive r ste ma • Rewrite a sequence of commits: - omit F - squash with parent E - edit D C B A 104
  105. 105. Rewrite history! (2) • git rebase --interactive r ste ma • Rewrite a sequence of commits: - omit F T (E+F) - squash with parent E - edit D S (D) C B B A 105
  106. 106. Rewrite history! (3) • git filter-branch • Rewrite history using functions/scripts • Edit author infos, commit message, directories, commits, tags, etc. • RTFM... 106
  107. 107. git bisect • Useful to find the commit that introduces a problem r ste ma • git bisect start <bad> [<good>] HEAD H D C B A 107
  108. 108. git bisect • Useful to find the commit that introduces a problem r ste ma • git bisect start <bad> [<good>] H • git bisect bag|good D C B HEAD A 108
  109. 109. git bisect • Useful to find the commit that introduces a problem r ste ma • git bisect start <bad> [<good>] H • git bisect bag|good D HEAD C B A 109
  110. 110. git bisect • Useful to find the commit that introduces a problem r ste ma • git bisect start <bad> [<good>] H • git bisect bag|good HEAD D C B A 110
  111. 111. git blame • git blame D <file> r ste ma H HEAD D C B A 111
  112. 112. git stash • git stash [save [message]] r ste • git stash apply ma • git stash pop # apply and drop modifs • Can also preserve the state of the index 112
  113. 113. git stash • git stash [save [message]] {0} r ste sh@ • git stash apply ma sta • git stash pop # apply and drop modifs • Can also preserve the state of the index 113
  114. 114. git stash • git stash [save [message]] {0} r ste sh@ • git stash apply ma sta • git stash pop # apply and drop modifs • Can also preserve the state of the index 114
  115. 115. git stash • git stash [save [message]] {0} r ste sh@ • git stash apply ma sta • git stash pop # apply and drop modifs modifs • Can also preserve the state of the index 115
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×