git basicS

HISTORY


GIT (NOUN)
      British English slang roughly equivalent to 
     "unpleasant person"


"I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'. " 
                                           Linus Torvalds



DECENTRALIZED

FAST, Small footprint


COMPOSABLE / FLEXIBLE



LINUX INSTALLATION



sudo apt-get install git

yum install git


CREATING A GIT REPO


~ $ git init blog 
Initialized empty Git repository in /home/kalmario/blog/.git/ 

~/blog $ ls -la
total 12K
drwxrwxr-x  3 kalmario kalmario 4.0K 2013-10-26 12:45 ./
drwxr-xr-x 49 kalmario kalmario 4.0K 2013-10-26 11:40 ../
drwxrwxr-x  8 kalmario kalmario 4.0K 2013-10-26 11:40 .git/
  • .git is directory tree all information of your repository
  • to un-git, simply delete .git folder

Cloning an Existing Repository


~ $ git clone https://github.com/larkarvin/TestRepo.git blog
Cloning into blog...
remote: Counting objects: 16, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 16 (delta 3), reused 16 (delta 3)
Unpacking objects: 100% (16/16), done. 

Setting UP your identity


git config --global user.name "Your Name"
git config --global user.email you@example.com

~/blog $ git config --global user.name "larkarvin"
~/blog $ git config --global user.email "arvin@almar.io"
~/blog $ git config --global -l user.name=larkarvin user.email=arvin@almar.io

REPOSITORY INFORMATION


~/blog $ git config -l
user.name=larkarvin
user.email=arvin@almar.io
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/larkarvin/TestRepo.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master 

WORKING TREE STATUS

svn status
git status

~/blog $ vim home.php
~/blog $ git status# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	home.php
nothing added to commit but untracked files present (use "git add" to track)

ADDING A FILE TO BE TRACKED


tells git to track a specific file
svn add
git add

~/blog $ git add home.php
~/blog $ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#	new file:   home.php
#

COMMITTING YOUR FIRST FILES


svn commit -m "First Commit"
git commit -m "First Commit"

 ~/blog $ git commit -m "First Commit"
[master (root-commit) f40d943] First Commit

git stage

Stores information that will go into your next commit or readying a file for commit
~/blog $ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   home.php
#
no changes added to commit (use "git add" and/or "git commit -a") 
~/blog $ git stage home.php
~/blog $ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#	modified:   home.php 
~/blog $ git commit -m "Commit Message"



UN-STAGING A FILE


~/blog $ git reset home.php
Unstaged changes after reset:
M	home.php
~/blog $ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   home.php
#
no changes added to commit (use "git add" and/or "git commit -a")
 

PUSH YOUR CHANGES FROM EXISTING REPO TO A REMOTE REPO


equivalent to your first svn import
~/blog $ git remote add origin https://github.com/larkarvin/TestRepo.git
~/blog $ git push -u origin master
Username: 
Password: 
Counting objects: 6, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 507 bytes, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/larkarvin/TestRepo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Git PULL


like svn update
Updates your local working copy
from your remote server
~/blog $ git pull
Password: 
remote: Counting objects: 2, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 41 (delta 27), reused 0 (delta 0)
Unpacking objects: 100% (41/41), done.
From https://github.com/larkarvin/TestRepo.git
   1ad00c7..320bd11  master     -> origin/master
Updating 1ad00c7..320bd11
Fast-forward
 home.php   |    8 ++
 1 files changed, 20 insertions(+), 13 deletions(-)
 

GIT PUSH


git push lets you move a local branch to
another or remote repository.

~/blog $ git push
Username: 
Password: 
Counting objects: 12, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (10/10), 807 bytes, done.
Total 10 (delta 3), reused 0 (delta 0)
To https://github.com/larkarvin/TestRepo.git
   6502f62..b8fd39d  master -> master 

GIT DIFF


svn diff [filename]
git diff [filename]
Show changes between the working tree or a tree
~/blog $ git diff home.php
diff --git a/home.php b/home.php
index 70c74b9..cf67afb 100644
--- a/home.php
+++ b/home.php
@@ -1,4 +1,4 @@
- <title> Hello World! </title>
+ <title> Hello Universe! </title>
 

BRANCHING

~/blog $ git branch -l
* master
~/blog $ git branch develop
~/blog $ git branch -l
  develop
* master 

BRANCH SWITCHING


    ~/blog $ git checkout develop
Switched to branch 'develop'
~/blog $ git branch -l
* develop
  master 
switch your working tree to a specified branch
 BEWARE: every uncommitted changes will be lost

MERGING


~/blog $ git checkout develop
Switched to branch 'develop'
~/blog $ vim home.php
~/blog $ git stage home.php
~/blog $ git commit -m "Commit Message"

~/blog $ git checkout master
Switched to branch 'master'
~/blog $ git merge develop
Merge made by recursive.
 home.php |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-) 

MOVING FILES

git mv
~/blog $ git mv home.php index.php
same as
~/blog $ mv home.php index.php
~/blog $ git rm home.php
~/blog $ git add index.php 

show log

git log
~/blog $ git log
commit b8fd39d4ca134662d44c1a90c2d95c16ff97f183
Author: larkarvin 
Date:   Sat Oct 26 13:45:31 2013 +0800

    Change Title

commit 98fed4c365e56309569cb03402e60a263b992350
Merge: a3e8b02 abf9968
Author: larkarvin 
Date:   Sat Oct 26 10:55:48 2013 +0800

    Merge branch 'develop' 

with date filter
git log --since=2.weeks

REVERTING CHANGES


like svn revert
Reverts your changes

~/blog $ git checkout home.php
~/blog $ git diff home.php 


.gitignore


  • .gitignore  file contains files 
  • that will be ignored by your repo
  • resides on your working tree's root dir


~/blog $ cat .gitignore
*.jpg
*.png
/includes/local-config.php 

GIT TAG

same as svn tag
~/blog $ git tag v3.0 


Listing your tags


~/blog $ git tag -l 'v1.1.*'v1.1.2
v1.1.3
v1.1.4

gitFLOW

GITFLOW


  • A strict branching model designed around the project release.
  • Formulated by Vincent Driessen
  • Provides a robust framework for managing larger projects
  • Perfect for large development teams

HISTORICAL BRANCHES


Record the history of the projects

  • master - stores the official release history (tags)
  • develop - integration branch of features

FEATURE BRANCH

  • May branch off from: develop
  • Must merge back into: develop
  • Branch naming convention: anything except master, develop, release-*, or hotfix-*

RELEASE BRANCH


  • Bug fixes, documentation generation, and other release-oriented tasks should go in this branch.
  • Frees up development branch and ensures no new features will be added to the release

RELEASE BRANCH

  • May branch off from: develop
  • Must merge back into: develop and master
  • Branch naming convention: release-*

HOT FIX BRANCH


  • Used to quickly patch production releases.
  • Only branch that should fork directly off of master.

NEXT TOPICS:

  • DIFFERENT MERGING STRATEGIES
  • RESOLVING CONFLICTS


References:

  • https://www.atlassian.com/git/workflows
  • git man pages



THANK YOU!!


https://slid.es/larkarvin/git-basics/
http://almar.io
@larkarvin



Karl Arvin Almario 

October 26, 2013
Solutions Architect at US AUTOPARTS NETWORK

Made with Slides.com