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.
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.
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.
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.
We could make a commit at this moment and add 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.
Before we make a new commit, we delete the third line.
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.
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.