Basics of Git Commit
Welcome to the world of version control with Git and GitHub! In this tutorial, we will focus on one of the fundamental concepts in Git – the “commit” command. Understanding how to make commits is crucial for tracking changes in your codebase, collaborating with others, and maintaining a well-organized version history.
Table of Contents
Prerequisites
Before we dive into Git commits, ensure that you have Git installed on your machine. You can download it from git-scm.com. Additionally, create a GitHub account if you don’t have one already at github.com.
What is a Git Commit?
In Git, a commit is a snapshot of your project at a specific point in time. It represents a set of changes that you want to include in your project’s history. Each commit has a unique identifier, a commit message, and points to the previous commit, forming a chain of changes.
Step 1: Initializing a Git Repository
Open your terminal or command prompt and navigate to the directory where your project is located. Use the following commands to initialize a new Git repository:
$ cd path/to/your/project
$ git init
This creates a hidden folder called .git
that tracks changes in your project.
Step 2: Making Changes
Now, let’s make some changes to your project. Create a new file, for example, index.html
, and add some content to it.
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<title>My First Git Project</title>
</head>
<body>
<h1>Hello, Git!</h1>
</body>
</html>
Step 3: Staging Changes
Before committing changes, you need to stage them. Staging allows you to selectively include changes in your commit. Use the following command:
$ git add index.html
This tells Git to track changes in the index.html
file.
Step 4: Creating a Commit
Now it’s time to commit your changes. Use the commit command with a descriptive message:
$ git commit -m "Add index.html with initial content"
This creates a commit with your changes and adds the provided message. It’s important to write clear and concise commit messages for better collaboration.
Step 5: Viewing Commit History
To view the commit history, use the following command:
$ git log
This will display a list of commits, each with a unique identifier, author information, date, and commit message.
Sample Output
commit 3a17e90c7f68b9b8e15c68b8445ab8b5a2d4c4c7
Author: Your Name <your.email@example.com>
Date: Tue Jan 25 12:00:00 2024 +0000
Add index.html with initial content
Conclusion
Congratulations! You’ve successfully made your first Git commit. Commits are the building blocks of version control, and understanding how to use them is essential for effective collaboration and code management.