Git checkout vs reset vs reset –hard

0. Initial status for all 3 cases below:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
 (use "git push" to publish your local commits)
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
 modified: committed.txt
 modified: pushed.txt
 new file: staged.txt
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)
 modified: committed.txt
 modified: pushed.txt
 modified: staged.txt
Untracked files:
 (use "git add <file>..." to include in what will be committed)
 working-directory.txt
$ ls
committed.txt pushed.txt staged.txt working-directory.txt
$ cat committed.txt
1st
2nd
3rd
$ cat pushed.txt
1st
2nd
3rd
4th
$ cat staged.txt
1st
2nd
$ cat working-directory.txt
1st
$ git diff
diff --git a/committed.txt b/committed.txt
index c8cac97..87cfd71 100644
--- a/committed.txt
+++ b/committed.txt
@@ -1,2 +1,3 @@
 1st
 2nd
+3rd
diff --git a/pushed.txt b/pushed.txt
index 87cfd71..4d5110c 100644
--- a/pushed.txt
+++ b/pushed.txt
@@ -1,3 +1,4 @@
 1st
 2nd
 3rd
+4th
diff --git a/staged.txt b/staged.txt
index 2a5d015..c8cac97 100644
--- a/staged.txt
+++ b/staged.txt
@@ -1 +1,2 @@
 1st
+2nd
$ git diff working-directory.txt
$ git diff --cached
diff --git a/committed.txt b/committed.txt
index 2a5d015..c8cac97 100644
--- a/committed.txt
+++ b/committed.txt
@@ -1 +1,2 @@
 1st
+2nd
diff --git a/pushed.txt b/pushed.txt
index c8cac97..87cfd71 100644
--- a/pushed.txt
+++ b/pushed.txt
@@ -1,2 +1,3 @@
 1st
 2nd
+3rd
diff --git a/staged.txt b/staged.txt
new file mode 100644
index 0000000..2a5d015
--- /dev/null
+++ b/staged.txt
@@ -0,0 +1 @@
+1st
$ git diff --cached working-directory.txt
$ git diff HEAD
diff --git a/committed.txt b/committed.txt
index 2a5d015..87cfd71 100644
--- a/committed.txt
+++ b/committed.txt
@@ -1 +1,3 @@
 1st
+2nd
+3rd
diff --git a/pushed.txt b/pushed.txt
index c8cac97..4d5110c 100644
--- a/pushed.txt
+++ b/pushed.txt
@@ -1,2 +1,4 @@
 1st
 2nd
+3rd
+4th
diff --git a/staged.txt b/staged.txt
new file mode 100644
index 0000000..c8cac97
--- /dev/null
+++ b/staged.txt
@@ -0,0 +1,2 @@
+1st
+2nd
$ git diff HEAD working-directory.txt
$ git diff
HEAD master origin/master
$ git diff origin/master
diff --git a/committed.txt b/committed.txt
new file mode 100644
index 0000000..87cfd71
--- /dev/null
+++ b/committed.txt
@@ -0,0 +1,3 @@
+1st
+2nd
+3rd
diff --git a/pushed.txt b/pushed.txt
index 2a5d015..4d5110c 100644
--- a/pushed.txt
+++ b/pushed.txt
@@ -1 +1,4 @@
 1st
+2nd
+3rd
+4th
diff --git a/staged.txt b/staged.txt
new file mode 100644
index 0000000..c8cac97
--- /dev/null
+++ b/staged.txt
@@ -0,0 +1,2 @@
+1st
+2nd
1. Restore working files from index (revert working directory changes excluding non-tracked files):
$ git checkout .
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
 (use "git push" to publish your local commits)
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
 modified: committed.txt
 modified: pushed.txt
 new file: staged.txt
Untracked files:
 (use "git add <file>..." to include in what will be committed)
 working-directory.txt
$ cat pushed.txt
1st
2nd
3rd
$ cat committed.txt
1st
2nd
$ cat staged.txt
1st
$ cat working-directory.txt
1st
$ git diff
$ git diff --cached
diff --git a/committed.txt b/committed.txt
index 2a5d015..c8cac97 100644
--- a/committed.txt
+++ b/committed.txt
@@ -1 +1,2 @@
 1st
+2nd
diff --git a/pushed.txt b/pushed.txt
index c8cac97..87cfd71 100644
--- a/pushed.txt
+++ b/pushed.txt
@@ -1,2 +1,3 @@
 1st
 2nd
+3rd
diff --git a/staged.txt b/staged.txt
new file mode 100644
index 0000000..2a5d015
--- /dev/null
+++ b/staged.txt
@@ -0,0 +1 @@
+1st
$ git diff HEAD
diff --git a/committed.txt b/committed.txt
index 2a5d015..c8cac97 100644
--- a/committed.txt
+++ b/committed.txt
@@ -1 +1,2 @@
 1st
+2nd
diff --git a/pushed.txt b/pushed.txt
index c8cac97..87cfd71 100644
--- a/pushed.txt
+++ b/pushed.txt
@@ -1,2 +1,3 @@
 1st
 2nd
+3rd
diff --git a/staged.txt b/staged.txt
new file mode 100644
index 0000000..2a5d015
--- /dev/null
+++ b/staged.txt
@@ -0,0 +1 @@
+1st
$ git diff HEAD working-directory.txt
2. Reset index with current HEAD (committed state), opposite to git add. Does not affect any working directory files
$ git reset HEAD .
Unstaged changes after reset:
M committed.txt
M pushed.txt
$ ls
committed.txt pushed.txt staged.txt working-directory.txt
$ cat pushed.txt
1st
2nd
3rd
4th
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
 (use "git push" to publish your local commits)
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)
 modified: committed.txt
 modified: pushed.txt
Untracked files:
 (use "git add <file>..." to include in what will be committed)
 staged.txt
 working-directory.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ cat committed.txt
1st
2nd
3rd
$ cat pushed.txt
1st
2nd
3rd
4th
$ cat staged.txt
1st
2nd
$ cat working-directory.txt
1st
$ git diff
diff --git a/committed.txt b/committed.txt
index 2a5d015..87cfd71 100644
--- a/committed.txt
+++ b/committed.txt
@@ -1 +1,3 @@
 1st
+2nd
+3rd
diff --git a/pushed.txt b/pushed.txt
index c8cac97..4d5110c 100644
--- a/pushed.txt
+++ b/pushed.txt
@@ -1,2 +1,4 @@
 1st
 2nd
+3rd
+4th
$ git diff --cached
$ git diff HEAD
diff --git a/committed.txt b/committed.txt
index 2a5d015..87cfd71 100644
--- a/committed.txt
+++ b/committed.txt
@@ -1 +1,3 @@
 1st
+2nd
+3rd
diff --git a/pushed.txt b/pushed.txt
index c8cac97..4d5110c 100644
--- a/pushed.txt
+++ b/pushed.txt
@@ -1,2 +1,4 @@
 1st
 2nd
+3rd
+4th
3. Reset working directory and index with HEAD version (non tracked files are not affected; but if a file was added to index but not committed, then it will be deleted from index and from working directory)
$ git reset --hard
HEAD is now at 70a5d6f 1x: committed.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
 (use "git push" to publish your local commits)
Untracked files:
 (use "git add <file>..." to include in what will be committed)
 working-directory.txt
nothing added to commit but untracked files present (use "git add" to track)
$ ls
committed.txt pushed.txt working-directory.txt
$ cat pushed.txt
1st
2nd
$ cat committed.txt
1st
$ cat working-directory.txt
1st
$ git diff
$ git diff --cached
$ git diff HEAD
$ git diff origin/master
diff --git a/committed.txt b/committed.txt
new file mode 100644
index 0000000..2a5d015
--- /dev/null
+++ b/committed.txt
@@ -0,0 +1 @@
+1st
diff --git a/pushed.txt b/pushed.txt
index 2a5d015..c8cac97 100644
--- a/pushed.txt
+++ b/pushed.txt
@@ -1 +1,2 @@
 1st
+2nd
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s