Git Collaboration

.. in a hackathon

Created By
Varun Maliwal / @vroom_

Software Engineer @ Capital One

http://bit.ly/hackathon-git-intro

Agenda

  • Git internals
  • Useful git commands
  • Workflow

What is Git?

  • A distributed version control system
  • Full history support
  • Version-tracking support, independent of central server

Why is Git important?

Ever make a few changes and now nothing works? Git is here to save the day!

Git allows you to revisit the entire history of your project and easily rollback to previous revisions.

Git Repository

.git subdirectory is called as repository

What does .git include?

  • commit objects
  • HEAD - a pointer to the current commit and branch you are working on
  • ..more..

.git can be opened and modified in text editor

Getting a Git repository

  • Initalize Repository into an existing directory
  • Cloning a Repository from remote server (github, bitbucket, etc.)

Initializing Repository

              
              $ mkdir [project-name]
              $ cd [project-name]
              $ git init
              
            
              
                    git-example/
                      .git/
                        config
                        description
                        HEAD
                        hooks/
                        info/
                        objects/
                        refs/
              
            

Cloning existing repository

Cloning creates your own personal copy of a repository.

              
        $ git clone [git-url]
        $ git clone https://github.com/vmaliwal/git-collab.git
              
            

Commit Objects

A commit object creats a snapshot of files in your project at a given point in time. It's a sort of bookmark that you can revisit at a later point in time.

Git is DAG that holds snapshots of commit objects as nodes

What does commit object hold?

  • reference to parent commit, if any
  • tree - set of files conveying current state of the files in the commit
  • author info, commiter info, commit message, etc.

Commit object is uniquely identified by a 40-character SHA1 hash

Creating a commit object

Create a new file or modify existing

              
            $ echo "Introduction to Git" > README
              
            
              
            $ git status
            # On branch master
            #
            # Initial commit
            #
            # Untracked files:
            #   (use "git add [file]..." 
            # to include in what will be committed)
            #
            # README
              
            
              
                $ git add [file-name]
                $ git commit -m "Initial commit"
              
            

Also

              
                $ git add .
                $ git commit -a -m "Initial commit"
              
            

My personal workflow

  1. Intialize/Clone repo.
  2. Make changes
  3. $ git status - to verify what files I have changed
  4. $ git diff [file-name] - to review a change made in a specific file
  5. $ git commit -a -m "commit message"

Branching

  • Collection of commit objects
  • Default branch is master
              
               HEAD -->  * fe1e7c5 Updated image 1
                         * 234954a Added images
                         * dce32e6 Initial commit 
              
            

HEAD points to commit (3) on master branch

Switching to a new branch

              
                # creates a new branch
                $ git branch [new-branch-name] 

                # move HEAD to point to new branch
                $ git checkout [new-branch-name] 
              
            
              
                $ git checkout -b [new-branch-name]
              
            

My personal workflow

  1. Create a new branch
  2. Make code changes
  3. Commit changes
  4. Run tests on code
  5. If test success merge those changes back to master branch
  6. It is considered good practice to always have master contain runnable and compilable code.

Merging

              $ git merge [branch-name]
            

Before merging

              
                COMMIT 1 --> * cd9e5f2 updated share url
                COMMIT 2 --> * b7df983 added agenda
                             |\  
                COMMIT 3 --> | * 6936cfc added some definitions
                COMMIT 4 --> * | 64d856e fixed typos 
              
            

$ git merge bug-fix

              
                COMMIT 1 --> * cd9e5f2 updated share url
                COMMIT 2 --> * b7df983 added agenda
                             |\  
                COMMIT 3 --> | * 6936cfc added some definitions
                COMMIT 4 --> * | 64d856e fixed typos 
                             |/  
                COMMIT 5 --> * 3bc4271 merged branch
              
            

(5) contains changes between (3) and (4) merged together

How to Collaborate?

  • Remote repository
  • Pull remote changes
  • Push local changes to remote

Remote repository

  • Create a new remote repository(on github, bitbucket, etc.) and clone to your local machine. Or,
  • Add existing git repo from your local machine to remote

How to create a remote repo?

How to create a remote repo?

Clone to your machine

              
      $ git clone [git-remote-repo-url].git
              
            

As name suggests clone command can be used to clone any remote repository

Add existing repo

  • Create new repository on github or bitbucket
  • On your local machine..
              
      $ git remote add origin [git-remote-repo-url].git
      $ git push origin master
              
            

origin is a remote repository reference that git uses

Pulling remote changes

              
      $ git pull [remote-repo-reference] [remote-branch-name]
      $ git pull origin master
              
            

Pushing changes to remote

              
      $ git push [remote-repo-reference] [remote-branch-name]
      $ git push origin master
              
            

.gitignore the unsung hero

  • It is very important to have this file in your project root directory to avoid merge conflicts from auto-generated code from VM, IDE, etc.
  • This also prevents binaries and other build objects from being accidentally added.
  • Google for gitignore nodejs/golang/java/android/iOS

Merge conflicts

While pulling or merging a branch merge conflicts can occur due to conflicting changes

              
                $ git checkout -b develop
              
            
              
              $ git branch
              * develop
                master 
              
            
              $ git checkout master
            
              
  $ git merge develop
  Auto-merging app.js
  CONFLICT (content): Merge conflict in app.js
  Automatic merge failed; fix conflicts and then commit the result.
              
            

$ git mergetool

My personal collaboration workflow

  1. Create new remote git repository
  2. Intialize git into existing project(if exist and skip step 3)
  3. Clone remote repository into local machine
  4. Add project/framework specific .gitignore file
  5. $ git commit -a -m "initial commit"
  6. $ git push origin master
  7. Make sure master branch only contains runnable/working code
  8. Create develop branch $ git checkout -b develop
  9. Add new changes, if any, and push develop to remote $ git push origin develop

..continue

  1. Ask developer friend to clone remote repository
  2. Crank out more code
  3. Commit changes
  4. If pushing to remote make sure to pull if there are any existing commit on remote
  5. If code looks okay, merge develop to master

Thank You

References