← Back to Chapters

Git – New Files

? Git – New Files

? Quick Overview

This topic explains how to create new files in a Git repository, track them, stage them, and finally commit them. Adding new files is a fundamental Git workflow.

? Key Concepts

  • Untracked files
  • Staging area
  • Commit history
  • Removing tracked files

? Syntax / Theory

Git does not automatically track new files. You must explicitly add them to the staging area before committing.

? Creating a New File

? View Code Example
// Create a new empty file
touch newfile.txt

? Checking Repository Status

? View Code Example
// Check untracked and staged files
git status

➕ Adding Files to Staging Area

? View Code Example
// Add a specific file
git add newfile.txt
? View Code Example
// Add all new and modified files
git add .

? Committing New Files

? View Code Example
// Commit staged files with a message
git commit -m "Add newfile.txt"

? Viewing Commit History

? View Code Example
// Display commit history
git log

?️ Removing Files

? View Code Example
// Remove a file before commit
git rm filename.txt
? View Code Example
// Remove file from Git but keep locally
git rm --cached filename.txt

? Use Cases

  • Adding source code files
  • Tracking configuration files
  • Managing documentation

? Tips & Best Practices

  • Always check git status before committing
  • Write meaningful commit messages
  • Avoid committing unnecessary files

? Try It Yourself

  • Create a new file and check its status
  • Add it to staging and commit
  • Verify using git log