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

Git Introduction

Avatar for Julian Chu Julian Chu
December 30, 2013

Git Introduction

This is slides for my ex-colleagues when I introduce git. Wish they to enjoy this wonderful tool.

Avatar for Julian Chu

Julian Chu

December 30, 2013
Tweet

Other Decks in Technology

Transcript

  1. Who create Git • Linus Torvalds • Maintains Linux Kernel

    more than 20 yrs • “Just for hobby” • Android, Server, Embedded System
  2. Git 1. For Linux Kernel 1. Thousands of developers in

    many countries 2. Thousands of branches 3. communicate via network 2. Fast 3. Is Open Source and For Open Source
  3. What Problems to Solve 1. Many people works in one

    project 2. Many branches in one project 3. Many features in developing 4. How to share piece of code in different projects
  4. Step 1 # i n c l u d e

    < s t d i o . h > i n t m a i n ( ) { p r i n t f ( " h i \ n " ) ; r e t u r n 0 ; }
  5. Step 2 # i n c l u d e

    < s t d i o . h > i n t m a i n ( ) { p r i n t f ( " f o o \ n " ) ; p r i n t f ( " b a r \ n " ) ; r e t u r n 0 ; }
  6. Step 3 # i n c l u d e

    < s t d i o . h > s t a t i c v o i d p r i n t S t r s ( ) { p r i n t f ( " f o o \ n " ) ; p r i n t f ( " b a r \ n " ) ; } i n t m a i n ( ) { p r i n t S t r s ( ) ; r e t u r n 0 ; }
  7. Step 4 # i n c l u d e

    < s t d i o . h > s t a t i c v o i d p r i n t S t r s ( ) { p r i n t f ( " f o o \ n " ) ; p r i n t f ( " b a r \ n " ) ; } s t a t i c v o i d c a l c ( ) { i n t i = 0 , t o t a l = 0 ; f o r ( i = 1 ; i < = 1 0 0 ; i + + ) { t o t a l = t o t a l + i ; } p r i n t f ( " t o t a l : % d \ n " , t o t a l ) ; } i n t m a i n ( ) { p r i n t S t r s ( ) ; c a l c ( ) ; r e t u r n 0 ; }
  8. Step 5 # i n c l u d e

    < s t d i o . h > s t a t i c v o i d p r i n t S t r s ( ) { p r i n t f ( " f o o \ n " ) ; p r i n t f ( " b a r \ n " ) ; } s t a t i c v o i d c a l c ( ) { i n t b o t t o m = 1 , t o p = 1 0 0 ; p r i n t f ( " t o t a l : % d \ n " , ( b o t t o m + t o p ) * t o p / 2 ) ; } i n t m a i n ( ) { p r i n t S t r s ( ) ; c a l c ( ) ; r e t u r n 0 ; }
  9. Patch Each steps create a patch. A patch is a

    snippet of modification that adds or removes lines from code. • patch / diff • Git is patch based tool • Git manages patches in order
  10. Commits(2) e09160 ede7ee ffabac 450e22 f17364 be6238 Step1.patch Step2.patch Step3.patch

    Step4.patch Step5.patch • Conceptually .. • commit 450e22 = commit ffabac + Step3.patch • commit ffabac = commit ede7ee + Step2.patch • We could regards each commits as ‘snapshot’
  11. Git Internal Objects • Git Internal Objects • Git is

    more like File System • Git is a simple key-value data store
  12. Summary for commits • Improve source code step by step

    • Each improvement is a Patch • Git manages patches in order • Each snapshot has a hash
  13. .git directory . ├ ─ ─ . g i t

    │ ├ ─ ─ b r a n c h e s │ ├ ─ ─ C O M M I T _ E D I T M S G │ ├ ─ ─ c o n f i g │ ├ ─ ─ d e s c r i p t i o n │ ├ ─ ─ H E A D │ ├ ─ ─ h o o k s │ ├ ─ ─ i n d e x │ ├ ─ ─ i n f o │ ├ ─ ─ l o g s │ ├ ─ ─ o b j e c t s │ ├ ─ ─ O R I G _ H E A D │ ├ ─ ─ p a c k e d ­ r e f s │ └ ─ ─ r e f s ├ ─ ─ . g i t i g n o r e └ ─ ─ h e l l o . c
  14. .git HelloGit_App hello.c hello.h world.c working tree Working Tree •

    HelloGit_App - Our project • .git - the database • working tree - where we are working on. • Check out source code from .git to working tree
  15. 450e22 f17364 be6238 .git checkout hello.c hello.h world.c working tree

    Git Checkout Check a commit from repository • $ git checkout be6238
  16. 450e22 f17364 be6238 .git hello.c hello.h world.c working tree foo.c

    Changed !! bar.c Git Status If working tree was modified - Changed !
  17. 450e22 f17364 be6238 commit !!! 450e22 f17364 be6238 dead42 .git

    Git Commit Create a new commit to history and Write Log
  18. Git Add What to commit? 1. $ git add foo.c

    2. $ git add bar.c 3. $ git commit
  19. .git .git .git .git Git is distribute • Everyone works

    in his own repository - locally • Push / Fetch objects to/from remote
  20. .git .git push fetch commit checkout Push and Fetch •

    Git push - sends you commits to remote • Git fetch - retrieve commits from remote
  21. .git .git github .git bitbucket Git Push • Git push

    - sends you commits to remote • $ git push bitbucket • $ git push github By default, you can only push to bare repository unless you config receive.denyCurrentBranch
  22. .git .git github .git bitbucket Git Fetch • Git fetch

    - retrieve commits from remote • $ git fetch bitbucket • $ git fetch github
  23. 450e22 f17364 be6238 master 450e22 f17364 be6238 cc0f38 master Git

    Branch(1) • master branch - default branch • master is like a pointer refer to the latest commit of a branch
  24. 450e22 f17364 be6238 cc0f38 master f1038a 578bb1 4398cb 55670a fix_bug

    feature Git Branch(2) • We could have lots of branches • $ git branch feature • $ git commit # commit something • $ git checkout master # switch back to master branch
  25. 450e22 f17364 be6238 cc0f38 master f1038a 578bb1 ffbbca fix_bug Git

    Merge • If we are in master, wanna merge fix_bug • $ git merge fix_bug
  26. 450e22 f17364 be6238 cc0f38 master f1038a 578bb1 fix_bug 450e22 f17364

    c09187a 7b6c51 master f1038a 578bb1 fix_bug Git Rebase • If we are in master, wanna rebase fix_bug • $ git rebase fix_bug
  27. Git Flow • When creating new feature, fixing bug -

    in new branch • When finishing, merge back to master branch • To keep master branch as stable as possible
  28. Basic Flow Use ‘SourceTree’ to 1. Clone a git repository

    2. Read log 3. Modify the source code 4. Commit to repository 5. Push to remote repository
  29. Config Name and Email • Tools / Options • Enter

    your name and email address • These information will be saved along with your commit
  30. Ready to Push • We have 1 more commit and

    are ahead of remote branch • Click Push push your changes to remote
  31. Make a branch 1. create a branch ‘new_feature’ 2. commit

    to new branch 3. checkout master - Boss wants to demo 4. checkout new_feature 5. merge to master
  32. We used to • Manage branches that many people work

    on • Code review (pull request) • Merge/Split/Modify commits (git rebase -i) • Export an archive of specific commit/tag (git archive) • To trace who wrote the code which defect program (git blame) • Compare changes between two commits (git diff)
  33. More Commands • $ git amend • $ git rebase

    -i • $ git bisect • $ git cherry-pick • $ git format-patch / am • $ git archive • $ git diff • $ git help ..