Basic Git Commands
Once you have a Git repository initialized (either by git init
or git clone
), you'll use a set of core commands to manage your project's history. These commands handle the tracking of file changes and saving snapshots of your project.
The Three States of a File in Git
Understanding Git revolves around three main states that your files can reside in:
- Working Directory: This is your local checkout of the project files. You make modifications to files here.
- Staging Area (Index): This is a file, generally contained in your Git directory, that stores information about what will go into your next commit. You use the staging area to selectively choose which changes in your working directory you want to commit.
- Git Directory (Repository): This is where Git stores the metadata and object database for your project. When you commit, Git takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.
1. `git status` - Check the Status
The git status
command is your go-to command to see the current state of your working directory and staging area. It tells you which files are modified, which are staged, and which are untracked.
git status
This command is safe to run at any time and will provide helpful information about what Git is aware of.
2. `git add` - Stage Changes
To start tracking new files or to stage modifications to existing files for your next commit, you use the git add
command. This moves changes from the working directory to the staging area.
- To stage a specific file:
git add filename.txt
- To stage all changes in the current directory and subdirectories:
git add .
- To stage all changes to tracked files (modified and deleted) in the entire working tree:
git add -u
orgit add --update
- To stage all changes (new, modified, deleted) in the entire working tree:
git add -A
orgit add --all
# Example: stage a single file
git add README.md
# Example: stage all modified and new files in the current directory
git add .
3. `git commit` - Save Your Changes
Once you have staged the changes you want to save, you use git commit
to permanently store that snapshot in your repository. Each commit requires a message describing the changes.
git commit -m "Your descriptive commit message"
If you run git commit
without the -m
option, Git will open your configured text editor for you to write a more detailed commit message. Good commit messages are crucial for understanding the project's history. This practice is vital, similar to how detailed documentation is important in fields like Understanding Blockchain Technology.
4. `git log` - View Commit History
The git log
command allows you to see the history of commits in your repository. It shows the commit hash, author, date, and commit message for each commit, starting with the most recent.
git log
There are many options to customize the output of git log
, for example:
git log --oneline
: Shows a condensed, one-line summary of each commit.git log --graph
: Displays an ASCII art graph of the branch and merge history.git log --stat
: Shows basic stats about the changes in each commit (files changed, lines added/deleted).git log -p
: Shows the patch (the actual changes introduced) for each commit.
5. `git diff` - View Differences
The git diff
command is used to see changes between commits, branches, or your working directory and the staging area.
git diff
: Shows unstaged changes (changes in the working directory not yet added to the staging area).git diff --staged
(orgit diff --cached
): Shows staged changes (changes in the staging area that will go into the next commit).git diff HEAD
: Shows all changes in your working directory, both staged and unstaged, compared to the last commit (HEAD).git diff <commit-hash1> <commit-hash2>
: Shows the differences between two specific commits.
6. `.gitignore` - Ignoring Files
Often, you have files or directories in your project that you do not want Git to track. These might include build artifacts, log files, or editor-specific configuration files. You can tell Git to ignore these files by creating a file named .gitignore
in your project's root directory.
Each line in .gitignore
specifies a pattern. For example:
# Ignore compiled Python files
*.pyc
# Ignore a build directory
/build/
# Ignore log files
*.log
# Ignore IDE specific files
.idea/
.vscode/
These are some of the most fundamental Git commands you'll use daily. Mastering them is key to effectively using Git. This understanding is crucial in various tech fields, including when you are Exploring WebAssembly for high-performance web applications.
Next: Branching and Merging in Git ➡️