Overview
In this blog post, we'll cover essential concepts related to Git, focusing on the git add and git commit commands. Before delving into the commands, let's address an important practice: avoiding the creation of a repository inside another repository.
Why One Should Not Create a Repo Inside a Repo
When initializing a Git repository in a directory, such as ProjectOne, Git monitors not only that directory but also everything nested within it (child or grandchild directories). Creating a repository inside an existing one leads to confusion, as it's akin to Git tracking Git or a workspace inside a workspace. To prevent this, always check the repository status using git status
and delete the corresponding .git
folder if unintentionally initialized inside an existing repository.
GIT Workflow
Make Changes: Create, edit, or delete files.
Stage/Add Changes: Group specific changes using the
git add
command in preparation for committing.Commit Changes: Use the
git commit
command to commit the staged changes. This is a multi-step process, allowing selective commits and creating a checkpoint in the repository's history.
What is a Commit?
A commit is a checkpoint in time, essentially a snapshot of changes in the repository. It enables the ability to revert to earlier commits, merge commits, and undo changes.
Staging Changes with git add
The first step towards committing changes is adding them to the staging area using the git add
command. The staging area is compared to a pet store - once you bring a pet home, you're committed.
Command: git add <file name>
or git add .
(to stage all changes at once).
git commit
The git commit
command is used to commit changes from the staging area. When making a commit, a commit message summarizing the changes and work snapshot is required. The command can be run with or without the -m
flag, allowing for an inline commit message.
Command: git commit
or git commit -m "my message"
git log
The git log
command retrieves information about the commit log, including author, date/time of the commit, and the commit message.
Congratulations on understanding how to add and commit changes in a Git repository! Feel free to share this blog with others who may benefit from it. Questions and feedback are welcome in the comments section.๐๐