Committing in Git is the last stage of the three stages we discussed in Introduction to Git. Before committing, we have a staging area where we add the changes. So in this tutorial, we will create a file and try to commit some changes to it. For this, we need to know few things beforehand.
Pre-Requisites
- How to Create a Repository (Refer tutorial)
- Navigating to another directory through Git Bash (Refer tutorial)
In this tutorial, we will be focusing on
- How to create a file in Git
- How to add the file in staging area
- Committing the changes in Git
- Committing the changes in Git without commit message
How to Create a file using Git Bash
For creating a file through Git Bash you have to first create a repository and navigate to this directory as the present working directory. I hope we are working in the First Project directory that we created in the tutorial about the Git repositories. After you are in the correct repository. Type the following command
touch <filename with extension>
Press enter to create the file named harish.txt.
You can see the directory to see that the file has been created successfully (or execute the ls command to verify, refer). For writing something in your file, type the following command in Git Bash
echo "Your Message" > filename
Press enter and the message would be written inside the file.
Now that we have the file and have also made the changes to it by writing a message, we will try to commit the changes. In the introductory chapter, we learned that we need to add the file to the staging area and from here only we can commit the changes we made to the file. So here, before committing, we have to add the file to the staging area.
How to add the file to the staging area
For committing the changes, first of all, we must be inside the staging area. Let's see what happens when we commit directly and have not added the file to the staging area beforehand. Type the following command
git commit
This command is used to commit the changes. But see that Git says there is nothing added to commit but untracked files present. This is because we have nothing in the staging area. We can only commit the changes if we have a file in the staging area. Notice the following line in the above image
Untracked files:
harish.txt
This means we have some files with changes but have not added in the staging area. A file is said to be tracked if it is in the staging area and all of its changes are regularly tracked. So for a successful commit, we need to add the file to the staging area. For doing the same, type the following command
git add <filename>
Now our file is in the staging area. We can now proceed to commit the changes.
How to commit a file in Git?
Although commit is a huge process and all of them cannot be discussed in the same tutorial. We will slowly progress in the coming tutorial. Committing the changes is a simple command in Git. Just type the following command.
git commit -m "This is my first commit"
As you can see the changes have been committed with the commit message "This is my first commit".
1 file changed is the file we just added to the staging area. Now it is important to discuss the message part in Git that we wrote above.
You can also commit by writing the commit message in the text editor about which we learned in setting up the notepad++ for git bash. For writing the commit message, just write git commit without the -m flag
Press Enter. Your text editor will open up.
You can enter your commit message here.
Press Ctrl + S to save the message and close the editor. Once you close the editor the changes will commit with the commit messages.
Committing in Git without a message
In the previous section, we looked that we committed with the message "This is my first commit" and the changes were committed successfully. Also, we looked at how to commit in notepad with the commit message. But, in those sections, we tried to commit when there were actually some changes to commit. What if there aren't any? For that first commit all the changes in Git with the proper commit command and message. Once that is done, type the following command
git commit
So if you don't have anything to commit, you will be told that there is nothing to commit without the text editor being opened. So if your text editor opens up after the command, there is definitely something to commit.
Why we need to have commit messages?
Git does not recommend to commit without any message. Git commit messages are necessary to look back and see the changes made during a particular commit. If everyone will just commit without any message, no one would ever know what changes a developer has done. Moreover, you won't be able to track down these changes once you see the history. So, Git does not recommend this.
What happened in the first scenario was that there was something to commit and committing it with just "git commit" is not allowed in Git.
But in the second scenario, we were actually not committing anything and git knows that since you are trying to commit and you did not have anything to commit, it will just display the message that there is nothing to commit. So actually you are not committing anything in the second scenario, therefore the git commit is executing successfully. The same goes for when you have nothing in the staging area but have made the changes which are called untracked files as discussed above.
Now, Git does not recommend to commit without any commit message does not mean that we cannot commit without a message. It is allowed but not recommended. To commit in Git without any commit message, follow these simple steps with a slight change in the previous command.
-
Open your Git Bash
-
Make some changes to the file we created above (harish.txt)
3.Add the file to the staging area
- Type the following command
git commit -a --allow-empty-message -m ' '
- Press enter and it is done.
This way we can commit the changes in Git without any commit message. It is not recommended though. We will move on to the next tutorial, keep practicing the changes and commit in Git.