Make a commit

I will explain how to commit with Git. You can commit with the git commit command. You need to be ready to commit.

To make a commit, you need to enter a comment that outlines the source code changes. Specify with the "-m" option. This comment is called a commit message.

#Enter a comment directly to commit
git commit -m "comment"

If you do not use the "-m" option, you can open it in an editor such as vi. This editor can also be set in the Git config file.

You can also open the #comment in an editor such as vi and write it before writing the commit message.
git commit

Confirm commit

Use the git log command to confirm the commit.

git log

This is a sample output of git log. At the top is the new commit. Make sure the commit message you just committed is at the top.

commit 99e77ecbb2fa0922aed2995ebc20eea20b559a87 (HEAD->main, origin / main)
Author: Yuki Kimoto <kimoto.yuki@gmail.com>
Date: Thu Jul 29 08:58:33 2021 +0900

    Added Get Window Text

commit 8166b40288648dab314147df751c2d27788b1d03
Author: Yuki Kimoto <kimoto.yuki@gmail.com>
Date: Wed Jul 28 08:53:01 2021 +0900

    Get list of child windows and get desktop window

commit 7768973127fa0495e1ac8ab745d2f10fc1c38f8a
Author: Yuki Kimoto <kimoto.yuki@gmail.com>
Date: Tue Jul 27 08:16:09 2021 +0900

    Summary of how to create a web browser automated operation robot in Perl

Note that this is from a personal site, so the commit message is pretty good, but if you're co-developing, write it a little more carefully.

How to get back the wrong commit?

Immediately after committing, I realized that I had committed by mistake. In this case, how can I get the wrong commit back?

Use the " git reset" command to revert to the previous commit. Check the previous commit ID. In the above example, it is the following "8166b40288648dab314147df751c2d27788b1d03".

commit 8166b40288648dab314147df751c2d27788b1d03
Author: Yuki Kimoto <kimoto.yuki@gmail.com>
Date: Wed Jul 28 08:53:01 2021 +0900

    Get list of child windows and get desktop window

Specify this commit ID in the argument of "git reset".

# Revert to the previous commit
git reset 8166b40288648dab314147df751c2d27788b1d03

If you make a mistake, don't specify the "--hard" option in the "git reset" command. All the file changes in the working directory are returned and tears come out. If you have a file open in a text editor and the content remains, overwrite it quickly.

Associated Information