Git Commit Explained

The git commit command is one of the most important in Git because it is used to record changes in the local repository. A commit saves a snapshot of the current work and adds it to the repository's history. Each commit is assigned a unique ID so that the history of a project can be tracked.

Bernd Fischer

Bernd Fischer

12. Oct 2024 · 3 min read

Git Commit Explained

1. What is a Git Commit?

A commit is a recording of a specific state of a Git repository. It records all changes made in the working copy that have been added to the staging area. Each commit creates a snapshot of the current state of the project, and these commits together form the project history.

Commit process:

  1. Make changes: Edit files in the project or add new files.
  2. Add changes to the staging area: Add the changed or newly added files with git add to the staging area.
  3. Create commit: Use git commit to save the changes permanently.

2. Syntax of git commit

The basic command for a commit looks as follows:

git commit -m "Commit-Botschaft"
  • -m: This option allows you to pass a commit message that describes which changes were made.
  • "Commit message": A short description of the changes performed.

Example: Creating a simple commit

git add <Datei>
git commit -m "Fügt neue Datei hinzu"

In this example, a file is added to the staging area and then committed with a description of the change made.

3. The Staging Area

Before creating a commit, changes must be added to the staging area (also called "index"). Using the command git add, files or changes can be prepared that will be included in the next commit.

Example: Adding changes to the staging area

git add datei1.txt
git add datei2.txt

Here, two files are added to the staging area. These files will be saved in the next commit.

Example: Adding all changes to the staging area

git add .

This command adds all changes (new files, changes in existing files, and deleted files) to the staging area.

4. Commit Message

The commit message describes the changes contained in the commit. A good commit message should be short, concise, and meaningful, making it easier to follow the history of changes.

Example: A meaningful commit message

git commit -m "Fix: Korrigiert Berechnungsfehler in der Summe-Funktion"

In this example, the commit message describes what was changed in the commit and why (in this case, a calculation error was fixed).

Extended commit message

If a more detailed description of the changes is needed, the command can be used without the -m option. This opens the default editor where a longer message can be entered.

git commit

On the first line, a short summary can be entered, followed by a detailed description in the subsequent lines.

5. Changing a Commit: git commit --amend

If something still needs to be added or a commit message needs correction after a commit, the command git commit --amend can be used. This modifies the last commit without creating a new commit.

Example: Changing a commit message

git commit --amend -m "Neue Commit-Nachricht"

This command overwrites the commit message of the last commit with a new message.

Example: Adding changes to the last commit

git add weitere_datei.txt
git commit --amend

In this example, a file that was forgotten in the previous commit is added. Instead of creating a new commit, the previous commit is edited to include the new file.

6. Git Commit without the Staging Area: git commit -a

Using the option -a allows all changed files (already existing in the repository) to be committed directly without adding them to the staging area first using git add. However, this option only affects already tracked files (files Git already knows).

Example: Commit without git add

git commit -a -m "Commit für alle geänderten Dateien"

In this example, all changed files are directly added to the commit and then committed.

7. Saving multiple files with one commit message

It is possible to save multiple changes at once in a single commit. This is often done when all changes belong together and should be recorded in one step.

Example: Saving multiple files in one commit

git add datei1.txt
git add datei2.txt
git commit -m "Fügt Datei 1 und Datei 2 hinzu"

8. Check commit history

The commit history can be checked with the command git log. This allows viewing all previous commits with their messages and IDs.

Example: Displaying commit history

git log

This command shows a list of all past commits, including the commit hash, author, date, and commit message.

Example: Displaying a short commit history

git log --oneline

This command shows a shorter overview of the commit history, displaying only the first part of the commit hash and the commit message.

9. Resetting a commit: git reset

If a commit needs to be undone, the command git reset can be used. This allows returning to a specific commit while keeping or discarding changes in the working directory.

Example: Returning to a previous commit

git reset --soft HEAD~1

This command resets the repository to the state of the previous commit but keeps changes in the staging area. Alternatively, with --hard, the working directory can be fully reset to the state of the previous commit:

git reset --hard HEAD~1