To show the changes that have been staged in Git, you can use the git diff --staged
command. This command compares the changes between the staged (index) state and the last committed state. Here's an example:
Suppose you have made changes to two files and staged those changes. Here's how you can use git diff --staged
to see the staged changes:
Make Changes and Stage Them:
Create two files and add some content to them.
sh
echo "This is file 1" > file1.txt
echo "This is file 2" > file2.txt
git add file1.txt file2.txt
View Staged Changes:
Run the git diff --staged
command to view the differences between the staged changes and the last commit.
sh
git diff --staged
The output will show the differences for the staged changes.
Example output:
diff
diff --git a/file1.txt b/file1.txt index 3a15ff1..a0deffe 100644 --- a/file1.txt +++ b/file1.txt @@ -1 +1 @@ -This is file 1 +Updated content for file 1 diff --git a/file2.txt b/file2.txt index e69de29..1a9bfb0 100644 --- a/file2.txt +++ b/file2.txt @@ -0,0 +1 @@ +This is file 2
The git diff --staged
command compares the staged changes with the last commit. The output displays the differences for each file that has staged changes.
This command is helpful for reviewing the changes you are about to commit before actually making the commit.
Comments
Post a Comment