Git commits

This post will cover what Git commits are and how they are working. If you are new to Git and versioning systems, you can read my introduction post on this topic.

What is commit?

If we look at our software project as a game, a commit is save of our progress. You pass some level, win a battle against some boss, and you want to save how far you got. You do not want to start from the beginning next time you play. The same is with software, you write some lines of code, but you still have lots more to write. So you make a commit. Write some more and do some more commits.

Git timeline example

You can use different visualization tools to see commits timeline, but the image above displays an example of a project timeline. You make first commit, after that second, and by tracking line, you can see what changed between each commit.

Commit data

Each commit can carry different data. Some are mandatory, and some are optional. One necessary attribute is commit ID. This ID is the SHA-1 hash of everything important about this commit and uniquely identifies it. Other mandatory data are commit date and commit changes. There are two other pieces of data that are not required but are good practice to have them set. Those are commit message and commit author.

Commit change

Earlier, I wrote that commit is like a game checkpoint. But what does it save? Git tracks only differences between two commits. If I go back to the game analogy, you could have a character standing in a spot and make a save. Then we make it do two steps forward and the one back before making the next save. Kept in the commit is just a difference, and that is only one step ahead.

Now let’s drop game analogy and go back to software. Let’s say we have a file containing the following JavaScript code:

console.log(“Hello world”);

We could make a commit at this moment and add another line.

console.log(“Hello world”);
console.log(“Another line”);

If we would make a commit now, it would only contain this newly added line. It will ignore the first line because the first commit is tracking it.

And now to explain that two-step forward one backward example in code. We have a file with two lines of javascript code. First, we could add two lines.

console.log(“Hello world”);
console.log(“Another line”);
console.log(“Line 3”);
console.log(“Line 4”);

Before we make a new commit, we delete the third line.

console.log(“Hello world”);
console.log(“Another line”);
console.log(“Line 4”);

Making commit at this moment would only contain added this one line, and it would ignore that deleted line ever existing.

Changing a line is a special case. If the text in the first line, “Hello world”, would be changed to say “Hello world!”, git would see it as deletion and addition. First, it treats the original line as deleted, and this new line as added in its place.

Wrap up

Commits are not a complex feature but are a fundamental feature of Git. They show only changes done between two, and I hope this post made it easier to understand how it is doing that.

For more, you can follow me on Twitter, LinkedIn, or GitHub.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Cat Clicker

Left Eye has been clicked

How to Build a Highly Responsive UI with ResizeObserver API

Text File Download in React


How we mocked improving JavaScript

What is Node.js REPL and its Usage?

What is Node.js REPL and its Usage?

CSS-In-JS Vs Traditional CSS — Which Should You Use?

[Leetcode]19:Remove Nth Node From End of List

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


More from Medium

How git worktrees enhance your efficiency while also save memory

[Git] Collaboration with remote side

Quick reference guide to GIT COMMANDS