Welcome to our comprehensive guide on git diff! This tutorial is designed to help both beginners and intermediate learners understand and effectively use git diff in their projects. Let's dive in!
git diffgit diff is a command that shows the differences between your working directory and the last commit, or between two specific commit versions. It's a powerful tool for comparing code and tracking changes in your project.
š” Pro Tip: git diff is often used to preview changes before committing them.
Before we get started, ensure you have Git installed on your machine. If not, you can download it from Git official website.
git diffLet's create a simple project and see git diff in action.
mkdir my-project
cd my-projectgit initapp.js and add some code:echo 'console.log("Hello World!");' > app.jsecho 'console.log("Welcome to my project!");' >> app.jsgit diff:git diffThis command will output the changes made to the file, like so:
diff --git a/app.js b/app.js
index 8e2734e..8f6403b 100644
--- a/app.js
+++ b/app.js
@@ -1 +1,2 @@
console.log("Hello World!");
+console.log("Welcome to my project!");
š Note: The output shows the differences between the versions of the file. The lines starting with - indicate deleted lines, while lines starting with + show added lines.
git diffgit diff can also compare two specific commit versions. Let's create a new commit and compare it with the initial commit:
git add app.js
git commit -m "Add welcome message"git diff HEAD~1This command will show the differences between the initial commit and the latest commit.
Now that you have a basic understanding of git diff, let's put it into practice.
What does `git diff` command show in a Git project?
In the next lesson, we'll explore more advanced usage of git diff and learn how to use it effectively in your projects. Stay tuned! š