Skip to content

Latest commit

 

History

History
160 lines (119 loc) · 3.65 KB

2. Tracking Changes.md

File metadata and controls

160 lines (119 loc) · 3.65 KB

Tracking Changes

Tracking changes in Git is essential to manage your files and monitor modifications. This page explains how Git tracks changes and how to use commands to manage your project’s history effectively.


Understanding Git's Tracking Process

When working with a Git repository:

  1. Untracked Files: Files that Git does not yet know about.
  2. Tracked Files: Files that Git is monitoring for changes. Tracked files can be:
    • Unmodified: No changes since the last commit.
    • Modified: Changes have been made but not yet staged.
    • Staged: Changes are prepared for the next commit.

1. Viewing Changes

Check the Status of Your Files

Use git status to see the current state of your files:

git status

Check Differences in Modified Files

To view changes in files compared to the last commit:

git diff

Check Staged Differences

To view changes that are staged for the next commit:

git diff --cached

2. Tracking New Files

To track new files, use the git add command:

git add filename

Track All New Files

To stage all new and modified files:

git add .

Add Files by Pattern

You can add specific file types:

git add *.txt

3. Updating Tracked Files

If a tracked file is modified, stage the changes:

git add filename

4. Ignoring Files

Use a .gitignore file to prevent certain files or directories from being tracked:

  1. Create a .gitignore file in your repository:
    touch .gitignore
  2. Add patterns for files to ignore. For example:
    # Ignore log files
    *.log
    
    # Ignore temporary files
    *.tmp
    
  3. Save the file and commit it:
    git add .gitignore
    git commit -m "Add .gitignore file"

5. Untracking Files

If you no longer want Git to track a file:

  1. Remove it from tracking without deleting it:
    git rm --cached filename
  2. Commit the change:
    git commit -m "Stop tracking filename"

6. Commit Changes

Once files are staged, commit them to save a snapshot of your changes:

git commit -m "Describe your changes"

Common Scenarios

Scenario Command
View the current status git status
View changes in the working directory git diff
Stage a single file git add filename
Stage all changes git add .
Commit staged changes git commit -m "message"
Stop tracking a file git rm --cached filename
Ignore files using .gitignore Add patterns to .gitignore and commit it

Example Workflow

  1. Modify a file:

    echo "Hello, Git!" > hello.txt
  2. Check the status:

    git status
  3. Stage the file:

    git add hello.txt
  4. Commit the changes:

    git commit -m "Add hello.txt with a greeting"

Conclusion

Tracking changes is a fundamental part of working with Git. By understanding how Git handles file states and using commands like git status, git add, and git commit, you can efficiently manage your project and maintain a clean version history.


Next Steps: Staging and Committing