Skip to content

git commit#

Once we do commit it, it's (theoretically) in the repository forever. There are two ways you commit things to a Git repository:

  1. You can provide a single message on the command line
  2. Or you can start the commit process and open up an editor to enter your commit

Let's try both.

First we will use the editor approach:

git commit in Vi editor

git commit in Vi editor

The reason git commit opens an editor is you have to provide it with a commit message. This is a message for other, future engineers who may review this commit you've made. A committ message should not repeat what changes you've made, because they're obviously visible in the commit itself, but instead should say why you made a change.

The git commit command decides what editor to load based on a few configuration options. I won't explore them all here because the manual page (man git and search for GIT_EDITOR), but just know that there are a few environment variables: EDITOR, VISUAL, and GIT_EDITOR. The latter, GIT_EDITOR only affects Git but the others affect everything on your system.

In the editor window above I'm going to type a commit message: We needed a file to greet people.

Then I save the file and exit the editor. Then I get this output from my git commit:

$ git commit
[master (root-commit) 82c315f] We needed a file for greeting people.
 1 file changed, 1 insertion(+)                                                    create mode 100644 hello.txt

So we commited to the master branch, this was the root-commit (first ever commit on the repository), and we have a commit hash of 82c315f (this is a only a smaller version of it).

We can see our file there too: hello.txt. Git is telling us (on the right of the filename) that we're creating the file (create mode).

Altogether we get 1 file changed, 1 insertion(+) inside of this commit.

That's a commit in a nutshell, but we'll also create some more files to put inside the repository and see it in action a bit more.

Next let's look at our git log so we can see the commit and the information inside of it.