24/7/365 Support

Storing file revisions with Git commit

We have initialized a new repository for our project. Now we will learn how to store file modifications using git add and git commit.

Getting ready

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.

How to do it…

Now that we have a new repo initialized for our project, let's go ahead and check in our files.

Before we add any files, simply check the current status of the repo with the 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.

Let's add all the files to the tracking list with 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.

Now check the git status again. This time, it will show newly added files marked by green text and a message saying Changes to be committed:

Next, commit the current state of the files with the 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.

Now if you check the git status again, it should show the nothing to commit message:

$ git status

On branch master

nothing to commit, working directory clean

Next, make some changes in any file and check the repo status again. This time, it should show the modified files as follows:

You can check the exact differences to the previous version and current modifications with the 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

Now you can repeat the add and commit process to store these changes. We have modified an existing file without creating new files. We can use the -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.

How it works…

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 article, 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.

There's more…

Another useful command is git checkout. It can be used to discard any modifications and restore a file to its previous state, or restore the deleted file to its known revision.

Help Category:

What Our Clients Say