Adding and Removing Files in Git: A Beginner's Guide
Learn the fundamental Git commands for adding and removing files from your repository. This beginner's guide covers git add, git rm, staging concepts, and best practices for managing files in version control.
Managing files in Git is one of the fundamental skills you'll need as you begin your automation journey. Whether you're working with Python scripts, configuration files, or documentation, understanding how to properly add and remove files from your Git repository is essential for effective version control.
The Git Working Directory vs. Staging Area
Before diving into commands, it's important to understand Git's workflow. When you create or modify files in your project folder (working directory), Git doesn't automatically track them. You need to explicitly tell Git which changes you want to include in your next commit using the staging area.
Think of the staging area as a preparation zone where you gather files before committing them to your repository's history.
Adding Files to Git
The git add command moves files from your working directory to the staging area. Here are the most common ways to use it:
Adding a Single File
To add a specific file, use:
git add filename.pyFor example, if you've created a new Python script for network automation:
git add network_backup.pyAdding Multiple Files
You can add several files at once:
git add file1.py file2.py config.yamlAdding All Files
To add all modified and new files in your current directory:
git add .Or to add all files in the entire repository:
git add -AChecking What's Staged
Before committing, verify what files are staged using:
git statusThis shows you which files are staged (green), which are modified but not staged (red), and which are untracked.
Removing Files from Git
There are two scenarios for removing files: removing them from Git tracking while keeping them on your file system, or removing them completely.
Remove from Git but Keep the File
To stop tracking a file but keep it in your working directory:
git rm --cached filename.pyThis is useful when you accidentally added sensitive files like configuration files with passwords.
Remove File Completely
To remove a file from both Git and your file system:
git rm filename.pyThis stages the removal, so you'll need to commit the change:
git commit -m "Remove outdated script"Unstaging Files
If you've added a file to staging but want to unstage it (without deleting):
git reset filename.pyOr to unstage all files:
git resetPractical Example: Managing Network Automation Files
Let's walk through a common scenario. You're building a network automation project and have created several files:
# Check current status
git status
# Add your main script
git add device_config.py
# Add your requirements file
git add requirements.txt
# Realize you don't want to track a config file with credentials
git add --all
git reset config_with_passwords.yaml
# Check what's staged
git status
# Commit your changes
git commit -m "Add device configuration script and requirements"Best Practices
When managing files in Git, remember these key points:
- Always use
git statusto review your changes before committing - Use
.gitignorefiles to automatically exclude sensitive or temporary files - Add files selectively rather than using
git add .blindly - Use descriptive commit messages when removing files
What's Next
Now that you understand how to add and remove files in Git, the next step is learning how to create meaningful commits and write effective commit messages. This will help you maintain a clean, professional project history that's essential for collaborative automation development.