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. 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)
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Initialization $ mkdir devtools_git $ cd devtools_git $ git init

    Initialized empty Git repository in /.../devtools_git/.git $ ls -a . .. .git
  10. 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
  11. 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)
  12. 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
  13. 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
  14. 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
  15. First Branch $ git checkout -b br_hi Switched to a

    new branch 'br_hi' $ gvim hello_world.py test_hello_world.py
  16. 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':
  17. 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”
  18. 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
  19. 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(-)
  20. 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
  21. 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.
  22. 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 …
  23. 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”
  24. 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