Git Commit
Git Commit
Once you have added/staged your files, the next step is actually to commit the changes. So if you run git status
again, you will be able to see that Git tells us that there are changes to be committed:
In this case, it is only the README.md
file that will be committed. So in order to do so, we can run the following command:
Rundown of the command:
git commit
: here, we are telling git that we want to commit the changes that we’ve staged with thegit add
command-m
: this flag indicates that we will specify our commit message directly after that- Finally, in the quotes we’ve got our commit message, it is important to write short and descriptive commit messages
In our case we could set our commit message to something like "Initial commit"
or "Add README.md"
file, for example.
If you don’t specify the -m
flag, Git will open the default text editor that we’ve configured in chapter 5 where you will be able to type the commit message directly.
Committing directly without staging files:
If you have not already staged your changes using git add
command you can still directly commit all your changes using the following command.
The -a
flag here will automatically stage all the changes and commit them.
Signing Commits
Git allows you to sign your commits. Commits signed with a verified signature in GitHub and GitLab display a verified label as shown below.
To sign commits, first you need to:
- make sure that you have GNU GPG installed on your host.
- Generate a GPG signing key pair if you don’t already have
- Use the
gpg --list-secret-keys --keyid-format=long
command to list the long form of the GPG keys
- Copy the long form of the GPG key ID you’d like to use. In this sample, the GPG key ID is
E630A0A00CAA7AAA
. - Export the public key:
- Copy your GPG key, beginning with
-----BEGIN PGP PUBLIC KEY BLOCK-----
and ending with-----END PGP PUBLIC KEY BLOCK-----
. - Login to GitHub or GitLab and add a new GPG key under settings:
- Set your GPG signing key in Git: (If you intend to add the signing key per repository, the omit the
--global
flag)
- Enable automatic signing for all commits:
- Or Sign per commit by passing
-S
option togit commit
:
After running the git commit
command, we can use the git status
command again to check the current status:
Output:
As you can see, Git is telling us that there are no changes to be committed as we’ve already committed them.
Let’s go ahead and make another change to the README.md
file. You can open the file with your favorite text editor and make the change directly, or you can run the following command:
The above would add a new line at the bottom of the README.md
file. So if we were to run git status
again, we will see the following output:
As you can see, Git has detected that the README.md
file has been modified and is also prompting us to use the command that we’ve learned to first stage/add the file!
In case that you wanted to change your last commit message, you can run the git commit --amend
command. This will open the default editor where you can change your commit message. Also, this allows you to change the commit changes.
The git status
command gives us a great overview of the files that have changed, but it does not show us what the changes actually are. In the next chapter, we are going to learn how to check the differences between the last commit and the current changes.
To check for commits that changed particular file you can use the --follow
flag:
The above shows the commits that changed the file, even across renames.