Skip to content

Creating a commit

Creating a commit involves two steps:

  1. Identify the changes that should be included in the commit. These changes are then "staged" and ready to be included in the next commit.

  2. Create a new commit that records these staged changes. This should be accompanied by a useful commit message.

We will now show how to perform these steps.

Note

At any time, you can see a summary of the changes in your repository, and which ones are staged to be committed, by running:

git status

This will show you:

  1. The files (if any) that contain changes that have been staged;
  2. The files (if any) that contain changes that have not been staged; and
  3. The files (if any) that are not recorded in the repository history.

Adding a new file

If you've created a new file, you can include this file in the next commit by running:

git add filename

Adding all changes in an existing file

If you've made changes to an existing file, you can include all of these changes in the next commit by running:

git add filename

Adding some changes in an existing file

If you've made changes to an existing file and only want to include some of these changes in the next commit, you can select the changes to include by running:

git add -p filename

This will show you each of the changes in turn, and allow you select which ones to stage.

Tip

This interactive selection mode is very flexible; you can enter ? at any of the prompts to see the range of available actions.

Renaming a file

If you want to rename a file, you can use git mv to rename the file and stage this change for inclusion in the next commit:

git mv filename newname

Removing a file

If you want to remove a file, you can use git rm to remove the file and stage this change for inclusion in the next commit:

git rm filename

Tip

If the file has any uncommitted changes, git will refuse to remove the file. You can override this behaviour by running:

git rm --force filename

Inspecting the staged changes

To verify that you have staged all of the desired changes, you can view the staged changes by running:

git diff --cached

You can view the staged changes for a specific file by running:

git diff --cached filename

Undoing a staged change

You may sometimes stage a change for inclusion in the next commit, but decide later that you don't want to include it in the next commit. You can undo staged changes to a file by running:

git restore --staged filename

Note

This will not modify the contents of the file.

Creating a new commit

Once you have staged all of the changes that you want to include in the commit, create the commit by running:

git commit

This will open your chosen editor and prompt you to write the commit message.

Tip

Note that the commit will not be created until you exit the editor.

If you decide that you don't want to create the commit, you can abort this action by closing your editor without saving a commit message.

Please see Choosing your Git editor for details.

Modifying the most recent commit

After you create a commit, you might decide that there are other changes that should be included in the commit. Git provides a simple way of modifying the most recent commit.

Warning

Do not modify the commit if you have already pushed it to another repository. Instead, record a new commit that includes the desired changes.

Remember that your commit history should not be a highly-edited, polished view of your work, but should instead act as a lab book.

Do not worry about creating "perfect" commits!

To modify the most recent commit, stage the changes that you want to commit (see the sections above) and add them to the most recent commit by running:

git commit --amend

This will open your chosen editor and allow you to modify the commit message.