Now that you've got Git installed and have initialized a new repository, it's time to make your first commit! Think of a commit as a snapshot of your project at a specific point in time. It's like saving your work with a descriptive note, so you can always go back to it or understand what changes were made and why.
The process involves a few key steps: adding files to be tracked, staging those changes, and then committing them. Let's break it down.
Step 1: Create or Modify a File First, let's create a new file in your project directory or make some changes to an existing one. For this example, let's create a simple README file to describe our project. Navigate to your project's root directory in your terminal and create a file named README.md. You can use your preferred text editor to add some content.
# My Awesome Project
This is a new project where I'm learning Git!Step 2: Check the Status
Before we commit, it's a good idea to see what Git knows about your project's files. The git status command will show you which files have been modified, which are untracked, and which are staged and ready to be committed.
git statusAfter creating your README.md file, running git status will likely show it as an 'untracked file'. This means Git sees the file exists but isn't actively tracking its changes yet.
Step 3: Stage Your Changes
To tell Git that you want to include this file in your next commit, you need to 'stage' it. This is done using the git add command. You can add specific files or all modified/untracked files.
git add README.mdNow, if you run git status again, you'll see that README.md is listed under 'Changes to be committed', usually in green. This indicates it's staged and ready for its snapshot.
graph TD;
A[Working Directory] -->|git add file.txt| B(Staging Area);
B -->|git commit -m "Message"| C(Local Repository);
Step 4: Make Your Commit
With your changes staged, you can now create your commit. This is done using the git commit command. It's crucial to provide a clear and concise commit message that explains what you've done. The -m flag is used to specify the message directly on the command line.
git commit -m "Initial commit: Add README.md file"Git will then create the commit, associating it with your name and email (which you set up during installation), and the message you provided. You'll see output confirming the commit was made, including a unique commit hash (a long string of characters).
Congratulations! You've just made your first commit. Your project is now saved at this specific point in time. If you were to make more changes and then run git status, you'd see that the README.md file is no longer listed as modified, and the output would indicate that your branch is up to date with the commit you just made.