Git Commit
Hey there! Hope you’re doing well.
In the last blog, we saw what staging and tracking in Git mean. Staging is the process of adding files to the next commit to be made. We saw different commands for staging a file, removing a stage file, and see the status of the repo. In this blog, we will know about Git commits in detail.
A commit is an integral part of the Version Control System. The whole point of Git is to manage code. Commit creates a “checkpoint” or a “snapshot” for an instance of your repo when it's made. Commit records the history of the changes made to your files and protects them from any accidental harms like deleting a file by mistake, failure of storage device, etc. The commits you make in the repo are then pushed to remote which contains updated instances of files and folders. You can visualize it in this way: Each commit wraps up the latest files (which are included in that commit) and saves them and records the changes made to them like which line in which file has been modified, which lines have been added, and which ones are removed, in comparison to the last commit. This means any modifications made after a commit is included in the next commit.
A Git Commit contains the current contents of the index and the log message, also called commit message which describes the changes included in the commit. A Git commit contains different parts, which we will discuss later in this blog.
Let us see how to make a commit in the repo. To make commits, you must have all the required files staged in order to add them to the commit. If you don't remember them, check in the previous blog.
To stage all files:
git add .orgit add -Aorgit add <filename> //to stage a particular file
After that, we commit. A commit mandatorily requires a commit message.
A commit message means a sentence or paragraph describing what changes you have done in the current set of files. Let’s say you completed a UI page for the login screen and connected it to the backend. You have put so much effort into this UI screen and obviously don't want to lose it.😐 So you commit it and the commit message could be “Completed the UI screen for login and connected it to the backend. Need to connect to the database, make requests to server and test.” Commit messages are supposed to give a brief but clear indication of your work included in the commit so that if any other developer accesses your code from the remote, they should be able to understand what work has been completed or what's left and needs to be done or where should they start next.🧠💡 You can also make detailed commit messages to explain your task, like: “Database connection strings saved in an encrypted file. While accessing the database, some modules return the error messages as “Access denied” while some work just fine.” This could be a typical example of a commit message. This commit message is also displayed in your log, which we will see later and in remote repos.
Git Commit
Use the following command to commit:
git commitorgit commit -m "<commit message>"
The first option, git commit
will open a text editor(Vim or Nano) in the terminal itself where you will type your commit message, like the one below:
This is the vi editor. You need to type your commit message on the first line. If you don't type any commit message, the commit will be aborted, because like I said, a commit mandatorily requires a commit message. Now, vi is quite complex to use. This cheat sheet might help you.
Vi basic commands
To write in the window, press ‘ i ’, which will enable the insert mode. Only after this, you can type the message.
To save and exit, press the ESC key, then ‘:’, then “wq”. Like this: :wq
. w means to write and q means quit. So, it will write the message and quit. If you simply want to quit, without writing, or even if you have written, just erase everything and press ESC, then “:q”. You can google for more details on vi or for more commands.
To eliminate all this extra work, you can use the second command to commit, git commit -m "<commit message>"
. The -m flag is for the message. Then type your message under the double-quotes. If you want to type only sentences, this command is okay, but if you want to give a detailed message, with bullet points and all, you can go for the first option.
As you can see, we have made a commit using the second option (with the message in the command itself). You can see the output of the command: it shows that 1 file is changed and 1 line has been inserted. Had you written more, the total number of line changes will be displayed, like the number of lines deleted. And you can notice I misspelled the second last word. We will see how to correct it below.
Amending a commit
There will be instances where you miss out including a file in a commit, or remember to make an important change in any file like README or make some mistake in a commit message, like a spelling error, or referring to a wrong file. But the commit has already been done. Now what?🤯
Git is designed to give you total control over your development workflow. You can amend a commit to rectify the changes.
It is not recommended to amend a commit after it has already been pushed to the remote because if other developers have already pulled your changes, it may create problems and merge issues. The amended commits are actually entirely the new commits and the old one is removed from your branch.
Now there are different scenarios of manipulating a commit.
- If you have to change only the message and not any files(when there is nothing staged; immediately after you commit a mistake in it), you can use the following command to alter the message without altering the snapshot:
git commit --amend -m "the updated commit message"
As we already know, -m
is for message and --amend
is the amend option.
Like you can see, I changed the commit message using the command for amending, and we a similar output, like the one we got when we made an actual commit. This also illustrates that the old commit is deleted and a new commit is made as I mentioned before.
2. Changing committed files: If you forget to edit a file, or want to add a new file to the commit, first stage your changes (git add <filename>
)and then use the following command:
git commit --amend --no-edit
The --no-edit
flag lets you modify the commit without changing the commit message.
Let’s see this in action by modifying our file, then staging it, and amending the commit. Right now I have only one line, so I’ll add more lines.
Now stage it using the command which I mentioned earlier. Guess which one..!
Now again, we have modified the file and included it in the same commit. The output shows 1 file changed and 3 insertions, just what we did.
Log of the commits
We have a very useful command that lets us see all our commits. It lists all commits made in a repo since beginning, in chronological order with details such as commit id, timestamp, contributor name and email and commit message.
to see the list of all commits, use the following command:
git log
This lists all the commits of a repo. You may not be able to see all commits together. So press q and you will see further commits.
Parts of a commit
- The command
- Author name & email id and Datetime stamp of the commit- There may be multiple contributors to a project, and it is necessary to know who made which changes. This helps in identifying that.
- The commit message- Notice it has our updated commit message.
- Commit id
The HEAD is like an alias to your latest commit.
In the next blog, we will discuss git branches.
My name is Harsh Seksaria. I like to write about the topics simplifying them as much as possible for you to understand without unnecessary effort. Still, sometimes something may not be very clear. If you find any mistake, error, or you have a suggestion, just mail me. I’ll try to improve it asap.
My email: harsh20.freelance@gmail.com
My LinkedIn: www.linkedin.com/in/harshseksaria