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

Getting Started with Git

Getting Started with Git

Anmol Sarma

May 17, 2013
Tweet

More Decks by Anmol Sarma

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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!!!
  5. 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
  6. 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 α β
  7. 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 α β γ δ
  8. 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
  9. Getting Started with Git Saying Hello $ git config --global

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

    cd git-tutorial $ git init Initialized empty Git repository in /home/anmol/git- tutorial/.git/
  11. 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)
  12. Getting Started with Git Getting Started $ touch foo.txt $

    echo “This is the 1st line.” >> foo.txt
  13. Getting Started with Git Getting Started $ git status #

    On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # foo.txt nothing added to commit but untracked files present (use "git add" to track)
  14. 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 <file>..." to unstage) # # new file: foo.txt
  15. 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
  16. 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 <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: foo.txt # no changes added to commit (use "git add" and/or "git commit -a")
  17. 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(+)
  18. Getting Started with Git Getting Started $ git log commit

    44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma <[email protected]> Date: Wed May 8 14:24:17 2013 +0530 Added 2nd line commit 36f461b160c278f1e86ffc6fab23f4a41f98824c Author: Anmol Sarma <[email protected]> Date: Wed May 8 14:22:54 2013 +0530 Initial Commit
  19. Getting Started with Git Getting Started $ git show commit

    44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma <[email protected]> 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.
  20. 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 <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: foo.txt # no changes added to commit (use "git add" and/or "git commit -a")
  21. Getting Started with Git Getting Started $ git checkout foo.txt

    $ git status # On branch master nothing to commit, working directory clean
  22. 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.
  23. 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.
  24. 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.
  25. 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!
  26. 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
  27. 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
  28. 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
  29. 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
  30. Getting Started with Git Checking-out Commits Initial Commit HEAD Added

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

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

    2nd Line Added 2nd Line Changes to Working Tree git checkout foo.txt
  33. Getting Started with Git Checking-out Commits $ git log commit

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

    44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma <[email protected]> Date: Wed May 8 14:24:17 2013 +0530 Added 2nd line commit 36f461b160c278f1e86ffc6fab23f4a41f98824c Author: Anmol Sarma <[email protected]> Date: Wed May 8 14:22:54 2013 +0530 Initial Commit
  35. 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
  36. Getting Started with Git Checking-out Commits $ git status #

    Not currently on any branch. nothing to commit, working directory clean
  37. Getting Started with Git Checking-out Commits Initial Commit HEAD Added

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

    2nd Line Added 2nd Line Working Tree git checkout 36f461b
  39. Getting Started with Git Checking-out Commits $ git checkout master

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

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

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

    2nd Line Added 2nd Line Working Tree git checkout master
  43. Getting Started with Git Merge Workflow $ git checkout new-branch

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

    Switched to branch 'new-branch' $ git branch master * new-branch
  45. 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
  46. Getting Started with Git Merge Workflow $ git status #

    On branch new-branch # 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: foo.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # feature.txt no changes added to commit (use "git add" and/or "git commit -a")
  47. Getting Started with Git Merge Workflow $ git add .

    $ git commit -m “Added cool new feature.”
  48. 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
  49. Getting Started with Git Merge Workflow $ git log commit

    b1148feda3956161eaa71f13f6d0a1d765f6d5b2 Author: Anmol Sarma <[email protected]> Date: Sun May 12 11:16:37 2013 +0530 Added new feature commit 44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma <[email protected]> Date: Wed May 8 18:17:17 2013 +0530 Added 2nd line
  50. Getting Started with Git Merge Workflow $ git checkout master

    Switched to branch 'master' $ git merge new-branch
  51. 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
  52. Getting Started with Git Merge Workflow $ git log commit

    b1148feda3956161eaa71f13f6d0a1d765f6d5b2 Author: Anmol Sarma <[email protected]> Date: Sun May 12 11:16:37 2013 +0530 Added new feature commit 44c17cdf64061ded390421143076abe9a9016a16 Author: Anmol Sarma <[email protected]> Date: Wed May 8 18:17:17 2013 +0530 Added 2nd line
  53. Getting Started with Git Merge Workflow Initial Commit Added 2nd

    Line master Added New Feature new-branch Did Something Else