Committing changes to the local git repository
In the previous article, we staged the changes we made to the files. Now, we will be committing our code to make the changes permanent and make additional changes on top of them.
Committing changes
Once we have staged our changes, we can now use git commit
command to commit the changes. Once the code is committed, git creates the snapshots of the changes which can be tracked through the change processes. Git will keep track of the author, timestamps, changes, commit hash, and message along with other details.
You can use the git commit command as follows:
$ git commit -m "Initial Commit"
Let's have a look at the above command. Here, we use the git commit
command to tell git we are ready to commit the changes we have staged. The -m
flag tells the commit that we will provide a commit message. We have passed "Initial Commit"
after the -m
flag which will tag the message to the commit.
Additionally, you can also use -a
flag to stage the tracked files automatically. We would run the following command to stage and commit tracked files (including changes and deleted files).
$ git commit -am "Stage and commit"
Amending commits
We tend to forget some changes more often than we would like to admit. When this happens you can always make further changes, and commit the changes with another commit. But what if there is a better way to do this? Well, you can just amend the last commit using the --amend
flag.
Just make the changes you missed and run the following command.
$ git commit --amend
This makes sure that the changes you have made are added to the last commit made on the git repository. You can verify the commit using git log
and see that no new commits were made. You can also use --no-edit
a flag if you do not want to edit the commit message. You can also use --amend
flag to update your commit message without making any changes.
In this article, we learned about the git commit command and some useful flags like -a
, -m
and --amend
. In the next article we will be diving into git remotes and explore how we can connect our local repository to Github.
Subscribe to my newsletter
Read articles from Abhishek KC directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Abhishek KC
Abhishek KC
Hi, I am data engineer from Nepal with over 3 years of professional experience and love working with ASP.NET, SQL Server and Raspberry Pis.