Sunday, 21 July 2019

git diff vs git diff --staged


Let’s see the difference between 'git diff' and 'git diff --staged' with an example.

Step 1: Create a directory 'demoRepo' and naviagte the 'demoRepo' directory and execute 'git init .' command.
$mkdir demoRepo
$
$cd demoRepo/
$git init .
Initialized empty Git repository in /Users/krishna/Documents/TechnicalDocuments/git/demoRepo/.git/

Step 2: Create a file ‘hello.txt’ and commit.

hello.txt
First Line

$git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 hello.txt

nothing added to commit but untracked files present (use "git add" to track)
$
$git add hello.txt 
$
$git commit -m "Adding hello.txt file"
[master (root-commit) f0e104d] Adding hello.txt file
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt


Step 3: Update hello.txt file.

hello.txt
First Line
Second Line


Step 4: Execute git diff on hello.txt file.
$git diff hello.txt 
diff --git a/hello.txt b/hello.txt
index 603cb1b..41b6522 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
 First Line
+Second Line


Add the file 'hello.txt' to staging area and execute 'git diff' again.
$git add hello.txt
$
$git diff hello.txt 
$


Since file 'hello.txt' is in staging area, 'git diff' do not show any difference, use 'git diff --staging' to see the difference between staging and indexed file.

$git diff --staged hello.txt 
diff --git a/hello.txt b/hello.txt
index 603cb1b..41b6522 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
 First Line
+Second Line


Previous                                                    Next                                                    Home

No comments:

Post a Comment