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

Introduction to Git

Introduction to Git

harjinder-hari

April 18, 2017
Tweet

More Decks by harjinder-hari

Other Decks in Programming

Transcript

  1. Introduction To Git

  2. Speaker: • Harjindersingh Mistry

  3. Git Confusions ! Commits, Branch ?!? Image courtesy: https://tytempletonart.files.wordpress.com/2010/09/calvin-faces.jpg

  4. Agenda • Getting Started • Git Model • Working with

    Git
  5. What is Git ? • Distributed version control tool focused

    on speed and efficiency • Usually runs as a CLI tool in your local machine • Stores entire repository (repo)/project on peer/node • Originally developed by Linus Torvalds and he handed over the maintenance to Junio Hamano after initial 6 months and Junio has been maintaining it from 2005 - (Talk by Linus on Git in Google May 2007) $ git --version git version 2.10.1 (Apple Git-78)
  6. Getting started is really simple ! • Fedora, RedHat, CentOS:

    dnf/yum install git • Mac OS: brew install git • Windows: download from https://git-scm.com/downloads • Create an empty repository ◦ mkdir devtools_git; cd devtools_git; git init
  7. Agenda • Getting Started • Git Model • Working with

    Git
  8. Goals of Git • Git is a distributed version control

    system with following goals: ◦ Track changes to the content ◦ Store content and safeguard it against corruption ◦ Distribute the content and history with collaborators
  9. Goals of Git • Git achieves its goals by having

    a simple model ◦ It stores snapshots of whole project directory and not diff ! ◦ It refers to each file by checksum ( SHA-1 ) of file-contents ◦ Repo is almost self-dependent ! It stores all files and entire history
  10. Main Sections of Git Project: An Example Working Directory hello_world.py

    test_hello_world.py README.md Staging Area <Empty> HEAD Repository br_hi hello to hi hello world v1 master hello to wassup br_wsup
  11. Git model: Repository • Git repository ( repo ) is

    a collection of files + history of changes • It is stored inside .git sub-directory • It has four kinds of objects: ◦ blob: actual file contents ◦ tree: collection of blobs along with their hierarchy ◦ tag: named reference to another object ◦ commit: revision ( snapshot ) of entire repo
  12. IMPORTANT: Git stores snapshots, not diff ! hello_world.py test_hello_world.py README.md

    hello world v1 hello_world.py test_hello_world.py README.md hello to hi hello_world.py test_hello_world.py README.md changed README
  13. Git model: Working Tree and Staging Area • Working tree

    is a single checkout of one version of the project. ◦ It is the directory that we work on ◦ These files are extracted from repo’s object database • Staging area ( index ) ◦ It is an area between working tree and the repo ◦ It is useful in keeping track of what will go in the next commit
  14. Agenda • Getting Started • Git Model • Working with

    Git
  15. Initialization $ mkdir devtools_git $ cd devtools_git $ git init

    Initialized empty Git repository in /.../devtools_git/.git $ ls -a . .. .git
  16. Initialization Working Directory <Empty> Staging Area <Does not exist> HEAD

    Repository
  17. Write src code in files $ gvim hello_world.py $ gvim

    test_hello_world.py $ gvim README.md $ ls -a . .. .git README.md hello_world.py test_hello_world.py
  18. Write src code in files Working Directory hello_world.py test_hello_world.py README.md

    Staging Area <Does not exist> HEAD Repository
  19. Add files into staging area $ ls -a . ..

    .git README.md hello_world.py test_hello_world.py $ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) README.md hello_world.py test_hello_world.py nothing added to commit but untracked files present (use " git add" to track)
  20. Add files into staging area $ git add . $

    git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md new file: hello_world.py new file: test_hello_world.py
  21. Add files into staging area Working Directory hello_world.py test_hello_world.py README.md

    Staging Area hello_world.py test_hello_world.py README.md HEAD Repository
  22. Commit $ git commit -m "hello world v1" [master (root-commit)

    4c47331] hello world v1 3 files changed, 21 insertions(+) create mode 100644 README.md create mode 100644 hello_world.py create mode 100644 test_hello_world.py $ git status On branch master nothing to commit, working tree clean
  23. Commit Working Directory hello_world.py test_hello_world.py README.md Staging Area <Empty> HEAD

    Repository master hello world v1
  24. First Branch $ git checkout -b br_hi Switched to a

    new branch 'br_hi' $ gvim hello_world.py test_hello_world.py
  25. First Branch Working Directory hello_world.py test_hello_world.py README.md Staging Area <Empty>

    Repository HEAD master hello world v1 br_hi
  26. First Branch $ git diff diff --git a/hello_world.py b/hello_world.py index

    a42d851..dfafaa5 100644 --- a/hello_world.py +++ b/hello_world.py @@ -2,7 +2,7 @@ def get_greetings(lang): greetings = 'hmmm ?!?' if lang == 'English': - greetings = 'hello world!' + greetings = 'hi world!' elif lang == 'Hindi':
  27. First Branch $ git add -u $ git commit -m

    “hello to hi”
  28. First Branch Working Directory hello_world.py test_hello_world.py README.md Staging Area <Empty>

    HEAD Repository br_hi hello to hi hello world v1 master
  29. Second Branch $ git checkout -b br_wsup Switched to a

    new branch 'br_wsup' $ gvim hello_world.py test_hello_world.py $ git add -u $ git commit -m “hello to wassup”
  30. Second Branch Working Directory hello_world.py test_hello_world.py README.md Staging Area <Empty>

    HEAD Repository br_hi hello to hi hello world v1 master hello to wassup br_wsup
  31. First Merge $ git checkout master Switched to branch 'master'

    $ git merge br_hi Updating 4c47331..57686cb Fast-forward hello_world.py | 2 +- test_hello_world.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
  32. First Merge Working Directory hello_world.py test_hello_world.py README.md Staging Area <Empty>

    HEAD Repository br_hi hello to hi hello world v1 master hello to wassup br_wsup
  33. Second Merge $ git checkout master Switched to branch 'master'

    $ git merge br_wsup Auto-merging test_hello_world.py CONFLICT (content): Merge conflict in test_hello_world.py Auto-merging hello_world.py CONFLICT (content): Merge conflict in hello_world.py Automatic merge failed; fix conflicts and then commit the result.
  34. Resolving Merge Conflict $ gvim hello_world.py def get_greetings(lang): greetings =

    'hmmm ?!?' if lang == 'English': <<<<<<< HEAD greetings = 'hi world!' ======= greetings = 'wassup world!' >>>>>>> br_wsup …
  35. Resolving Merge Conflict Choose the src code from conflict area:

    $ gvim hello_world.py def get_greetings(lang): greetings = 'hmmm ?!?' if lang == 'English': greetings = 'wassup world!' … $ git add -u; git commit -m “merged commits”
  36. Second Merge Working Directory hello_world.py test_hello_world.py README.md Staging Area <Empty>

    HEAD Repository br_hi hello to hi hello world v1 master hello to wassup br_wsup merged commits
  37. Go Git it ! Image courtesy: https://s-media-cache-ak0.pinimg.com/736x/4a/f7/ac/4af7acd3a273b838dbd886c56a8ffd51.jpg

  38. References • http://www.aosabook.org/en/git.html • http://web.mit.edu/nelhage/Public/git-slides-2009.pdf • http://www.vogella.com/tutorials/Git/article.html • https://git-scm.com/book/en/v2