We have initialized a new repository for our project. Now we will learn how to store file modifications using git add
and git commit
.
Make sure you have initialized a new git repository and created sample files under your project directory. Follow the previous recipes to get more details.
Now that we have a new repo initialized for our project, let's go ahead and check in our files.
git status
command. This should list all the files under the Untracked files
list, as follows:$ git status
As shown by git status
, none of our files are being tracked by Git
. We need to add those files before Git tracks any changes to them.
git add
:$ git add .
This command does not create any output, but stages all untracked files to be added to the repo. The symbol (.
) specifies the current directory and processes all files under the current directory. You can also specify file name(s) to add specific files.
Changes to be committed
:git commit
command. Commit means asking Git to save the current state of staged files:$ git commit -m "First commit"
The git commit
command will display details of updates to the repository, along with the commit ID (4459fcc
). In this case, we have added three new files without any new insertion or deletion of contents.
git status
again, it should show the nothing to commit
message:$ git status On branch master nothing to commit, working directory clean
git diff
command. Use git diff
without any file name to get all modifications in all files, or use it with a file name to check specific files:$ git diff
-a
flag with git commit to stage changes and commit them in a single command, as follows:$ git commit -a -m "index.html updated"
The -a
flag will stage all modified files and commit will proceed with newly staged contents. Note that this only works with modified files. If you have created any new file, you need to use git add to stage them.
This recipe uses two primary commands: git add
and git commit
. The first one stages the content for the next commit, and the second actually stores the current state of the content. The git add command is used to add new files, stage updates to existing files, and remove any entries of deleted files. All these modifications to the current working tree are staged for the next commit. The command can be used multiple times to stage multiple modifications. Additionally, you can stage all files under the current directory at once by adding a single file, naming it explicitly, or even choosing a single line from a bunch of updates in the single file.
Once the modifications are staged, you can use git commit
to store the updates. When the changes are committed, Git stores the updates in the revision history and changes Git Head to point to the latest revision. All updated files are stored in the form of a binary large object (blob) as a new snapshot. The commit process also triggers some hooks or events that can be used to execute external scripts to carry out some additional functions. Later in this chapter, we will discuss Git hooks in more detail.
Other than git add and git commit, we have used git status
and git diff
commands. As the name suggests, git status
shows the current status of the repository in question. It lists all files that have been modified after the last commit, newly created or deleted files, and any updates that have already been staged. The git diff
command can be used to list all modifications to a given file. It compares the current state of a file against its last committed or indexed state. Note that you can use git diff
before indexing any file with git add.