Basic Git Commands & The Commit Lifecycle
With Git successfully installed on your machine, you're now ready to start using it! While Git is incredibly powerful and has hundreds of commands, you'll find that you use a small handful of them for about 90% of your daily work.
In this lesson, we're going to focus on this essential set of commands. We'll learn how to start tracking a project, how to check the status of your files, and most importantly, how to save your work by creating "commits".
This core workflow, often called the "commit lifecycle", is the foundation upon which all other Git operations are built. Let's get hands-on! ⌨️
Initializing Your Repository – git init
The very first step for any project that you want to put under version control is to initialize a Git repository. You only need to do this once per project.
Navigate to your project's root folder using your terminal or command prompt, and then run the following command:
git init
That's it! This single command creates a new, hidden sub-directory named .git. This .git folder is your local repository – it's where Git stores all the metadata and the entire history of your project. All the magic of Git happens inside this folder.
Once you see a message like "Initialized empty Git repository in .../.git/", Git is now officially tracking that folder and its contents.
The Three States – Git's Mental Model
Before we use more commands, it's crucial to understand Git's mental model for your files. Every file in your working directory can be in one of two states: tracked or untracked. A tracked file is one that Git knows about. A tracked file can then exist in one of three main states:
- Modified: You have changed the file, but you haven't yet saved this change to your repository's history.
- Staged: You have marked a modified file in its current version to be included in your next commit (your next snapshot).
- Committed: The data is safely stored as a snapshot in your local repository (the
.gitdirectory).
This introduces the concept of the Staging Area (or "index"). The staging area is an intermediate step between modifying a file and committing it. It allows you to carefully select which changes you want to include in your next commit.
The basic workflow looks like this:
Working Directory → (You modify files) → Modified Files → (You run git add) → Staging Area → (You run git commit) → Repository
The Shopping Cart Analogy
Think of the staging area like an online shopping cart. Your working directory is the entire store. You can browse and make changes to files (putting items in your physical cart). When you're ready, you use git add to put specific changes into your staging area (your online shopping cart). You can review the cart and make sure it's exactly what you want. Finally, you run git commit to "check out" and finalize the transaction, creating a permanent record (a receipt/commit) in your repository's history.
Checking Your Status – git status
The git status command is one of the most helpful tools for understanding what's going on in your repository. It gives you a snapshot of:
- Which files have been modified since your last commit.
- Which files are currently staged and ready to be committed.
- Which files in your working directory are new and not yet being tracked by Git.
It also provides clear guidance on next steps, such as staging changes or discarding modifications.
If you're using a visual Git client like Sourcetree, GitHub Desktop, or GitKraken, this information is often shown in real-time within the interface – making it even easier to understand your working directory and staging area without typing commands.
Whether you prefer the command line or a GUI, being aware of your current Git status helps ensure your commits are intentional and well-structured.
Staging Changes – git add
Once you've modified some files and you're happy with the changes, you need to move those changes to the staging area. This is done with the git add command. This command tells Git, "I want to include the current version of this file in my next commit."
Staging a Specific File
To stage a single file, you provide its name:
git add MyTestFile.cs
Staging All Changes
A very common shortcut is to stage all new and modified files in your current directory and all subdirectories. You can do this with a period (.):
# The '.' represents the current directory
git add .
After running git add, if you run git status again, you'll see that your files have moved from the "Changes not staged for commit" section to the "Changes to be committed" section.
Saving Your Work – git commit
Once you have staged all the changes you want to include in your next historical snapshot, you save them to your repository using the git commit command.
A commit takes everything in the staging area, creates a permanent snapshot of that state in your Git history, and gives it a unique hash ID. Every commit must have a message associated with it to describe the changes.
The most common way to commit is with the -m flag, which allows you to provide a commit message directly on the command line.
git commit -m "Feat: Add initial calculator class and its unit tests"
Writing Great Commit Messages
A commit message is a permanent part of your project's history. Make it count! A good commit message helps you and your teammates understand why a change was made weeks or years later. A common convention is:
- Start with a short, imperative summary (e.g., "Fix: Correct login validation bug").
- Optionally, add a blank line followed by a more detailed description of the "what" and "why" of the change.
Clear messages are invaluable for debugging and understanding the project's evolution.
Viewing Your History – git log
Now that you've made some commits, how do you see the history you've created? The git log command is your window into the past.
Running git log by itself will show you a detailed list of all the commits in your repository's history, starting with the most recent. Each entry will typically show:
- The full commit hash (the unique SHA-1 ID).
- The author's name and email.
- The date the commit was made.
- The full commit message.
The output can be quite verbose. A very useful flag for a more compact view is --oneline:
git log --oneline
Example Output:
a1b2c3d (HEAD -> main) Fix: Add error handling for division by zero
e4f5g6h Feat: Implement subtraction method
c7d8e9f Feat: Add initial calculator class and its unit tests
This gives you a quick, one-line summary of each commit, showing the short version of the hash and the commit message title.
Key Takeaways
- The core Git workflow involves moving files from a modified state to a staged state, and finally to a committed state.
- Start tracking a project with
git init. - Use
git statusconstantly to check the state of your files and staging area. - Use
git addto move changes from your working directory to the staging area. - Use
git commit -m "message"to create a permanent snapshot of your staged changes in the repository history. - Use
git logto review the history of commits you have made. - Visual Git clients like Sourcetree or GitHub Desktop simplify staging, committing, and viewing history by offering intuitive UI – making it easier to understand Git operations without using command-line commands.
Mastering the Basics
- Pro Git Book: Recording Changes to the Repository The official book's chapter on the core workflow.
- A Visual Git Reference A great, visual guide for the most common commands in Git.
- VS Code Docs: Introduction to Git in VS Code Learn how to set up Git in Visual Studio Code.
- Microsoft Learn: Make a Git commit in Visual Studio Explore Git functionality in Visual Studio IDE.