loading...

Git — how to commit

chriss profile image Kristijan Pajtasev Originally published at Medium on ・3 min read

Git — how to commit

Commit is a primary Gitfeature, and in my last post, I described how it works. But how to make one? That is something I am covering in this post. While making it is a simple CLI command, there are some small tips on committing I am using during my work.

Git logo

Command

To commit all you need to do is execute one small, simple CLI command.

$ git commit

It would be a short post if that would be all. There are numerous flags you can use with this command. A full list of them is in the official commit documentation. Below are just some of the most commonly used ones.

-m

This flag is the most commonly used flag when making a commit. With this flag, we set a description text of our commit.

$ git commit -m “example of a message”

--amend

Often it happens that we make some mistake in a commit message. Maybe some typo, copy-paste error, or we didn’t type everything we wanted. If commit still wasn’t pushed, by using this flag, we can edit a commit message.

$ git commit --amend

-a, --all

Adding changes is something done before committing. This action is something you would usually do with add command, but this flag is also one of the ways to do it.

$ git commit -a
$ git commit --all

--author

When making a commit, Git looks into the global configuration for setting the author of that commit. To override global value, we can use this flag.

$ git commit --author=”John Doe <john@doe.com>”

Tips and tricks

Commit size

While you are working, you usually want to commit as often as possible and keep commits small. One of the big reasons to use Git or any versioning system is reverting changes when things go wrong. Having many commits helps us to revert only something that needs to be reverted and not something that is not. Also, commits are cheap. Having one or thousand won’t make a performance issue.

Commit message

You can set a message to be anything. Sometimes, companies have some validation rules set, but it can be anything. However, it is encouraged to use some meaningful message. It should be something that describes your change. Like that, when you are looking for some commit two months later, it is easy to find it. Also, most companies today use Jira for tracking, just like my current company. That is why I prefer to start each message with the Jira ticket number. Like this, it is much easier to find the reason for some change. Or in the other direction, find all changes made for some feature. This message formating is something you can often find in financial companies because of strict regulations.

$ git commit -m “JIRACODE-123: commit message”

Wrap up

Commit isn’t complicated command. A straightforward one. Most often, you only use -m flag with it. But to do any work with Git it is the most important one to understand. Keep commits small and keep commit messages clear and relevant. There are also some more advanced operations that you can do with them, like squash commit. But that is something you can look at when you have more experience.

Discussion

pic
Editor guide