Git
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2620503/Git-Icon-1788C.png)
- 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 ?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632393/two-repos-centralized-with-updates.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632401/two-repos-and-master-distributed.png)
Installing Git
Install on Windows
(Click Git Logo)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632819/do-you-need-git.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632450/staging.png)
Git Local Workflow & Files Status Life cycle
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632536/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3134343739382f3834303334342f65333562303938652d663336662d313165322d383462322d6536653463393131336165612e706e67.png)
$ 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)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632495/IY4PK.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632497/commit-tree-blob.png)
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>
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635980/revert.png)
Local and remote repositories
Reset to Undo a Commit
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635985/reset.png)
# for local repository
# make a commit and move HEAD pointer
# to point to that commit
$ git reset <commit>
Only Local repositories
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2636017/git-tutorial_reset-commit.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632536/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3134343739382f3834303334342f65333562303938652d663336662d313165322d383462322d6536653463393131336165612e706e67.png)
Viewing Changes
$ git diff
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2634906/GitDiffSimple.png)
Viewing Only Staged Changes
$ git diff --cached
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2634906/GitDiffSimple.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635357/gitflow_branches.png)
Discussion
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635085/capture_stepup1_5_6.png)
Discussion
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635619/03-sprint.gif)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635085/capture_stepup1_5_6.png)
Text
branch = list of commits
Create Branches
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635119/two-branches.png)
# create a testing branch
$ git branch testing
Switching Branches
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635111/head-to-testing.png)
$ 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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635085/capture_stepup1_5_6.png)
Fast Forward Merge VS True Merge
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635129/FastForwardMerge.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635132/TypicalMerge.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635134/basic-merging-1.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635138/basic-merging-2.png)
True Merge
1
2
Rebasing
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635165/basic-rebase-1.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635167/basic-rebase-2.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635172/basic-rebase-3.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635177/basic-rebase-4.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635400/interesting-rebase-1.png)
A history with a topic branch off another topic branch
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635406/interesting-rebase-2.png)
Rebasing a topic branch off another topic branch
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635411/interesting-rebase-3.png)
Fast-forwarding your master branch to include the client branch changes
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635417/interesting-rebase-4.png)
Rebasing your server branch on top of your master branch
Final commit history
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635424/interesting-rebase-5.png)
1
2
3
4
5
Merging Conflicts
Classical
Resolve Merging Conflicts
Classical
Strategies to Reduce Merging Conflicts
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635189/conflicts_meme.jpg)
- 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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632811/sta.png)
Viewing Stashed Changes
$ git stash list
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2636243/git-stash-stashing-changes.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635246/remote-branches-1.png)
Server and local repositories after cloning
Fork Remotes Repositories
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635246/remote-branches-1.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632802/imajjges.png)
Tools
SSH Key
(click Github Logo to access SSH Help)
Git With an IDE
(click EGit Logo)
Git Hosting
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2632737/github-logo.jpg)
Next : SASS Essentials
![](https://s3.amazonaws.com/media-p.slid.es/uploads/332149/images/2635486/color-1c4aab2b.png)
Git
By Mohammed Amine EL JIRARI
Git
Presentation about Git versionning.
- 881