Git Commit
A Git commit records the current state of the repository, allowing for easy tracking and managing of changes in the project. Commits make it possible to switch between different versions of a project and inspect how it has evolved over time.
Staging Changes with Git
Staging changes in Git means preparing modified files for a commit. This process allows you to organize your changes into meaningful chunks before committing them. In this tutorial, we'll guide you through the process of staging changes with Git.
Step 1: Open a Terminal
First, open a terminal on your system. If you're on Windows, you can use Git Bash which was installed with Git.
Step 2: Navigate to Your Git Repository
Use the cd
(change directory) command to navigate to your Git repository. For example, if your repository is located in a folder named "my_project" on your desktop, you would use the following command:
cd Desktop/my_project
Step 3: Check the Status of Your Repository
Before staging changes, it's a good idea to check the status of your repository. The git status
command will show you which files have been modified and are ready to be staged.
git status
Step 4: Stage Your Changes
To stage changes, you use the git add
command followed by the file name. For example, if you have modified a file named "file.txt", you would stage the changes with the following command:
git add file.txt
If you want to stage all changes in the repository, you can use the .
(dot) instead of the file name:
git add .
Committing Changes with Git
Committing changes in Git means saving the staged modifications to the repository with a meaningful message describing what was done. This creates a checkpoint in your project that you can revert back to if needed. In this tutorial, we'll guide you through the process of committing changes with Git.
Step 1: Open a Terminal
First, open a terminal on your system. If you're on Windows, you can use Git Bash which was installed with Git.
Step 2: Navigate to Your Git Repository
Use the cd
(change directory) command to navigate to your Git repository. For example, if your repository is located in a folder named "my_project" on your desktop, you would use the following command:
cd Desktop/my_project
Step 3: Check the Status of Your Repository
Before committing changes, it's a good idea to check the status of your repository. The git status
command will show you which files have been staged and are ready to be committed.
git status
Step 4: Commit Your Changes
To commit changes, you use the git commit
command followed by -m
and your commit message in quotes. For example:
git commit -m "Add new feature"
This command will create a new commit with the message "Add new feature". This message should briefly describe what changes were made.
Viewing Commit History with Git
Viewing commit history in Git allows you to understand the evolution of your project, see what changes were made, and by whom. In this tutorial, we'll guide you through the process of viewing commit history with Git.
Step 1: Open a Terminal
First, open a terminal on your system. If you're on Windows, you can use Git Bash which was installed with Git.
Step 2: Navigate to Your Git Repository
Use the cd
(change directory) command to navigate to your Git repository. For example, if your repository is located in a folder named "my_project" on your desktop, you would use the following command:
cd Desktop/my_project
Step 3: View the Commit History
To view the commit history, you use the git log
command. This command will display a list of all commits made to the repository, along with the commit hash, author, date, and commit message.
git log
The output will look something like this:
commit a1b2c3d4e5f6g7h8i9j0
Author: John Doe <john@doe.com>
Date: Tue May 16 13:00:00 2023 -0400
Add new feature
Each commit is displayed with its full SHA-1 checksum, the author’s name and email, the date it was committed, and the commit message.
Conclusion
Congratulations! You've successfully staged changes, committed changes and viewed the commit history with Git. In the next parts of this tutorial, you'll learn more about managing branches, merging changes, and more.