Skip to main content

Commit

What is a Commit?

A commit is a snapshot of the repository at a specific point in time. It is a way to save your work and to share it with others. It stores the changes you made to the repository. It also stores the commit message, the author, the committer, the timestamp, the parent commit, and the tree.

Imagine you have a repository with a file named main.js. You made some changes to the file and saved it. Now, you can commit the changes you made to the repository. When you commit the changes, git will store the changes you made to the repository, commit message, author, committer, timestamp, parent commit, and tree.

How to Commit

Before you commit, you need to make some changes to the repository, and add the changes to the staging area. Then, you can commit the changes by using the git commit command like this:

Add changes to the staging area
git commit -m "Add new login functionality."

-m is the flag that you can use to pass the commit message as an argument. This is the most commonly used flag.

You can also use the -a flag to commit all changes.

Add and commit all changes
git commit -a -m "Add new login functionality."

This is the same as running git add . and then git commit -m "message".

Options

git commit command has many options. Here is a list of the most commonly used options:

ArgumentDescription
-mThe commit message.
-aCommit all changes.
-vShow the diff of the changes.
-sSign the commit.
-SSign the commit with a GPG key.
-uUse the default editor to write the commit message.
-eUse the default editor to write the commit message.
-FRead the commit message from a file.
-cCommit using the commit message of the specified commit.
-CCommit using the commit message of the specified commit.
--amendAmend the last commit.
--no-verifySkip the pre-commit and commit-msg hooks.

Above, we have seen how to commit changes to the repository. Now, let's see what a commit message is.

Commit Message (Rules)

A commit message is a short description of the changes you made in a commit. It is a way to document your work and to help others understand your changes.

There are no strict rules for writing commit messages but When working on a project, it's important to communicate clearly and concisely about the changes you've made. One way to do this is through the use of keywords in your commit messages. These keywords, or labels, help to indicate the nature of the changes and make it easier for others to understand the context of your contributions.

Here are some common keywords and what they indicate:

FEAT

Use this keyword to indicate that you are committing to a new feature.

"FEAT: Add new login functionality."

FIX

Use this keyword to indicate that you are committing a fix for a specific problem or issue.

"FIX: Fix bug causing crashes on certain devices."

STYLE

Use this keyword to indicate that you are making changes to the style or formatting of the code, but not its functionality.

"STYLE: Update indentation in main.js."

REFACTOR

Use this keyword to indicate that you are making changes to the code that improve its structure or organisation, but do not add new features or fix bugs.

"REFACTOR: Refactor the code to improve readability."

TEST

Use this keyword to indicate that you are adding or updating tests for the code.

"TEST: Add new unit tests for login functionality."

CHORE

Use this keyword to indicate that you are making changes to the build process or other tasks that are not directly related to the code itself.

"CHORE: Update dependencies in package.json."

PERF

Use this keyword to indicate that you are making changes to improve the performance of the code.

"PERF: Optimize image loading for faster performance."

CI

Use this keyword to indicate that you are making changes to the continuous integration process.

"CI: Fix issue with test pipeline on Dashboard CI."

BUILD

Use this keyword to indicate that you are making changes to the build process.

"BUILD: Add new script for building the production version of the app."

By using these keywords in your commit messages, you can help to make your contributions more clear and more understandable to others. However, it is important to note that these are just suggestions and not all projects use them, it's important to check the project's documentation to see if there are any specific guidelines you should follow.

Commit Hash

Before we talk about commit hash, let's talk about hashing in general

What is Hashing?

Hash is a function that takes a string of text and generates a fixed-length string of letters and numbers. It is also known as a hash function or a hashing algorithm. It is used in many applications, including cryptography, data integrity, version control, or password storage.

The most popular hashing algorithm is SHA-1, which is used in git, cryptography, and many other applications. It is designed to be a one-way function. This means that it is easy to generate a hash from a string of text, but it is very difficult to generate the original string of text from the hash. This makes it ideal for generating unique identifiers for commits.

What is a Commit Hash?

A commit hash is a 40-character string of letters and numbers that uniquely identifies a commit in a git repository. It is used to reference a specific commit in a git repository. It is also used to reference a specific commit in a git command.

How commit hash is generated?

A commit hash is generated by taking the contents of a commit and passing it through a hashing algorithm. The hashing algorithm takes the contents of the commit and generates a 40-character string of letters and numbers. This string is the commit hash.

Content of a commit includes the commit message, the author, the committer, the timestamp, the parent commit, and the tree. For example, commit content can be:

tree 62a0ee91b2b9c1e2e3f4a5b6c7d8e9f0a1b2c3d4
parent 62a0ee91b2b9c1e2e3f4a5b6c7d8e9f0a1b2c3d4
author mrizwanashiq <mrizwanashiq@outlook.com>
committer mrizwanashiq <mrizwanashiq@outlook.com>
timestamp 2021-07-01 12:00:00 +0000

These contents are passed through a hashing algorithm to generate a 40-character string of letters and numbers. This string is the commit hash. For example, the commit hash of the above commit content is 62a0ee91b2b9c1e2e3f4a5b6c7d8e9f0a1b2c3d4.

Uses of Commit Hash

It is used to uniquely identify a commit in a git repository. It is used to reference a specific commit in a git repository. It is also used to reference a specific commit in a git command.

How to Reference Commit Hash

It can be referenced in a git command by using the commit hash as the argument. For example, to checkout a specific commit, you can use the git checkout command and pass the commit hash as the argument.

Checkout a specific commit
git checkout 62a0ee91b2b9c1e2e3f4a5b6c7d8e9f0a1b2c3d4

It will checkout to that commit, means that the working directory will be in the state of that commit. It also updates the HEAD to point to that commit. It is super useful when you want to go back to a specific commit to see the code at that time. I do it all the time. It also can be used to revert a commit, but I will talk about it later.

Hash is 40 characters long, but you can also use the first 7 characters of the hash. This is called a short hash. For example, the short hash for the commit 62a0ee91b2b9c1e2e3f4a5b6c7d8e9f0a1b2c3d4 is 62a0ee9.

Checkout a specific commit using short hash
git checkout 62a0ee9

Undoing Changes AKA Reverting Changes

There are two ways to undo changes in git. One is to revert a commit and the other is to reset a commit. Let's talk about both of them.

git reset

The git reset command resets the current branch to the specified commit. It also discards all the commits that are newer than the specified commit.

There are three types of git reset:

  • git reset --soft - It keeps the changes in the working directory.
  • git reset --mixed - It keeps the changes in the staging area.
  • git reset --hard - It discards all the changes.

If we don't specify the --soft, --mixed, or --hard option, it defaults to --mixed.

Reset last commit
git reset --soft HEAD~1

It will reset the current branch to the previous commit. It will also keep the changes in the working directory. It is useful when you want to undo the last commit and keep the changes in the working directory.

To undo any commit, you need to know the commit hash of that commit. You can get the commit hash by using the git log command.

Reset any commit
git reset --hard 62a0ee91b2b9c1e2e3f4a5b6c7d8e9f0a1b2c3d4

git revert

Creates a new commit that undoes the changes made by the original commit. This can be a safer option if other people are also working on the same branch.

To revert the last commit, you can use the git revert command and pass the commit hash of the last commit as the argument.

Revert last commit
git revert HEAD

To revert any commit, you need to know the commit hash of that commit. You can get the commit hash by using the git log command.

Revert any commit
git revert 62a0ee91b2b9c1e2e3f4a5b6c7d8e9f0a1b2c3d4

Result:

[main 1b2c3d4] Revert "Add a new file"
1 file changed, 1 deletion(-)

Local Commits vs Remote Commits

There are two types of commits:

  • Local commits: These commits are not pushed to the remote repository. They are only available in the local repository.
  • Remote commits: These commits are pushed to the remote repository. They are available in the local repository and the remote repository.

Summary

In this article, we learned about git commits. We learned about the anatomy of a commit, how to create a commit, how to reference a commit, and how to undo a commit. We also learned about local commits and remote commits.