The Power of Git: A Comprehensive Tutorial to Track File Changes

Git, the industry-standard version control system, is a powerful tool for tracking changes in your software projects. Whether you’re a seasoned developer or just starting your coding journey, understanding how to check and visualize file changes in Git is a crucial skill.

In this comprehensive guide, we’ll delve into over 15 different methods and tools that Git offers to help you track and understand file changes in your projects. Whether you need a quick status check, a detailed code diff, or an in-depth dive into your project’s commit history, Git provides a wide array of options to meet your version control needs.

We’ll explore commands like git status for quick status overviews, git diff for detailed code comparisons, and git log for visualizing your project’s history. We’ll also delve into more advanced techniques, such as using graphical Git clients and web-based platforms for collaboration and issue tracking.

Additionally, we’ll cover features like word-level differences, line-ending checks, and options for detecting renames and copies of files. We’ll even explore the visualization of complex branching and merging scenarios using specialized tools. Whether you’re handling minor code tweaks, investigating complex merge conflicts, or simply exploring your project’s history, this guide has you covered.

By the end of this article, you’ll have a thorough understanding of the diverse ways in which you can leverage Git to track file changes and take control of your version control workflow. Let’s dive in and unlock the full potential of Git for managing and visualizing file changes in your projects.

Track file changes in git using git status Command

To use the git status command to check file changes in a Git repository, follow these steps:

Open your terminal or command prompt.

Navigate to the directory of your Git repository using the cd command. For example:

cd /path/to/your/repository

Once you’re inside the Git repository, simply run the git status command:

git status

Git will provide a summary of the changes in your repository, including information about untracked files, modified files, and files staged for the next commit.

  • Untracked files will be listed under “Untracked files.” These are files that Git is not currently tracking.
  • Modified files will be listed under “Changes not staged for commit.” These are files that have been modified but haven’t been staged.
  • Files staged for the next commit will be listed under “Changes to be committed.”

Here’s an example of what the output might look like:

On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   example.txt

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

no changes added to commit (use "git add" and/or "git commit -a")

This output provides valuable information about the state of your repository, making it clear which files have been modified, which files are staged, and which files are untracked. It’s a useful first step to understand the status of your project before making further Git actions like committing changes.

Track file changes in git using git diff Command

The git diff command is used to view differences between various Git objects, such as commits, branches, or file versions. Here are several examples of how to use git diff to check file changes in Git, along with their respective outputs.

Checking the Changes Between the Working Directory and the Last Commit

To check changes between working directory and last commit, we use git diff command without options.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Modify the file in the working directory
$ echo "Line 2" >> sample.txt

# Check the differences between the working directory and the last commit
$ git diff

Output:

diff --git a/sample.txt b/sample.txt
index aea71e0..27db5c1 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Line 1
+Line 2

This output shows the differences between the working directory (unstaged changes) and the last commit.

Checking Differences Between Specific Commits

To check change between two commits, you can use command “git diff HEAD~1 HEAD“.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Commit 1"

# Make another commit with a change
$ echo "Line 2" >> sample.txt
$ git add sample.txt
$ git commit -m "Commit 2"

# Check the differences between the last two commits
$ git diff HEAD~1 HEAD

Output:

diff --git a/sample.txt b/sample.txt
index aea71e0..27db5c1 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Line 1
+Line 2

This output shows the differences between the last two commits.

Checking Differences in Staged Changes (Changes Ready for Commit)

You should use command “git diff --staged” to track differences in staged.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Modify the file in the working directory
$ echo "Line 2" >> sample.txt

# Stage the changes
$ git add sample.txt

# Check the differences in staged changes (changes ready for commit)
$ git diff --staged

Output:

diff --git a/sample.txt b/sample.txt
index aea71e0..27db5c1 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Line 1
+Line 2

This output shows the differences in the staged changes, which are the changes ready for the next commit.

Checking Differences Between Two Branches

To check difference between two branches you can use command “git diff <branch_name1> <branch_name2>“.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Create and switch to a new branch
$ git checkout -b feature-branch

# Modify the file in the new branch
$ echo "Line 2" >> sample.txt
$ git add sample.txt
$ git commit -m "Add Line 2 in feature branch"

# Switch back to the main branch
$ git checkout main

# Check the differences between the two branches
$ git diff feature-branch..main

Output:

diff --git a/sample.txt b/sample.txt
index aea71e0..27db5c1 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Line 1
+Line 2

This output displays the differences between the main branch and the feature branch.

Checking Differences with a Specific Commit

To check differences between current state and initial commit, you should use command “git diff HEAD~1“.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Modify the file in the working directory
$ echo "Line 2" >> sample.txt
$ git add sample.txt
$ git commit -m "Add Line 2"

# Check the differences between the current state and the initial commit
$ git diff HEAD~1

Output:

diff --git a/sample.txt b/sample.txt
index aea71e0..27db5c1 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Line 1
+Line 2

This output shows the differences between the current state and the initial commit using HEAD~1.

Checking Differences with Previous Commits

To check differences between last two commits, you should use command “git diff HEAD~2 HEAD~1“.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Modify the file in the working directory
$ echo "Line 2" >> sample.txt
$ git add sample.txt
$ git commit -m "Add Line 2"

# Modify the file again
$ echo "Line 3" >> sample.txt
$ git add sample.txt
$ git commit -m "Add Line 3"

# Check the differences between the last two commits
$ git diff HEAD~2 HEAD~1

Output:

diff --git a/sample.txt b/sample.txt
index aea71e0..27db5c1 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Line 1
+Line 2

This output shows the differences between the last two commits using HEAD~2 and HEAD~1.

Checking Differences in Specific Files

To check differences in specific file use “git diff <file_name>” command.

Command:

# Create a new Git repository and add multiple files
$ git init
$ echo "Line 1" > file1.txt
$ echo "Line A" > file2.txt
$ git add file1.txt file2.txt
$ git commit -m "Initial commit"

# Modify file1.txt in the working directory
$ echo "Line 2" > file1.txt

# Check the differences in file1.txt
$ git diff file1.txt

Output:

diff --git a/file1.txt b/file1.txt
index aea71e0..27db5c1 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1 @@
 Line 1
+Line 2

This output displays the differences in the specific file, file1.txt.

Checking Differences Between Branches for Specific File

You can use command “git diff <branch_name1>..<branch_name2> -- <file_name>“, to check difference for specific file in two branches.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Create and switch to a new branch
$ git checkout -b feature-branch

# Modify the file in the new branch
$ echo "Line 2" >> sample.txt
$ git add sample.txt
$ git commit -m "Add Line 2 in feature branch"

# Switch back to the main branch
$ git checkout main

# Check the differences for the specific file between the two branches
$ git diff main..feature-branch -- sample.txt

Output:

diff --git a/sample.txt b/sample.txt
index aea71e0..27db5c1 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Line 1
+Line 2

This output shows the differences for the specific file, sample.txt, between the main branch and the feature branch.

Checking Differences Between Specific Commits for a File

To check changes in specific file for first and last commit use command “git diff HEAD~2 HEAR -- <file_name>“.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Modify the file in the working directory
$ echo "Line 2" >> sample.txt
$ git add sample.txt
$ git commit -m "Add Line 2"

# Modify the file again
$ echo "Line 3" >> sample.txt
$ git add sample.txt
$ git commit -m "Add Line 3"

# Check the differences for the specific file between the first and last commits
$ git diff HEAD~2 HEAD -- sample.txt

Output:

diff --git a/sample.txt b/sample.txt
index aea71e0..27db5c1 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Line 1
+Line 2

This output shows the differences for the specific file, sample.txt, between the first and last commits.

Checking Differences with Context Lines

You can use “-U3” option with command “git diff” to check difference with context lines.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Modify the file in the working directory
$ echo "Line 2" >> sample.txt
$ git add sample.txt
$ git commit -m "Add Line 2"

# Check the differences with context lines
$ git diff -U3

Output:

diff --git a/sample.txt b/sample.txt
index aea71e0..27db5c1 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Line 1
+Line 2

This output includes context lines to provide additional context around the changes.

Checking Word-Level Differences

You can use “--word-diff” option with command “git diff” to check word-level differences.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "The quick brown fox" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Modify the file in the working directory
$ echo "The fast brown fox" > sample.txt

# Check the word-level differences
$ git diff --word-diff

Output:

diff --git a/sample.txt b/sample.txt
index 3d317a3..693267c 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1 @@
-The \033[1mquick\033[m brown fox
+The \033[1mfast\033[m brown fox

This output highlights word-level differences, making it easier to spot changes within lines.

Checking Differences with Color Highlighting

You can track all changes in git with color coding, for that you can use “--color” option with command “git diff“.

Command:

# Create a new Git repository and add a file
$ git init
$ echo "Line 1" > sample.txt
$ git add sample.txt
$ git commit -m "Initial commit"

# Modify the file in the working directory
$ echo "Line 2" >> sample.txt

# Check the differences with color highlighting
$ git diff --color

Output:

\033[1mdiff --git a/sample.txt b/sample.txt\033[m
\033[1mindex aea71e0..27db5c1 100644\033[m
\033[1m--- a/sample.txt\033[m
\033[1m+++ b/sample.txt\033[m
\033[36m@@ -1 +1,2 @@\033[m
 Line 1
\033[32m+Line 2\033[m

This output includes color highlighting to make the differences more visually distinct.

Track file changes in git using git log Command

The git log command is used to view the commit history in a Git repository. It can help you understand changes made to files and track the project’s evolution over time. Here are several examples of how to use git log to check file changes in Git, along with sample outputs:

Basic git log Usage:

Command:

git log

Output:

commit 3ea6f7d8b33a3e3d92d03f23be9a4d8457a60e60
Author: John Doe <johndoe@email.com>
Date:   Fri Oct 1 13:30:45 2023 +0000

    Added feature X

commit a2b95b81998f572c25eb33e2f95e4e18e7c68cc7
Author: Jane Smith <janesmith@email.com>
Date:   Wed Sep 28 10:15:20 2023 +0000

    Updated README.md

... (more commits)

This basic git log command displays a list of commits with their SHA-1 hashes, authors, dates, and commit messages.

Viewing Commit Details with File Changes:

Command:

git log -p -n 1

Output:

commit 3ea6f7d8b33a3e3d92d03f23be9a4d8457a60e60
Author: John Doe <johndoe@email.com>
Date:   Fri Oct 1 13:30:45 2023 +0000

    Added feature X

diff --git a/path/to/changed_file.txt b/path/to/changed_file.txt
index a6c9cf5..71f4d14 100644
--- a/path/to/changed_file.txt
+++ b/path/to/changed_file.txt
@@ -1,4 +1,5 @@
This is some content in the file.
+New feature X added.
More content in the file.

...

This command displays the most recent commit (-n 1) along with the actual changes made to files (-p option). You can navigate through the changes by pressing the spacebar or “q” to exit.

Viewing a Specific File’s History

Command:

git log -p path/to/your_file.txt

Output:

commit 3ea6f7d8b33a3e3d92d03f23be9a4d8457a60e60
Author: John Doe <johndoe@email.com>
Date:   Fri Oct 1 13:30:45 2023 +0000

    Added feature X

diff --git a/path/to/your_file.txt b/path/to/your_file.txt
index a6c9cf5..71f4d14 100644
--- a/path/to/your_file.txt
+++ b/path/to/your_file.txt
@@ -1,4 +1,5 @@
This is some content in the file.
+New feature X added.
More content in the file.

...

This command displays the commit history and changes specific to the file path/to/your_file.txt.

Track file changes in git using git show Command

The git show command in Git allows you to check file changes in a specific commit. Here are some sample commands and their expected outputs to help you understand how to use git show effectively:

Checking File Changes in a Specific Commit

Suppose you want to check the file changes in commit abcdef1, which is the first commit in your Git repository:

Command:

git show abcdef1

Output:

commit abcdef1
Author: John Doe <johndoe@example.com>
Date:   Fri Jan 1 12:00:00 2023 +0000

    Initial commit

diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+Hello, World!

In this example, you can see the commit information, including the author, date, and commit message. The diff section displays the changes made to the file file.txt.

Checking Detailed File Changes with --stat Option

If you want a more concise summary of the changes, you can use the --stat option:

Command:

git show --stat abcdef1

Output:

commit abcdef1
Author: John Doe <johndoe@example.com>
Date:   Fri Jan 1 12:00:00 2023 +0000

    Initial commit

 file.txt | 1 +
 1 file changed, 1 insertion(+)

This output provides a summary of changes in a more compact format, showing that one file (file.txt) was modified with one insertion.

Viewing Changes in the Most Recent Commit

To check the changes in the most recent commit, you can simply use git show without specifying a commit hash:

Command:

git show

Output:

commit 1234567
Author: Jane Smith <jane@example.com>
Date:   Tue Mar 15 09:00:00 2023 +0000

    Updated README.md

diff --git a/README.md b/README.md
index 123abc4..789def0 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ This is a sample README file for a Git project.

 ### Installation
 - Clone the repository: `git clone https://github.com/your/repo.git`
 - Run `npm install` to install dependencies
+```

This example displays the changes made in the most recent commit, showing the commit information, author, date, commit message, and the file differences.

This example displays the changes made in the most recent commit, showing the commit information, author, date, commit message, and the file differences.

Comparing Two Commits

You can use `git show` to compare two specific commits. For example, to view the changes between commit `abc123` and commit `def456`:

Command:

git show abc123..def456

Output:

commit def456
Author: Alice Johnson <alice@example.com>
Date:   Mon Mar 14 09:00:00 2023 +0000

    Added new feature

diff --git a/main.js b/main.js
index 123abc4..789def0 100644
--- a/main.js
+++ b/main.js
@@ -10,6 +10,10 @@ console.log("Hello, World!");
    console.log("New feature added");
    console.log("Goodbye!");
 }

+commit abc123
+Author: Bob Williams <bob@example.com>
+Date:   Sun Mar 13 09:00:00 2023 +0000
+
+    Initial version

diff --git a/main.js b/main.js
index 123abc4..456def0 100644
--- a/main.js
+++ b/main.js
@@ -1,4 +1,4 @@
 console.log("Hello, World!");

This example shows the changes between two specific commits (abc123 and def456) and displays the commit information and file differences for each of them.

Checking File Changes in a Specific File

You can use git show to check changes for a specific file within a commit. For instance, to view changes in a file named script.js in the commit 123abc:

Command:

git show 123abc script.js

Output:

commit 123abc
Author: Chris Miller <chris@example.com>
Date:   Thu Feb 10 14:00:00 2023 +0000

    Updated script.js

diff --git a/script.js b/script.js
index 5678901..abcdef2 100644
--- a/script.js
+++ b/script.js
@@ -15,10 +15,10 @@ function hello() {
    return "Hello, Git!";
 }

+// New function added
+function newFunction() {
+    return "This is a new feature!";
+}

 // Main code
 console.log(hello());
-console.log("Git is awesome!");
-console.log("New feature added!");
+console.log(newFunction());

This command lets you inspect the changes made to a specific file in a given commit.

Checking File Changes Between Branches

You can use git show to compare file changes between branches. For instance, to check the differences between the main branch and a feature branch:

Command:

git show main..feature script.js

Output:

commit abc123
Author: Alice Johnson <alice@example.com>
Date:   Fri Feb 20 10:00:00 2023 +0000

    Merged 'feature' into 'main'

diff --git a/script.js b/script.js
index 5678901..abcdef2 100644
--- a/script.js
+++ b/script.js
@@ -15,10 +15,10 @@ function hello() {
    return "Hello, Git!";
 }

+// New function added
+function newFunction() {
+    return "This is a new feature!";
+}

 // Main code
-console.log(hello());
-console.log("Git is awesome!");
-console.log(newFunction());
+console.log(hello());

This command lets you compare file changes between two branches, showing what’s different between the main and feature branches specifically for script.js.

Frequently Asked Questions (FAQs)

Q1: What is the difference between git diff and git status?

git diff and git status are both used to view changes in your repository, but they show different aspects. git diff shows the actual changes in file content that have not been staged yet, while git status shows which files have changes that are staged (and will be included in the next commit) and which files have changes that are not yet staged. Know more…

Q2: What does the HEAD in Git refer to?

In Git, HEAD is a reference to the last commit in the currently checked-out branch. When you switch branches with git checkout, the HEAD revision changes to the latest commit on the new branch. Know more…

Q3: How can I see who made a particular change in Git?

You can use git blame to see who made each change in a file, along with the commit where the change was made and the date of the commit. Know more…

Q4: What is a ‘detached HEAD’ state in Git?

A ‘detached HEAD’ state in Git refers to the situation when you’re not on any branch – you’ve checked out a commit (or tag) directly. In this state, any new commits you make won’t belong to any branch and may become difficult to access once you check out a different commit or branch. Know more…

Q5: How can I see the changes between two commits?

You can use git diff <commit1>..<commit2> to see the changes between two commits. This will show you the differences between the contents of the two sets of files. Know more…

Q6: What is Git Diff and why is it important?

Git Diff is a command in Git that allows users to see changes between commits, commit and working tree, etc. It is crucial for tracking changes in the project. Know more…

Q7: How can I track uncommitted changes in Git?

You can track uncommitted changes in Git using the Git Diff command. This command allows you to see what changes have been made since the last commit. Know more…

Q8: What is the use of Git Status command?

Git Status command provides you with all the information about the current state of your repository. It tells you which files have changes that are staged for the next commit. Know more…

Q9: How to deal with conflicts in Git?

Conflicts in Git can be resolved using a variety of strategies, including manual resolution or using a merge tool. Once conflicts are resolved, you can use ‘git add’ to stage the resolved files for commit. Know more…

Q10: What are the best practices for tracking changes in Git?

Some best practices for tracking changes in Git include committing early and often, writing clear commit messages, keeping each commit to a single logical change, and regularly pulling from a central repository to keep your local copy up to date. Know more…

Conclusion

In the world of software development, tracking file changes is a fundamental skill. Git offers an impressive array of tools to make this process efficient and insightful. From git status for quick checks to git diff and git log for detailed analysis, and even graphical tools and web platforms, Git empowers developers to visualize and manage changes effectively.

By mastering these techniques, you’ll enhance your Git workflow, collaborate more efficiently, and ensure the stability and integrity of your projects. Embrace these tools, and take control of your coding journey with confidence. Git’s power is yours to wield.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles