Open In App

What is Git Commit?

Last Updated : 15 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Git is a distributed revision control system with an emphasis on speed, data integrity, and support for distributed, non-linear workflows. Every Git working directory is a full-fledged repository with complete history and full version-tracking capabilities, independent of network access or a central server.

How do you insert items from your Staging Area into your Local Repository?

For this git commit is used and the command is:

git commit -m “commit message”

The git commit command is used to move files from the staging area to your local repository. This command is run after git add and it can be seen as a checkpoint. After executing the git commit, your staging area will be empty.

 

Working with Git Commit

Suppose, you are on the master branch and you did a commit now git internally will create a block i.e., a node, of a commit you did. In git, we have a pointer called HEAD (It is the reference to the commit in the current branch in our case i.e., the master branch). When you commit something the HEAD will be pointed to the new commit and a hash key is assigned to that new commit. If you made another commit, again HEAD will move and point to the commit having another hash key. It can be assumed as a right to left singly linked list, whenever a new commit happens, a new node is created and points to HEAD, and after that HEAD is updated and comes over the new commit i.e., the latest one.

Flags in Git Commit

There are certain flags in git commit like -a, -m, -am.

  • Git commit -m “commit message”: A command which creates a commit with a commit message written under quotation.
  • Git commit -a: The command only includes modification to the files that have been added with git add at any time i.e., all the tracked files.
  • Git commit -am “commit message”: It is a combination of both the -m and -a command.

If you now find that you accidentally left something out of your last commit, be it a file or an extra change to a file that you just committed, then you can fix that also. Let’s now make some modifications in the “file.txt” i.e., I write “Hello Crio” in that and now we have to repeat the process of add and commit.

 

After this, the file “file.txt” has been modified and you can check that using the git command git show.

 

If you want to view the history of everything that happens to a repository then you can use the git log command. A Git log is a running record of commits.

 

As you can see above there are two committing histories, let’s say you did a mistake in “file.txt” so you have the repeat the process once again and hence there is one more committing history generated. For the small number of history, it is fine, but if you are making mistakes several times and committing them then there is a huge number of history created and it is a bit confusing for the other developers when working with complex types of projects. So to handle that, git commit –-amend command is used. git commit –amend -m “commit-msg” where -m is a flag used for the commit message is a command used to doesn’t create another commit history and is just overrides the previous commit.

 

Here in the above image, you can see that there is no third history created. What did here is the previous commit is overridden and hence the commit history remains the same.

Revert Commit Changes

If you want to revert a commit you have made you have to use:

git reset --hard HEAD^

This command will revert the latest commit means the top commit.

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads