Git working directory/stage area/committed

echo "committed" > file.txt
git status
On branch master
Initial commit
Untracked files:
 (use "git add <file>..." to include in what will be committed)
 file.txt
nothing added to commit but untracked files present (use "git add" to track)
git add file.txt
git status
On branch master
Initial commit
Changes to be committed:
 (use "git rm --cached <file>..." to unstage)
 new file: file.txt
git commit -m "committed" file.txt
[master (root-commit) 5e9cc5f] committed
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
 (use "git branch --unset-upstream" to fixup)
nothing to commit, working directory clean

echo "staged" >> file.txt
git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
 (use "git branch --unset-upstream" to fixup)
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: file.txt
no changes added to commit (use "git add" and/or "git commit -a")
git add file.txt
git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
 (use "git branch --unset-upstream" to fixup)
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
 modified: file.txt

echo "working directory" >> file.txt
git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
 (use "git branch --unset-upstream" to fixup)
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
 modified: file.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: file.txt

git diff file.txt # working dir vs index
diff --git a/file.txt b/file.txt
index 54bef0c..d237daf 100644
--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,3 @@
 committed
 staged
+working directory

git diff --cached file.txt # index vs most recent commit
diff --git a/file.txt b/file.txt
index d0fe822..54bef0c 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,2 @@
 committed
+staged

git diff HEAD file.txt # working dir vs most recent commit
diff --git a/file.txt b/file.txt
index d0fe822..d237daf 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,3 @@
 committed
+staged
+working directory


tortoise-git-diff


intellij-git-diff

Let's check if there were any remote changes to origin/master

git fetch origin master
From https://github.com/bmwieczorek/my-git2
 * branch master -> FETCH_HEAD
git diff FETCH_HEAD file.txt # working dir vs FETCHED HEAD from origin master
diff --git a/file.txt b/file.txt
index d0fe822..d237daf 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,3 @@
 committed
+staged
+working directory

As there were not changes then diff FETCH_HEAD, diff HEAD, diff master, diff origin/master show the same result.

NOW ANOTHER USER PUSHES A CHANGE 5e9cc5f..c82cd74 (append 'line added by another user') TO REMOTE/ORIGIN/MASTER and in order to see that change we need to fetch it:

git fetch origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/bmwieczorek/my-git2
 * branch master -> FETCH_HEAD
 5e9cc5f..c82cd74 master -> origin/master

git diff FETCH_HEAD file.txt # working dir vs latest fetched from remote repo; the same result would give here 'git diff origin/master'
diff --git a/file.txt b/file.txt
index 45822f2..d237daf 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
 committed
-line added by another user
-
+staged
+working directory

git diff master file.txt # working dir vs local master (fetch does not merge with remote master changes); the same result would be for 'git diff HEAD' 
diff --git a/file.txt b/file.txt
index d0fe822..d237daf 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,3 @@
 committed
+staged
+working directory

REVERTING / DISCARDING CHANGES:

1. Permanently override working directory with these files that were added to index with their index version. If a file is only in working directory and it was not added to the index then it will not be deleted from working directory. If a file was recently deleted from working directory but was previously added to the index then it will be restored in working directory with its index version.

BEFORE:
cat file.txt
committed
staged
working directory
git diff file.txt
diff --git a/file.txt b/file.txt
index 54bef0c..d237daf 100644
--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,3 @@
 committed
 staged
+working directory

git checkout file.txt

AFTER:
cat file.txt
committed
staged
git diff file.txt
<empty>

2. Revert ONLY index modifications (working dir changes stays) with HISTORY (committed) version:

BEFORE:
cat file.txt
committed
staged
working directory
git diff file.txt
diff --git a/file.txt b/file.txt
index 54bef0c..d237daf 100644
--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,3 @@
 committed
 staged
+working directory
git diff --cached file.txt
diff --git a/file.txt b/file.txt
index d0fe822..54bef0c 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,2 @@
 committed
+staged

git reset file.txt
Unstaged changes after reset:
M file.txt

AFTER:
cat file.txt
committed
staged
working directory
git diff file.txt
diff --git a/file.txt b/file.txt
index d0fe822..d237daf 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,3 @@
 committed
+staged
+working directory
git diff --cached file.txt
<empty>

3. Revert working directory and index and override it with committed version (do not affect working directory file that was not added to index nor committed; delete file that was only added to the index but not committed)

git reset --hard
HEAD is now at 5e9cc5f committed
cat file.txt
committed
git diff file.txt
git diff --cached file.txt
git diff HEAD file.txt
git diff FETCH_HEAD file.txt
diff --git a/file.txt b/file.txt
index 45822f2..d0fe822 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1 @@
 committed
-line added by another user
-

NOTE: use the following command:

cat git.txt | egrep '^echo|^git'

to reply my commands, result:

echo "committed" > file.txt
git status
git add file.txt
git status
git commit -m "committed" file.txt
git status
echo "staged" >> file.txt
git status
git add file.txt
git status
echo "working directory" >> file.txt
git status
git diff file.txt # working dir vs index
git diff --cached file.txt # index vs most recent commit
git diff HEAD file.txt # working dir vs most recent commit
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