DEV Community

Cover image for Git commit
professional writer
professional writer

Posted on

Git commit

The project's staged changes are captured in a snapshot by the git commit command. You can think of committed snapshots as "safe" versions of a project because Git won't alter them unless you specifically instruct it to. The git add command is used to'stage' or promote changes to the project that will be saved in a commit before the git commit command is executed. The two most frequently used commands are git commit and git add.

read also : What does git commit Amend do?

Git commit vs SVN commit

Despite having the same name, git commit differs significantly from svn commit. For Git newcomers with an svn background, this shared term may cause confusion, so it's crucial to highlight the distinction. A centralised application model (svn) and a distributed application model are being compared when comparing git commit and svn commit (Git). A commit in SVN pushes modifications from the local client to a remote, centralised shared repository. Git distributes its repositories, commits Snapshots to the local repository, and does all of this without ever interacting with other Git repositories. Git commits can be pushed to any remote repository at a later time.

How it works

Git can be seen as a timeline management tool at a high level. The fundamental units of a Git project timeline are commits. Commits can be viewed as snapshots or checkpoints along a Git project's timeline. The git commit command creates commits to record the state of a project at that particular moment. The local repository is always updated with Git Snapshots.

read also : vs code git blame inline

In contrast to SVN, where the working copy is committed to the main repository, this is fundamentally different. Git, on the other hand, waits until you're prepared to interact with the main repository. Each developer's local repository acts as a buffer between the working directory and the project history, much like the staging area does.

Snapshots, not differences

SVN and Git differ practically from one another, but their underlying implementations also adhere to wholly different design philosophies. Git uses snapshots as its basis for version control, whereas SVN tracks file differences. For instance, an SVN commit includes a diff to the original file that was added to the repository. On the other hand, every commit in Git includes a complete record of the contents of every file.

This makes many Git operations much faster than SVN operations because each file's complete revision is immediately available from Git's internal database, rather than having to be "assembled" from its diffs.
Git's snapshot model significantly influences practically every aspect of its version control model, including branching and merging.

Common options

git commit
Enter fullscreen mode Exit fullscreen mode

Commit the staged snapshot. This will launch a text editor prompting you for a commit message. After you’ve entered a message, save the file and close the editor to create the actual commit.

git commit -a
Enter fullscreen mode Exit fullscreen mode

Commit a snapshot of all changes in the working directory. This only includes modifications to tracked files (those that have been added with git add at some point in their history).

git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

A shortcut command that immediately creates a commit with a passed commit message. By default, git commit will open up the locally configured text editor, and prompt for a commit message to be entered. Passing the -m option will forgo the text editor prompt in-favor of an inline message.

git commit -am "commit message"
Enter fullscreen mode Exit fullscreen mode

A power user shortcut command that combines the -a and -m options. This combination immediately creates a commit of all the staged changes and takes an inline commit message.

git commit --amend
Enter fullscreen mode Exit fullscreen mode

This option adds another level of functionality to the commit command. Passing this option will modify the last commit. Instead of creating a new commit, staged changes will be added to the previous commit. This command will open up the system's configured text editor and prompt to change the previously specified commit message.

Examples

Saving changes with a commit
The following example assumes you’ve edited some content in a file called hello.py on the current branch, and are ready to commit it to the project history. First, you need to stage the file with git add, then you can commit the staged snapshot.

git add hello.py
Enter fullscreen mode Exit fullscreen mode

This command will add hello.py to the Git staging area. We can examine the result of this action by using the git status command.

git status
On branch main
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
   new file: hello.py
Enter fullscreen mode Exit fullscreen mode

The green output new file: hello.py indicates that hello.py will be saved with the next commit. From the commit is created by executing:

git commit
Enter fullscreen mode Exit fullscreen mode

This will open a text editor (customizable via git config) asking for a commit log message, along with a list of what’s being committed:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch main
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
#modified: hello.py
Enter fullscreen mode Exit fullscreen mode

Git doesn't require commit messages to follow any specific formatting constraints, but the canonical format is to summarize the entire commit on the first line in less than 50 characters, leave a blank line, then a detailed explanation of what’s been changed. For example:

Change the message displayed by hello.py

- Update the sayHello() function to output the user's name
- Change the sayGoodbye() function to a friendlier message
Enter fullscreen mode Exit fullscreen mode

It is a common practice to use the first line of the commit message as a subject line, similar to an email. The rest of the log message is considered the body and used to communicate details of the commit change set. Note that many developers also like to use the present tense in their commit messages. This makes them read more like actions on the repository, which makes many of the history-rewriting operations more intuitive.

How to update (amend) a commit

To continue with the hello.py example above. Let's make further updates to hello.py and execute the following:

git add hello.py
git commit --amend
Enter fullscreen mode Exit fullscreen mode

This will once again, open up the configured text editor. This time, however, it will be pre-filled with the commit message we previously entered. This indicates that we are not creating a new commit, but editing the last.

Summary

The git commit command is one of the core primary functions of Git. Prior use of the git add command is required to select the changes that will be staged for the next commit. Then git commit is used to create a snapshot of the staged changes along a timeline of a Git projects history. Learn more about git add usage on the accompanying page. The git status command can be used to explore the state of the staging area and pending commit.

The commit model of SVN and Git are significantly different but often confused, because of the shared terminology. If you are coming to Git from a personal history of SVN usage, it is good to learn that in Git, commits are cheap and should be used frequently. Whereas SVN commits are an expensive operation that makes a remote request, Git commits are done locally and with a more efficient algorithm.

Top comments (0)