Git History

Javier Negre @ tnwlabs

Staging area

Git directory:

# On branch master
nothing to commit (working directory clean)

Working directory:

# Changes not staged for commit:
  modified: change-in-file-1.php
  modified: change-in-file-2.js
  modified: change-in-file-3.css

Staging area

Staging area + working directory:

$ git add change-in-file-1.php
$ git status
# Changes to be committed:
  modified: change-in-file-1.php
# Changes not staged for commit:
  modified: change-in-file-2.js
  modified: change-in-file-3.css
$ git commit -m "Change in file 1"
$ git status
# Changes not staged for commit:
  modified: change-in-file-2.js
  modified: change-in-file-3.css

Staging area

Staging area + working directory:

$ git add -p
  ...  
# Changes to be committed:
  modified: change-in-file-2.js
# Changes not staged for commit:
  modified: change-in-file-2.js
  modified: change-in-file-3.css
$ git commit -m "New function 'a' in file 2"
$ git status
# Changes not staged for commit:
  modified: change-in-file-2.js
  modified: change-in-file-3.css

change-in-file-2.js

function a () {
  console.log("Hey, I'm a");
  // And I'll be staged
}
 
function b () {
  console.log("Hey, I'm b");
  // But I'm not ready yet
}

Staging area

git diff

$ git diff
# Shows working directory differences
 
$ git diff --staged # --cached also works
# Shows staging area differences

Atomic commits

$ git log --oneline
109efcf Remove not so AWESOME feature 1 :(
7454c97 Add new AWESOME page and features
$ git log --oneline
31cf49c Remove not so AWESOME feature 1, reverts 8dddd5f :(
77ed5a3 Add AWESOME feature 2 :)
8dddd5f Add AWESOME feature 1 :)
dffc429 Add new AWESOME view
c80078c Add new AWESOME controller
63a8f26 Add new AWESOME model

Atomic commits

git add -p

$ git add -p
    ... code ...
-     line removed #1
+     line added #1
+     line added #2
    ... code ...
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y
    ... code ...
-     line removed #2
+     line added #3
    ... code ...
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? n

Atomic commits

git add -p options

Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? ?
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

Atomic commits

Useful commands

$ git add -i
# Interactive mode to manage the staging area
# Useful to add untracked files
$ git reset -p
# Moves code hunks from 'staging area' to 'working directory'
$ git checkout -p
# Removes code hunks from the 'working directory'
# Be careful! You can remove valid code by accident

Managing local history

local commits = commits that have not been pushed to origin

Managing local history

git commit --amend

$ git commit -m "AWESOME feature is ready to rock"
9d4361a1 AWESOME feature is ready to rock
$ git add file_modified.php
$ git diff --staged
    ... code ...
-     var_dump('This text should not be displayed');
    ... code ...
$ git commit --amend
3ea652b9 AWESOME feature is ready to rock

Managing local history

git cherry-pick

$ git commit -m "New feature in master"                   #1
$ git checkout master -b dev
 ...
$ git commit -m "New feature in dev, 1st commit"          #2
 ...
$ git commit -m "New feature in dev, WIP"                 #3
 ...
$ git commit -m "Quick hotfix for new feature in master"  #4
// Dev branch log:
0452505 Quick hotfix for new feature in master
4e31657 New feature in dev, WIP

Managing local history

git cherry-pick

$ git checkout master
$ git cherry-pick 0452505
$ git log --oneline
62c5c15 Quick hotfix for new feature in master # new hash!
51b5a11 New feature in master
 ...

Managing local history

git reset --hard

# Back to dev branch
0452505 Quick hotfix for new feature in master   # HEAD
4e31657 New feature in dev, WIP                  # HEAD~1
fce3412 New feature in dev, 1st commit           # HEAD~2
$ git reset --hard HEAD~1
# or
$ git reset --hard 4e31657
# You'll remove the commit and its code!
$ git log
4e31657 New feature in dev, WIP                  # HEAD
fce3412 New feature in dev, 1st commit           # HEAD~1

Managing local history

git reset

4e31657 New feature in dev, WIP                  # HEAD
fce3412 New feature in dev, 1st commit           # HEAD~1
$ git reset HEAD
$ git log --oneline
fce3412 New feature in dev, 1st commit           # HEAD
$ git status
# Changes to be committed:
  modified: file-added-in-dev-branch.js

Managing local history

git revert

ff3ebe1 Add banner images
02cb308 Set font to Comic Sans            #1
7a1d1b7 Add users table
$ git revert 02cb308
cf09a5c Revert "Set font to Comic Sans"   #2
ff3ebe1 Add banner images
02cb308 Set font to Comic Sans            #1
7a1d1b7 Add users table

#1

-  font-family: Helvetica;
+  font-family: Comic-Sans;

#2

-  font-family: Comic-Sans;
+  font-family: Helvetica;

Questions?

◂ negre.co