Getting Started with Git
In Git, there are two core and essential concepts every programmer must know. These two concepts are staging and committing. These two concepts ensure that your changes are properly tracked and saved in a safe way in the repository’s history. Staging files allow you to select the changes you want to commit, while committing actually saves those changes with a message that has the description of what was done. In this lesson, we will learn about using Git to prepare and commit your changes to your repository, marking important milestones in the development of your project.
The Meaning of Staging
Staging means the process of preparing changes (modified, manipulated, deleted, or newly created files) to be committed. When you stage a file, you decide which changes should be part of the next commit. This step allows you to break up your work into logical, smaller pieces. For instance, you can stage changes in a file selectively if you have been working on several features but only want to commit one of them (make only one feature work).
Staging Files with git add
To stage a file, use the git add
command followed by the filename or directory you want to stage.
Staging a Single File
If you’ve edited an index.html
file and want to stage it for commit, use this widely used command:
git add <file-name>
git add index.html
Staging Whole Changes
To stage all changes in your project (new, modified, and deleted files) in one command, use this easy command:
git add
This stages all files in the current directory (folder) and its subdirectories.
Checking Staged Files
After staging files, you can use the get status
command:
git status
If you've staged the index.html file, it will appear under "Changes to be committed"
:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: index.html
The file has now been staged and is prepared for commit.
Meaning of Committing
Committing means the process of saving the staged changes to the repository’s version history. Each commit includes a commit message that describes what changes were made, providing context to anyone viewing the history. Committing serves as a snapshot (like recording a video to detect project changes) of your project at a given point, and every commit is recorded with a unique identifier (hash) that allows you to trace back to previous versions of your work.
Commiting New Changes with git commit
Once we have staged our changes, you’re ready to commit them. To commit the staged files, use the git commit
command, followed by a message that describes the changes you’ve made.
Committing Changes with a Message
Follow the following syntax to ensure that you commit changes in a true way:
git commit -m "Your commit message"
As an example:
git commit -m "Add contact section to the homepage"
This command creates a commit with a brief description of the changes made (a message that comes after the -m
option). The commit message should be clear and concise, explaining what was changed (updated) or added in the commit.
Viewing The Commit History
After you have committed your new changes (updates), you can view your commit history (commit list) using the git log command:
git log
This essential command shows you the commit history, including the commit ID (a long alphanumeric string), author, date, and commit message. For example, you will see something like that:
commit 1a2b3c4d5e6f7g8h9i0jklmnopqrst
Author: Your Name <youremail@example.com>
Date: Mon Nov 28 15:30:12 2024 -0500
Add a new section to the homepage
You can scroll through the log using the arrow keys or press q to quit.
Undoing Staged Changes
If you by chance stage a file or want to unstage it before committing, to do that you can use the following command:
git restore --staged <file>
For example, if you want to unstage the index.html
file:
git restore --staged index.html
When you run the git status
command after this command, it will show the file as modified but not staged.
Conclusion
To summarise this lesson, you learned how to stage and commit changes in Git, which are the key functions that allow you to manage your project’s version history. Staging allows you to prepare specific changes for a commit, while committing saves those changes with a message for future reference (Claneder with the saved updates). With these essential and basic Git skills, you’re ready to continue managing your projects with confidence. In our next lesson, we’ll explore more about viewing your commit history and working with Git logs to have a good base to use more advanced commands.