Slide 1

Slide 1 text

Getting Started with Anmol Sarma http://anmolsarma.in May 2013 | CC-BY-SA 3.0

Slide 2

Slide 2 text

Getting Started with Git Agenda ● SCM/VCS - Distributed ● Git Intro, Setup, Getting Started ● Working Tree, Staging Area, Repository ● Branches & Tags ● Merge Workflow ● Remote and Patches ● GitHub

Slide 3

Slide 3 text

Getting Started with Git Git is a Source Code Management system A Source Code Management system: ● Keeps track of changes to your code across versions ● Lets you switch between different versions a.k.a Version Control Version Control: ● The “Big Undo” ● Support concurrent and iterative development ● Simplify code sharing and multi-developer projects

Slide 4

Slide 4 text

Getting Started with Git Git is a Source Code Management system A Source Code Management system: ● Keeps track of changes to your code across versions ● Lets you switch between different versions a.k.a Version Control Version Control: ● The “Big Undo” ● Support concurrent and iterative development ● Simplify code sharing and multi-developer projects

Slide 5

Slide 5 text

Getting Started with Git About Git ● Originally conceived by Linus Torvalds for the Kernel ● Open-source (GPL v2) ● Cross-platform: Linux, Windows, OS X, Solaris ● Suitable for projects ranging from a few to tens of millions of lines of code. ● Distributed ● FAST!!!

Slide 6

Slide 6 text

Getting Started with Git Who uses Git ...millions of others and so can you!

Slide 7

Slide 7 text

Getting Started with Git Git is distributed ● No difference between a client and a server ● Each repository contains the entire code history ● Available offline ● Sync changes between any two repositories

Slide 8

Slide 8 text

Getting Started with Git Git is distributed ● No difference between a client and a server ● Each repository contains the entire code history ● Available offline ● Sync changes between any two repositories α β

Slide 9

Slide 9 text

Getting Started with Git Git is distributed ● No difference between a client and a server ● Each repository contains the entire code history ● Available offline ● Sync changes between any two repositories α β γ δ

Slide 10

Slide 10 text

Getting Started with Git Getting Git ● Linux: Install Git using your distro's package manger. If your distro doesn't have Git in its software repositories, consider switching to a saner distro  ● Mac OS X: http://git-scm.com/download/mac ● Windows: http://git-scm.com/download/win

Slide 11

Slide 11 text

Getting Started with Git Saying Hello $ git config --global user.name “Your Name” $ git config --global user.email “[email protected]

Slide 12

Slide 12 text

Getting Started with Git Getting Started $ mkdir git-tutorial $ cd git-tutorial

Slide 13

Slide 13 text

Getting Started with Git Getting Started $ mkdir git-tutorial $ cd git-tutorial $ git init

Slide 14

Slide 14 text

Getting Started with Git Getting Started $ mkdir git-tutorial $ cd git-tutorial $ git init Initialized empty Git repository in /home/anmol/git- tutorial/.git/

Slide 15

Slide 15 text

Getting Started with Git Getting Started $ git status

Slide 16

Slide 16 text

Getting Started with Git Getting Started $ git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)

Slide 17

Slide 17 text

Getting Started with Git Getting Started $ touch foo.txt $ echo “This is the 1st line.” >> foo.txt

Slide 18

Slide 18 text

Getting Started with Git Getting Started $ git status

Slide 19

Slide 19 text

Getting Started with Git Getting Started $ git status # On branch master # # Initial commit # # Untracked files: # (use "git add ..." to include in what will be committed) # # foo.txt nothing added to commit but untracked files present (use "git add" to track)

Slide 20

Slide 20 text

Getting Started with Git Getting Started $ git add foo.txt $ git status

Slide 21

Slide 21 text

Getting Started with Git Getting Started $ git add foo.txt $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached ..." to unstage) # # new file: foo.txt

Slide 22

Slide 22 text

Getting Started with Git Getting Started $ git commit -m “Initial Commit”

Slide 23

Slide 23 text

Getting Started with Git Getting Started $ git commit -m “Initial Commit” [master (root-commit) 36f461b] Initial Commit 1 file changed, 1 insertion(+) create mode 100644 foo.txt

Slide 24

Slide 24 text

Getting Started with Git Getting Started $ git echo “The 2nd line” >> foo.txt

Slide 25

Slide 25 text

Getting Started with Git Getting Started $ git echo “The 2nd line” >> foo.txt $ git status # On branch master # Changes not staged for commit: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: foo.txt # no changes added to commit (use "git add" and/or "git commit -a")

Slide 26

Slide 26 text

Getting Started with Git Getting Started $ git add foo.txt $ git commit -m “Added 2nd line” [master 44c17cd] Added 2nd line 1 file changed, 1 insertion(+)

Slide 27

Slide 27 text

Getting Started with Git Getting Started $ git log commit 44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma Date: Wed May 8 14:24:17 2013 +0530 Added 2nd line commit 36f461b160c278f1e86ffc6fab23f4a41f98824c Author: Anmol Sarma Date: Wed May 8 14:22:54 2013 +0530 Initial Commit

Slide 28

Slide 28 text

Getting Started with Git Getting Started $ git show commit 44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma Date: Wed May 8 18:17:17 2013 +0530 Added 2nd line diff --git a/foo.txt b/foo.txt index cea4b69..800cca5 100644 --- a/foo.txt +++ b/foo.txt @@ -1 +1,2 @@ This is the first +The 2nd line.

Slide 29

Slide 29 text

Getting Started with Git Getting Started $ git echo “This is a random line...” >> foo.txt $ git status # On branch master # Changes not staged for commit: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: foo.txt # no changes added to commit (use "git add" and/or "git commit -a")

Slide 30

Slide 30 text

Getting Started with Git Getting Started $ git checkout foo.txt $ git status # On branch master nothing to commit, working directory clean

Slide 31

Slide 31 text

Getting Started with Git Commits ● A commit is a snapshot of your source code at some point in time. ● Each commit is identified by a 40 character string 44c17cdf64061ded390421143076abe9a9016a16 i.e. the SHA-1 hash ● Most other systems store data as changes to a base version; Git treats data as a set of commits.

Slide 32

Slide 32 text

Getting Started with Git Commits ● A commit is a snapshot of your source code at some point in time. ● Each commit is identified by a 40 character string 44c17cdf64061ded390421143076abe9a9016a16 i.e. the SHA-1 hash ● Most other systems store data as changes to a base version; Git treats data as a set of commits.

Slide 33

Slide 33 text

Getting Started with Git Commits ● A commit is a snapshot of your source code at some point in time. ● Each commit is identified by a 40 character string 44c17cdf64061ded390421143076abe9a9016a16 i.e. the SHA-1 hash ● Most other systems store data as changes to a base version; Git treats data as a set of commits.

Slide 34

Slide 34 text

Getting Started with Git Commits ● A commit is a snapshot of your source code at some point in time. ● Each commit is identified by a 40 character string 44c17cdf64061ded390421143076abe9a9016a16 i.e. the SHA-1 hash ● Most other systems store data as changes to a base version; Git treats data as a set of commits. Git behaves like a mini filesystem!

Slide 35

Slide 35 text

Getting Started with Git Working Tree, Staging Area, Repository ● Working Tree: Contents of your code directory ● Staging Area: Stores what goes into next commit ● Repository: Set of commits; HEAD-->Checked-out Working Tree

Slide 36

Slide 36 text

Getting Started with Git Working Tree, Staging Area, Repository ● Working Tree: Contents of your code directory ● Staging Area: Stores what goes into next commit ● Repository: Set of commits; HEAD-->Checked-out Staging Area Working Tree

Slide 37

Slide 37 text

Getting Started with Git Working Tree, Staging Area, Repository ● Working Tree: Contents of your code directory ● Staging Area: Stores what goes into next commit ● Repository: Set of commits; HEAD-->Checked-out Repository Staging Area Working Tree

Slide 38

Slide 38 text

Getting Started with Git Working Tree, Staging Area, Repository ● Working Tree: Contents of your code directory ● Staging Area: Stores what goes into next commit ● Repository: Set of commits; HEAD-->Checked-out Repository Staging Area Working Tree

Slide 39

Slide 39 text

Getting Started with Git Checking-out Commits Initial Commit

Slide 40

Slide 40 text

Getting Started with Git Checking-out Commits Initial Commit HEAD

Slide 41

Slide 41 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Working Tree

Slide 42

Slide 42 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Working Tree

Slide 43

Slide 43 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Changes to Working Tree

Slide 44

Slide 44 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Changes to Working Tree git checkout foo.txt

Slide 45

Slide 45 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Changes to Working Tree git checkout foo.txt

Slide 46

Slide 46 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Working Tree

Slide 47

Slide 47 text

Getting Started with Git Checking-out Commits $ git log commit 44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma Date: Wed May 8 14:24:17 2013 +0530 Added 2nd line commit 36f461b160c278f1e86ffc6fab23f4a41f98824c Author: Anmol Sarma Date: Wed May 8 14:22:54 2013 +0530 Initial Commit

Slide 48

Slide 48 text

Getting Started with Git Checking-out Commits $ git log commit 44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma Date: Wed May 8 14:24:17 2013 +0530 Added 2nd line commit 36f461b160c278f1e86ffc6fab23f4a41f98824c Author: Anmol Sarma Date: Wed May 8 14:22:54 2013 +0530 Initial Commit

Slide 49

Slide 49 text

Getting Started with Git Checking-out Commits $ git checkout 36f461b160c278f1e86ffc6fab23f4a41f98824c

Slide 50

Slide 50 text

Getting Started with Git Checking-out Commits $ git checkout 36f461b160c278f1e86ffc6fab23f4a41f98824c Note: checking out '36f461b160c278f1e86ffc6fab23f4a41f98824c'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_name HEAD is now at 36f461b... Initial Commit

Slide 51

Slide 51 text

Getting Started with Git Checking-out Commits $ git status

Slide 52

Slide 52 text

Getting Started with Git Checking-out Commits $ git status # Not currently on any branch. nothing to commit, working directory clean

Slide 53

Slide 53 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Working Tree

Slide 54

Slide 54 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Working Tree git checkout 36f461b

Slide 55

Slide 55 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Working Tree git checkout 36f461b

Slide 56

Slide 56 text

Getting Started with Git Checking-out Commits $ git checkout master

Slide 57

Slide 57 text

Getting Started with Git Checking-out Commits $ git checkout master Previous HEAD position was 36f461b... Initial Commit Switched to branch 'master'

Slide 58

Slide 58 text

Getting Started with Git Checking-out Commits $ git checkout master Previous HEAD position was 36f461b... Initial Commit Switched to branch 'master'

Slide 59

Slide 59 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Working Tree git checkout master

Slide 60

Slide 60 text

Getting Started with Git Checking-out Commits Initial Commit HEAD Added 2nd Line Added 2nd Line Working Tree git checkout master

Slide 61

Slide 61 text

Getting Started with Git Merge Workflow $ git branch

Slide 62

Slide 62 text

Getting Started with Git Merge Workflow $ git branch * master

Slide 63

Slide 63 text

Getting Started with Git Merge Workflow $ git branch new-branch $ git branch

Slide 64

Slide 64 text

Getting Started with Git Merge Workflow $ git branch new-branch $ git branch * master new-branch

Slide 65

Slide 65 text

Getting Started with Git Merge Workflow $ git checkout new-branch

Slide 66

Slide 66 text

Getting Started with Git Merge Workflow $ git checkout new-branch Switched to branch 'new-branch'

Slide 67

Slide 67 text

Getting Started with Git Merge Workflow $ git checkout new-branch Switched to branch 'new-branch' $ git branch

Slide 68

Slide 68 text

Getting Started with Git Merge Workflow $ git checkout new-branch Switched to branch 'new-branch' $ git branch master * new-branch

Slide 69

Slide 69 text

Getting Started with Git Merge Workflow $ touch feature.txt $ echo “This will add a new feature.” >> feature.txt $ echo “3rd line for the new feature.” >> foo.txt

Slide 70

Slide 70 text

Getting Started with Git Merge Workflow $ git status

Slide 71

Slide 71 text

Getting Started with Git Merge Workflow $ git status # On branch new-branch # Changes not staged for commit: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: foo.txt # # Untracked files: # (use "git add ..." to include in what will be committed) # # feature.txt no changes added to commit (use "git add" and/or "git commit -a")

Slide 72

Slide 72 text

Getting Started with Git Merge Workflow $ git add . $ git commit -m “Added cool new feature.”

Slide 73

Slide 73 text

Getting Started with Git Merge Workflow $ git add . $ git commit -m “Added cool new feature.” [new-branch b1148fe] Added new feature 2 files changed, 2 insertions(+) create mode 100644 feature.txt

Slide 74

Slide 74 text

Getting Started with Git Merge Workflow $ git log commit b1148feda3956161eaa71f13f6d0a1d765f6d5b2 Author: Anmol Sarma Date: Sun May 12 11:16:37 2013 +0530 Added new feature commit 44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma Date: Wed May 8 18:17:17 2013 +0530 Added 2nd line

Slide 75

Slide 75 text

Getting Started with Git Merge Workflow $ git checkout master

Slide 76

Slide 76 text

Getting Started with Git Merge Workflow $ git checkout master Switched to branch 'master'

Slide 77

Slide 77 text

Getting Started with Git Merge Workflow $ git checkout master Switched to branch 'master' $ git merge new-branch

Slide 78

Slide 78 text

Getting Started with Git Merge Workflow $ git checkout master Switched to branch 'master' $ git merge new-branch Updating 44c17cd..b1148fe Fast-forward feature.txt | 1 + foo.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 feature.txt

Slide 79

Slide 79 text

Getting Started with Git Merge Workflow $ git log commit b1148feda3956161eaa71f13f6d0a1d765f6d5b2 Author: Anmol Sarma Date: Sun May 12 11:16:37 2013 +0530 Added new feature commit 44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma Date: Wed May 8 18:17:17 2013 +0530 Added 2nd line

Slide 80

Slide 80 text

Getting Started with Git Merge Workflow Initial Commit Added 2nd Line master Added New Feature new-branch Did Something Else