Git Commit - Saving changes in Git repository

Git

In this tutorial we will learn to commit the changes in Git repository.

git commit

We use the git commit command to commit all the files that are staged in the staging area to the Git repository.

When we execute the git commit command, Git will create a checksum for the snapshot that is there in the staging area.

The committed snapshot is saved in the local Git repository and will not change unless we explicitly instruct Git to make some change.

On running this command a text editor will open if you have set an editor when your configured Git.

Example:

$ git config --global core.editor emacs

If you configured emacs as the core editor for Git then, when you run the git commit command then emacs will open. If nothing was set then Git will use the default editor of the system.

The editor will prompt you for a commit message. After you have entered and saved the commit for the snapshot Git will create the commit in the local repository.

git commit -m

If we use the git commit -m "message" command then Git will not open any editor and will use the "message" as the commit message and will create the commit.

So, if we look at the staged changes from the previous tutorial.

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   index.php
	new file:   js/default.js

We can tell that we have staged files and they are ready for commit.

So, to commit the changes we will run the git commit -m command with a commit message "initial commit".

$ git commit -m "initial commit"
[master (root-commit) f066f07] initial commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.php
 create mode 100644 js/default.js

So, from the above output we can tell that 2 files were changed and added to the master branch.

git log

We can check the log of all the commits using the git log command.

So, if we run this command in the git-project directory we will get the commits.

$ git log
commit f066f077af008d20e0457f253d730641f55c6752
Author: Yusuf Shakeel <myemail@example.com>
Date:   Wed Feb 12 20:45:06 2014 +0530

    initial commit

In the next tutorial we will configure Git to ignore files that we don't want to commit.