Understanding Git and Version Control

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:

Diagram illustrating Git's three states: Working Directory, Staging Area, and Repository.

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.

# 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.

Conceptual image of files being committed to a Git repository.

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:

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.

Abstract visualization of differences between file versions highlighted by git diff.

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 ➡️