Commit Files – git commit
git commit
is the command to register files in your Local Repository. Once the files are registered with the git commit
command, you can retrieve the saved version of the set of files anytime.
Commit message
When you commit files, you need to write some messages about the commit. The messages are recorded along with the code. Typically, you describe what changes were made.
When you run the commit command without the "-m
" option, which will be explained below, the command line will get into the waiting mode...
git commit
hint: Waiting for your editor to close the file...
...while the text editor will open up for adding a commit message.
After typing the commit message, save it and close the file.
the first commmit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
# modified: git_practice.html
#
A new commit will be created as shown below.
git commit
[master 0d93dbb] the first commit
1 file changed, 1 insertion(+), 1 deletion(-)
There are some frequently used short-cut options for this command.
"-m" option
The -m
option is used to describe a commit message directly in the command. If you don't use this option, a registered text editor is launched automatically. In that case, you need to write a commit message in the text editor, save it, and close the text editor. The -m
option is useful to make the operation quicker.
git commit -m "Fixed Bugs"
"-a" option
Another option frequently used is the -a
or --all
option. To commit a modified or deleted file, you need to add the status to the Staging Area by running the git add
command. The -a
or --all
option allows you to directly make a commit (skip the Staging Area). This option is not applicable to untracked files. For example, when you create a new file from the last commit, the file needs to be added to the Staging Area by running the git add
command.
git commit -a
Combine options
You can use these two options together. For example, if you modify some files to fix a bug and you want to commit the status, you can run the following command.
git commit -a -m "Fixed Bugs"
Or
git commit -am "Fixed Bugs"
Note: No file path is needed for the git commit command
Another note for the git commit
command is that you don't need to specify a file or directory path differently from the git add
command. Because the Git system already tracks the files in the Working Tree and Staging Area, the git commit
command automatically detects what files need to be committed.
Tips: Git source control in VS Code
VS Code also provides a GUI (Graphic User Interface) for the Git source control feature. Using the feature, you can make commits using the GUI.
After you edit your code, you can make a commit by typing a commit message and pressing the Commit button.
Using the Git source control GUI, you can do many other things. See the following link to learn more about the Git source control in VS Code — Using Git source control in VS Code.