Patricia Sauer - Authentic Programming

The Git Commit Command

A Git log as a graph

After you made some changes, you want to commit them. By using the commit command, you create a new commit containing the current contents of the index and the given log message describing the changes. The contents to be committed can be specified in several ways.

Using the commit command after incrementally adding changes to the index

You can use the Git add command to incrementally add changes to the index. This simply means that you are using the git add command once or multiple times to add files to the index.

git add file1 file2
git add file3
git add folder4
git commit -m "improved xyz"

Using git rm to remove files from the working tree

Once you added files to the working tree, you might want to remove them from the working tree again. To do so, you can use the git rm command.

git rm file1
git commit -m "improved xyz"

Listing all files as arguments to the commit command

You can list all files to be committed as arguments to the commit command. The resulting commit will ignore changes staged in the index and instead record the current content of the listed files. The files must already be known to Git, i.e. they have to have been added once before. This means, that newly created files cannot be committed by passing them as arguments to the commit command and will result in an error:

git commit file1.txt file2.txt -m "improved xyz"
error: pathspec 'file1.txt' did not match any file(s) known to git
error: pathspec 'file2.txt' did not match any file(s) known to git

Instead, you would need to add them before by using the git add command:

git add file1.txt file2.txt
git commit file1.txt file2.txt -m "improved xyz"

Using the commit command with the -a switch

When using the -a switch with the commit command, Git will

  • add changes from all known files (i.e. all files that are already listed in the index)
  • remove files in the index that have been removed from the working tree

and then perform the actual commit.

Let’s assume that we have file1.txt and file2.txt in the index. Executing the commit command with the -a switch

git commit -a -m "improved xyz"

will result in the following:

2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1.txt
create mode 100644 file2.txt

Reference

For full reference see Git commit documentation.

Liked this article?

Buy Me A Coffee

© 2018 - 2024 Patricia Sauer