Git - Tracking Changes

Git - Tracking Changes

Tracking Changes in Git

One of Git’s most powerful features is its ability to track changes in files and directories with extreme accuracy and efficiency. Tracking changes is essential for source control, collaboration, debugging, and ensuring a clean project history. This document provides comprehensive coverage on how Git tracks changes, including the staging process, commit history, diffing, viewing blame, working with tags, and managing stashes.

Understanding the Git Workflow

Git works on a three-tiered structure for tracking file changes:

  • Working Directory: Where files are created, edited, and deleted
  • Staging Area (Index): Where changes are prepared for the next commit
  • Repository (HEAD): Where committed changes are permanently recorded

Tracking changes means moving them systematically from working directory to staging area to the repository using Git commands.

Checking Status

Basic Status Command

git status

This command shows which files are:

  • Tracked and modified
  • Untracked
  • Staged

Typical Output

On branch main
Changes not staged for commit:
  modified:   index.html
Untracked files:
  style.css

Tracking a New File

Adding a New File

touch readme.md

Stage the File

git add readme.md

Commit the File

git commit -m "Add initial README"

Untracked vs Tracked Files

# Untracked
touch notes.txt
git status

# Now tracked
git add notes.txt
git commit -m "Add notes file"

Modifying Existing Files

Edit a Tracked File

echo "Hello World" >> index.html

Check Status

git status

This will show the file as modified.

Stage and Commit Changes

git add index.html
git commit -m "Update homepage text"

Using git diff

Show Changes in Working Directory

git diff

Show Changes in Staging Area

git diff --cached

Show Changes Between Commits

git diff HEAD~1 HEAD

Committing with Descriptions

Standard Commit

git commit -m "Fix broken contact form submission"

Multi-line Commit Messages

git commit

# This opens the default editor.
# First line: short summary
# Followed by a blank line
# Then a detailed description

Tracking Rename and Delete

Rename a File

git mv oldname.html newname.html
git commit -m "Rename file for clarity"

Delete a File

git rm unwanted.js
git commit -m "Remove unused script"

Viewing Commit History

List Commits

git log

One-Line Log

git log --oneline

Log with Graph

git log --oneline --graph --all

Log by Author

git log --author="Alice"

Viewing File History

Show Who Changed a File

git log --follow app.js

Show File History with Patch

git log -p index.html

Using git blame

Identify Line-by-Line Changes

git blame index.html

This shows each line of the file with commit hash, author, and date.

Using git show

Show a Specific Commit

git show 5d6f87c

Show Changes for a File in a Commit

git show HEAD:index.html

Working with Tags

Create a Tag

git tag v1.0

Annotated Tag

git tag -a v1.1 -m "Release version 1.1"

View Tags

git tag

Show Tag Details

git show v1.1

Using Stash to Track Temporary Changes

Save Uncommitted Work

git stash

View Stashed Changes

git stash list

Apply a Stash

git stash apply

Apply and Drop a Stash

git stash pop

Clear All Stashes

git stash clear

Undoing Changes

Undo Unstaged Changes

git checkout -- file.txt

Unstage a File

git reset HEAD file.txt

Amend the Last Commit

git commit --amend

Remove Last Commit (Keep Changes)

git reset --soft HEAD~1

Tracking Binary Files

Add an Image or PDF

git add diagram.png
git commit -m "Add architecture diagram"

Git tracks binary files by storing entire snapshots rather than diffs.

File Permissions Tracking

Make a File Executable

chmod +x script.sh
git add script.sh
git commit -m "Make script executable"

Tracking Configuration Files

It is common to track versioned config files but exclude environment-specific values.

Example

config.example.json  # tracked
config.json          # ignored

.gitignore Entry

config.json

Tracking Submodules

Add a Submodule

git submodule add https://github.com/other/repo.git lib/dependency

Initialize Submodules

git submodule update --init

Remove Submodule

git submodule deinit -f lib/dependency
rm -rf .git/modules/lib/dependency
git rm -f lib/dependency

Best Practices for Tracking Changes

  • Commit frequently with clear messages
  • Use branches for features or experiments
  • Track only essential files (ignore logs, builds, secrets)
  • Use git diff before committing
  • Tag releases and important milestones
  • Use git blame to debug history
  • Stash work before switching context
  • Use commit hooks to enforce standards

Tracking changes effectively is the core function of Git and the key to maintaining a clean, reliable codebase. From the initial add and commit process to inspecting logs, diffs, and blame, Git provides a robust toolset for monitoring and managing changes over time. Understanding how to use Git's change-tracking commands will help developers maintain a high-quality history, collaborate efficiently, and resolve issues faster.

As you gain experience with Git, you'll learn how to use advanced tools like interactive rebases, hooks, cherry-picking, and patch management to further refine the change history and ensure consistency across your projects.

Beginner 5 Hours
Git - Tracking Changes

Tracking Changes in Git

One of Git’s most powerful features is its ability to track changes in files and directories with extreme accuracy and efficiency. Tracking changes is essential for source control, collaboration, debugging, and ensuring a clean project history. This document provides comprehensive coverage on how Git tracks changes, including the staging process, commit history, diffing, viewing blame, working with tags, and managing stashes.

Understanding the Git Workflow

Git works on a three-tiered structure for tracking file changes:

  • Working Directory: Where files are created, edited, and deleted
  • Staging Area (Index): Where changes are prepared for the next commit
  • Repository (HEAD): Where committed changes are permanently recorded

Tracking changes means moving them systematically from working directory to staging area to the repository using Git commands.

Checking Status

Basic Status Command

git status

This command shows which files are:

  • Tracked and modified
  • Untracked
  • Staged

Typical Output

On branch main Changes not staged for commit: modified: index.html Untracked files: style.css

Tracking a New File

Adding a New File

touch readme.md

Stage the File

git add readme.md

Commit the File

git commit -m "Add initial README"

Untracked vs Tracked Files

# Untracked touch notes.txt git status # Now tracked git add notes.txt git commit -m "Add notes file"

Modifying Existing Files

Edit a Tracked File

echo "Hello World" >> index.html

Check Status

git status

This will show the file as modified.

Stage and Commit Changes

git add index.html git commit -m "Update homepage text"

Using git diff

Show Changes in Working Directory

git diff

Show Changes in Staging Area

git diff --cached

Show Changes Between Commits

git diff HEAD~1 HEAD

Committing with Descriptions

Standard Commit

git commit -m "Fix broken contact form submission"

Multi-line Commit Messages

git commit # This opens the default editor. # First line: short summary # Followed by a blank line # Then a detailed description

Tracking Rename and Delete

Rename a File

git mv oldname.html newname.html git commit -m "Rename file for clarity"

Delete a File

git rm unwanted.js git commit -m "Remove unused script"

Viewing Commit History

List Commits

git log

One-Line Log

git log --oneline

Log with Graph

git log --oneline --graph --all

Log by Author

git log --author="Alice"

Viewing File History

Show Who Changed a File

git log --follow app.js

Show File History with Patch

git log -p index.html

Using git blame

Identify Line-by-Line Changes

git blame index.html

This shows each line of the file with commit hash, author, and date.

Using git show

Show a Specific Commit

git show 5d6f87c

Show Changes for a File in a Commit

git show HEAD:index.html

Working with Tags

Create a Tag

git tag v1.0

Annotated Tag

git tag -a v1.1 -m "Release version 1.1"

View Tags

git tag

Show Tag Details

git show v1.1

Using Stash to Track Temporary Changes

Save Uncommitted Work

git stash

View Stashed Changes

git stash list

Apply a Stash

git stash apply

Apply and Drop a Stash

git stash pop

Clear All Stashes

git stash clear

Undoing Changes

Undo Unstaged Changes

git checkout -- file.txt

Unstage a File

git reset HEAD file.txt

Amend the Last Commit

git commit --amend

Remove Last Commit (Keep Changes)

git reset --soft HEAD~1

Tracking Binary Files

Add an Image or PDF

git add diagram.png git commit -m "Add architecture diagram"

Git tracks binary files by storing entire snapshots rather than diffs.

File Permissions Tracking

Make a File Executable

chmod +x script.sh git add script.sh git commit -m "Make script executable"

Tracking Configuration Files

It is common to track versioned config files but exclude environment-specific values.

Example

config.example.json # tracked config.json # ignored

.gitignore Entry

config.json

Tracking Submodules

Add a Submodule

git submodule add https://github.com/other/repo.git lib/dependency

Initialize Submodules

git submodule update --init

Remove Submodule

git submodule deinit -f lib/dependency rm -rf .git/modules/lib/dependency git rm -f lib/dependency

Best Practices for Tracking Changes

  • Commit frequently with clear messages
  • Use branches for features or experiments
  • Track only essential files (ignore logs, builds, secrets)
  • Use git diff before committing
  • Tag releases and important milestones
  • Use git blame to debug history
  • Stash work before switching context
  • Use commit hooks to enforce standards

Tracking changes effectively is the core function of Git and the key to maintaining a clean, reliable codebase. From the initial add and commit process to inspecting logs, diffs, and blame, Git provides a robust toolset for monitoring and managing changes over time. Understanding how to use Git's change-tracking commands will help developers maintain a high-quality history, collaborate efficiently, and resolve issues faster.

As you gain experience with Git, you'll learn how to use advanced tools like interactive rebases, hooks, cherry-picking, and patch management to further refine the change history and ensure consistency across your projects.

Related Tutorials

Frequently Asked Questions for GitHub

Teams use GitHub for version control, code sharing, pull requests, and project management.

SSH allows secure communication with GitHub for pushing and pulling code without passwords.

A release marks a specific version of code, often used for deployments or tagging milestones.

Git is a distributed version control system for tracking changes in source code efficiently.

It shows the current state of the repository, including staged, unstaged, and untracked files.


GitHub Pages hosts static websites directly from a GitHub repository.

Git is a tool; GitHub is a platform using Git for remote code collaboration.

Use git revert <commit> to undo changes by creating a new commit.

git commit saves staged changes to the local repository with a message.


Issues track bugs, tasks, or feature requests, allowing discussion and assignment.

Merging combines changes from different branches into one branch, typically main or master.


git push uploads local repository changes to a remote repository like GitHub.

GitHub Actions automates workflows like building, testing, and deploying code with CI/CD pipelines.

.gitignore specifies files and directories Git should ignore and not track.

git init initializes a new Git repository in your local project directory.

git add stages changes in files for the next commit.

A pull request proposes changes from one branch to another, usually for review and merge.

A branch allows parallel development by creating independent code versions from the main project.

GitHub is a cloud-based platform for hosting and managing Git repositories collaboratively.

The default branch name is usually main, previously known as master.

Cloning downloads a copy of a GitHub repository to your local machine.

git pull fetches and merges changes from a remote repository to your local branch.

A commit records a snapshot of file changes with a message and unique ID.

A repository stores project files, folders, and version history for collaborative development.

A fork creates a personal copy of another user's repository for independent development.


A GitHub milestone is a way to track progress on a specific goal or release by grouping related issues and pull requests.

To merge a pull request, review the proposed changes and click "Merge pull request" to integrate them into the base branch.

GitHub labels are tags that help categorize and prioritize issues and pull requests, making it easier to manage and filter them.​

To create a GitHub issue, navigate to the "Issues" tab of your repository and click "New issue."

After making changes in your forked repository, navigate to the original repository and click "New pull request" to propose your changes.

A merge conflict occurs when GitHub cannot automatically merge changes due to conflicting modifications in the same part of a file.​

To use GitHub Actions, create a YAML file in the .github/workflows directory of your repository to define your workflow.

To resolve a merge conflict, manually edit the conflicting files to combine changes, then commit the resolved files.

A branch in GitHub is a parallel version of a repository, allowing you to work on different features or fixes without affecting the main codebase.​

To add a collaborator, go to your repository's settings, select "Collaborators," and enter the GitHub username of the person you want to add.​

A GitHub Gist is a simple way to share code snippets or text, useful for sharing small pieces of code or notes.

A fork creates a personal copy of someone else's repository, allowing you to propose changes. A clone creates a local copy of a repository on your machine.​

To create a GitHub repository, log in to your GitHub account, click the "+" icon in the top right corner, and select "New repository."

To set up GitHub Pages, navigate to your repository's settings, scroll to the "GitHub Pages" section, and select the source branch.

To create a GitHub Gist, log in to your GitHub account, click the "+" icon, and select "New Gist."

A GitHub organization is a shared account where multiple people can collaborate on repositories, issues, and other GitHub features.​

The GitHub CLI is a command-line interface that allows you to interact with GitHub directly from your terminal, enabling operations like creating issues and pull requests.

o use GitHub Copilot, install the extension in a supported IDE, such as Visual Studio Code, and start typing code to receive suggestions.

To create a GitHub organization, click your profile picture in the top right corner, select "Your organizations," and click "New organization."

GitHub Copilot is an AI-powered code completion tool developed by GitHub in collaboration with OpenAI, providing suggestions as you code.​

GitHub is a web-based platform for version control and collaboration, allowing developers to host and review code, manage projects, and build software together.​

To install the GitHub CLI, download the appropriate version for your operating system from the official GitHub CLI website and follow the installation instructions.

line

Copyrights © 2024 letsupdateskills All rights reserved