Committing in Git
A commit is a point in git which is treated as a “Save point” which keeps records of all save points, so if we want to go back to the previous save point we can switch back. it is the backbone of developing and updating software according to the passage of time and client demands. As per the requirement, we can toggle back to make changes. Commits are like a snapshot of all the files and folders present in the repository at a particular point in time. Let us discuss it with help of an illustration that is as follows:
Illustration:
Suppose if somebody changed something in the file say “a.txt” we need to add those changes in the repository for that we will be using the command git add file_name, and to make a commit we will be using the command.
git commit -m "Commit_message" file_name
Shortcuts: Now if we have changed a lot of files in the directory, so to avoid using git add file_name for every file we can avoid this by writing some shortcuts so now let’s have a look at the commands. Following are some of the shortcuts to add the files.
Git Commands | Action Performed |
---|---|
git add –all | Add all the files present in the repository |
git add . | Add all the files present in the current repository |
git add -u | To only add files that are currently tracked it will not track any other file which is not being tracked by the git |
Now if we have modified any already existing files we can use the command git commit -am “Commit_message_here” this command will add and commit a file at the same time.
Good commit messages: So now if someone is traversing through git logs he/she should understand are the changes being done, why it has been done, and how it has been done.
Amending a commit
If some latest commit has been made into the repository that is not yet pushed to the upstream repository on GitHub and if you think that the commit message is incorrect we can edit or amend the commit message by using the below command as follows:
git commit --amend
The above command will open the default text editor for e.g vi, vim, or emacs and when it gets open you can see the commit message which you have used earlier now you can edit that commit message to whatever message you find suitable. It is illustrated via below pictorial aids as follows:
If we want the previous commit message only without changing it.
git commit --amend --no-edit
Committing without opening an editor:
Git usually opens up a default editor like vi, vim, or emacs on running the git commit command so to avoid this we can also type in the commit message in the git commit command only bypassing the -m option.
git commit -m "Commit message here"
We can also multiple messages as arguments in one command using the below command as follows:
git commit -m "message_1" -m "message_2"
Committing Changes Directly
For tracking the changes made to a file or a folder we first add it into the staging and then we commit that file but we can do this all in just one the command which will add the changes made to the file along with a commit message The command is basically as follows:
git commit -am "commit_message here"
For committing a particular file present in a folder for that we can use the below command as follows:
git commit /path_of_the_file -m "commit_message here"
Now let us discuss which line should be staged for committing for selection
Suppose we have done many changes in one or more files but at last, we only want some of the changes to be committed so to select those changes we can use the below command so that we can select the desired changes.
The command is git add -p file_name and whatever change we have done will be displayed individually for each change, we will be prompted to choose one of the following options. Let us understand the various options given by the command git add -p command
It is depicted below in tabular format below as follows:
Symbol | Action Performed |
---|---|
y | Yes add this hunk |
n | No don’t add this hunk |
d | No, don’t add this hunk, or any other remaining hunks for this file. useful if you have already added what you want to, and want to skip over the rest |
s | split the hunk into smaller hunks if possible |
e | Manually edit the hunk. This is probably the most powerful option it will open the hunk in a text editor and you can edit it as needed. |
Creating an Empty Commit
As we know that while doing a commit we need to create a file or make changes in a file to commit the file but creating an empty commit will help us without having to edit or create a file we can easily create commits.
The –allow-empty will help us in creating the commit without having to edit or create a file.
Committing Changes in Specific Files
As we know that for committing changes made to a file for that we need the file to be added in the staging area and then make a commit of those particular here in this explanation the files are already in the staging area that’s why on using the command git commit file_name1 file_name2 command is working.
Committing at a Specific Date
While committing a file if you want to set a particular date that will appear in the standard output of the git log. For that, the command will be git commit -m ‘commit_message” –date YYYY-MM-date.
Let us see our commit in the git log
The date parameter accepts a lot of flexible formats which are being supported in git.
git commit -m 'commit_message' --date yesterday
git commit -m 'commit_message' --date '3 days ago'
But when we don’t specify time git uses the current time and only the date changes then. Now if you want that the time should be changed.
Amending the Time of commit:
We can amend the time of the commit using the below command as follows:
git commit --amend --date="day_name month_name date time YYYY -0400"
Or even
git commit --amend --date="now"
The second command shows the current date and time