Git Commit allows you to create snapshots of different stages of project’s progress and save the changes. This makes it one of the most important commands when working on a project.

Cheap domain names – buy yours now
  • Free website pro­tec­tion with SSL Wildcard included
  • Free private re­gis­tra­tion for greater privacy
  • Free Domain Connect for easy DNS setup

What is Git Commit?

The Git command Git Commit is one of the most important and most used commands in projects. Teams use this command to create a snapshot of the project’s current state once changes have been made, tested it out and approved. Git considers this snapshot to be a secure version, and will only make changes to it if spe­cific­ally asked to do so. Git Commit will also take the snapshot if you are working with a local re­pos­it­ory. This will not affect other re­pos­it­or­ies. These can be accessed through Git Push and Git Pull.

The dif­fer­ence between Git and SVN is worth men­tion­ing here. Git’s de­cent­ral­ised approach allows de­velopers to work on the same project at the same time. Each team member can record their de­vel­op­ment status in the local re­pos­it­ory using Git Commit. You can save changes in the moment, and it also allows you to revert to another version at a later point. This sig­ni­fic­antly increases security and pro­tec­tion against errors. Per­form­ing Git Commits at regular intervals and after major changes is advised.

How does the Git Commit syntax work?

Working in an isolated en­vir­on­ment leads to finding the best solution to a problem without changing the central re­pos­it­ory. You should only apply these changes to the whole project after you’ve thor­oughly tested them. Git Commit preserves all in­di­vidu­al saves for this purpose. This gives you to option to revert to older snapshots. The syntax of Git Commit is basically the same, but you can add arguments to it. Use the following standard command to carry out a Git Commit in the staging area. Use the text editor to insert a message and save the snapshot.

git commit

What are the other Git Commit options?

You can customise the snapshot in advance to optimise your workflow by adding an argument to Git Commit.

How do I save changes in the working directory?

The -a argument creates a snapshot of all changes in the working directory. This takes into account all the files which were added to the local re­pos­it­ory using Git Add in the past. This is what the command looks like:

git commit -a

How do I insert message?

You can use the -m option to avoid going through the text editor. Creating a snapshot with Git Commit prompts you to include an ex­plan­at­ory message. You can do this step directly with -m.

git commit -m "here is your statement".

How do I combine both options?

A com­bin­a­tion of both options is also possible for main users. All changes in the working directory are taken into account and an ex­plan­a­tion is sent directly.

git commit -am "Here's your statement"

How do I modify Git Commits?

Use this option to change your last Git Commit. New changes can be added to the most recent snapshot. You can make the changes using the text editor.

git commit --amend

What is an example of a Git Commit?

We’ll show you what Git Commit looks like in practice. In our example, we’ve been working on a file and want to commit the changes. We have to move it to the staging area using the Git Add command:

git add example.py

We can then use the Git Status command to check if the file has been added to the staging area:

git status
On branch main
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)</file>
new file: example.py

You can carry out Git Commit if the status is correct:

git commit

Your text editor will then open. Although you can formulate the ex­plan­a­tion as you wish, it is common to start with a summary of around 50 char­ac­ters, then insert an empty line and give a detailed ex­plan­a­tion of the in­di­vidu­al changes. This ensures that all team members can quickly un­der­stand your changes. Lines beginning with a hash (#) are ignored in the ex­plan­a­tion. The Git Commit will not be performed if the ex­plan­a­tion is com­pletely empty.

How do I delete Git Commits?

It is possible to delete a Git Commit. However, it is only re­com­men­ded in ab­so­lutely ex­cep­tion­al cases. Deleting a commit can cause major problems, es­pe­cially if you are working in a team. For example, your starting points will no longer be identical if you delete a Git Commit in your local re­pos­it­ory while your team members continue to work with the old version. This can lead to merge errors. You can use the Git Revert command to undo changes without changing your re­pos­it­ory’s history. Use the Git Reset command to per­man­ently delete a Git Commit. You can find this command and many others in our Git cheat sheet with a PDF download.

Tip

Get your project online in just three steps! You can easily perform Git Push, Build and Deploy with Deploy Now by IONOS. Take advantage of the many useful features, fast setup and maximum scalab­il­ity.

Go to Main Menu