Git

  • What is Git
  • Installing Git
  • Getting Started
  • Git Concepts & Architecture
  • Making Changes to files
  • Using Git in a Real Project
  • Undoing Changes
  • Ignoring Files
  • Branching
  • Merging
  • Stashing Changes
  • Remotes
  • Tools

What is Git ?

About Distributed Version Control

Subversion Tracks Versions (Of a master copy, developers owns a working copies)

Git Tracks Changes (Stored as change sets shared between repositories)

Subversion User: Do we have our working directory files in a particular version of master copy files ?

Git User: Do we have a particular change sets applied to our repository/staged Area working directory files ?

Installing Git

Install on Windows

(Click Git Logo)

Configuration

Configuration Level System Global (User) Project
Configuration file location Program Files\Git\etc\gitconfig $HOME\.gitconfig project_folder\.git\config
$ git config --local
$ git config --global
$ git config --system

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

# Options :
# --list (-l), --edit (-e)

Help

$ git help <verb>

# Or

$ git <verb> --help

# Or

$ man git-<verb>

Getting Started

Git init

.../project_folder $ git init

Tell Git to start tracking

.git

.git folder is the Git workspace

To untrack a project, just remove the .git folder

.svn everywhere...

First Commit

# check files status
$ git status

# tell git to add current folder to what will be committed
$ git add .

# check files status
$ git status

# tell git to track this changes (not this version)
$ git commit -m "init"

# check files status
$ git status

Log

$ git log -n 10

$ git log --since=2012-06-15

$ git --until=2012-06-18

$ git log --author "maeljirari"

$ git grep "init"

Git Concepts

&

Architecture

3-Trees Architecture

Working Directory

Repository

commit

update

2-Trees Architecture

3-Trees Architecture

Git Local Workflow & Files Status Life cycle

$ git status

SHA-1

"Git generates a checksum for each committed change set."

If data changes, checksum changes => Data integrity

5a8e8f

a99ea4

98e44f

WD

WD

WD

SA

SA

SA

Repository

Repository

Repository

commit

commit

commit

commit

commit

add

add

add

HEAD Pointer

Commit : pointer to changes stored as change sets (snapshot of changed files)

HEAD : pointer to the parent of the next commit in the current checked branche

Undoing Changes

Undoing Working Directory Changes

# checkout particular file
$ git checkout -- <file>

# checkout the whole branch
$ git checkout <branch>

Unstaging Files (change sets, snapshots)

# changes will persist in our working directory, reset only the staging area
# to look like repository
$ git reset HEAD <file>

Amending Commit

# change last commit, commit that HEAD still point to
$ git commit --amend -m "commit message"

Retrieve an Old Version (Change set, snapshot)

# retrieve an old version of a file and put it directly to staging area
$ git checkout <commit> -- <file>

# commit
$ git commit -m "commit message"

Revert a Commit

# create a mirror commit that abort previous commit, will do a commit
$ git revert <commit>


# create a mirror commit that abort previous commit, will stage it
$ git revert -in <commit>

Local and remote repositories

Reset to Undo a Commit

# for local repository
# make a commit and move HEAD pointer
# to point to that commit

$ git reset <commit>

Only Local repositories

Soft Reset

  • Move HEAD pointer in repository
  • Doesn't change Staging Area
  • Doesn't change Working Directory

Mixed Reset (default behaviour)

  • Move HEAD pointer in repository
  • Change Staging Area to match repository
  • Doesn't change Working Directory

Hard Reset

  • Move HEAD pointer in repository
  • Change Staging Area to match repository
  • Change Working Directory to match repository

Making Changes to files

Add Files

$ git add <pathspec>

# add current folder
$ git add .

# add file
$ git add <file>

Add files to staging index (to what will be committed)

Edit Files

Viewing Changes

$ git diff

Viewing Only Staged Changes

$ git diff --cached

Delete Files

# remove the file only from the Git repository and the filesystem
$ git rm <file>
$ git commit -m "remove file"


# remove the file only from the Git repository 
# and not remove it from the filesystem
$ git rm --cached <file>

Move and Rename Files

# move file
$ git mv <source> <destination>

# rename file
$ git rm <file>

Using Git in a Real Project

Discussion

Discussion

Discussion

Ignoring Files

.gitignore Files

  • project_folder/.gitignore
# basic regular expression

* ? [aeiou] [0-9]

# negate expressions with !

*.txt
!myFile.txt

# ignore all files in a directory with a trailing slash

assets/videos/

project_folder/.gitignore

Ignoring Files Globally

# global ignore file, used by all git repositories (not committed, not pushed)
$ git config --global core.excludesfile '~/.gitignore'

Add a  .gitignore file to the home directory

Ignoring Tracked Files

Files must be untracked before being ignored

remove untracked files from the repository and working copy

# remove file from repository and working directory
$ git rm <file>

remove untracked files from the staging index

# remove file from staging index only, copy still in working
# directory and repository
$ git rm --cached <file>

clean untracked files

# remove all untracked files, dangerous
$ git clean

# safer option
$ git stash --all

Tracking Empty Directories

add an empty file named .gitkeep

Branching

Branching Overview

Text

branch = list of commits

Create Branches

# create a testing branch
$ git branch testing

Switching Branches

$ git status

# checking out test branch
$ git checkout test


# checking out master branch
$ git checkout master

Switching on a clean directory

Switching Branches with Uncommitted Changes

Commit your changes or stashes them

then

siwtch branch

Comparing Branches

# order is not important
$ git diff <branch1>..<branch2>

# show branches that are completely merged with the current branch
$ git branch --merged

Renaming Branches

# rename the current branch
$ git branch -m <new_name>

# rename branch
$ git branch -m <old_name> <new_name> 

Deleting Branches

# delete a local merged branch
git branch -d <branch>

# delete a local not merged branch
git branch -D the_local_<branch>

Merging

Merging Overview

Fast Forward Merge VS True Merge

True Merge

1

2

Rebasing

1

3

4

2

Simple divergent history

Merging to integrate diverged work history

Rebasing the change introduced in C4 onto C3

Fast-forwarding the master branch

Advanced Rebasing

A history with a topic branch off another topic branch

Rebasing a topic branch off another topic branch

Fast-forwarding your master branch to include the client branch changes

Rebasing your server branch on top of your master branch

Final commit history

1

2

3

4

5

Merging Conflicts

Classical

Resolve Merging Conflicts

Classical

Strategies to Reduce Merging Conflicts

  • Keep lines short
  • Keep commit small and focused
  • Beware stray edits to white space (spaces, tabs, returns)
  • Merge often
  • Track changes to master

Stashing Changes

Stashing Changes in the Stash

"Stashing takes the dirty state of your working directory – that is, your modified tracked files and staged changes – and saves it on a stack of unfinished changes that you can reapply at any time."

$ git status

$ git stash

$ git status

Viewing Stashed Changes

$ git stash list

Retrieving Stashed Changes

# apply last stash

$ git stash apply

# apply specific stash
$ git stash list

$ git stash apply stash@{1}

# create a branch from stash
$ git stash branch <branch>

Deleting Stashed Changes

$ git stash list

$ git stash drop stash@{1}

$ git stash list

Remotes

Local & Remote Repositories

Add Remote Repositories

$ git remote

$ git remote -v

$ git remote show

$ git remote show <remote_name>

$ git remote add <remote_name> <remote_url>

Create Remote Branches

$ git push <remote_name> <branch_name>

$ git push -u <remote_name> <branch_name>

Clone Remote Repositories

Server and local repositories after cloning

Fork Remotes Repositories

No more connection between remote repositories

Track Remote Branches

$ git checkout -b [local_branch_name] [remote_name]/[remote_branch_name]

$ git branch -u origin/serverfix

Push Changes to Remotes

$ git push <remote_name> <branch_name>

Fetch Changes From Remotes

# fetch all changes that you don't have yet
$ git fetch <remote_name> <branch_name>

Merge Fetched Changes

$ git merge <branch_name>

$ git merge --rebase <branch_name>

Checking Out Remote Branches

$ git checkout -b [branch] [remotename]/[branch]

Push Changes to an Updated Remote Branches

Pull before Push

Delete Remote Branches

$ git push <remote_name> --delete <branch_name>

Collaborative Workflow

Tools

SSH Key

(click Github Logo to access SSH Help)

Git With an IDE

(click EGit Logo)

Git Hosting

Next : SASS Essentials

Git

By Mohammed Amine EL JIRARI

Git

Presentation about Git versionning.

  • 881