/    /  Git – Local repository

Git Local Repository

 

As soon as a file is created, it will be added to the local repository by adding and committing it.

Git Commit :

  • After git add, it is the next command that records the changes in the repository. Every commit contains index data and a message. Every commit forms a parent-child relationship. Git adds a file in the staging area. Git commits updates from the staging area to the repository.
  • Staging allows us to continue making changes to the repository, and committing allows us to share these changes with the version control system.
  • The master branch records every commit.Every commit can be recalled or reverted. Two different commits will never overwrite each other because each commit has its own commit-id. It is a cryptographic number generated by the SHA algorithm (Secure Hash Algorithm).

Git commit command :

The commit command commits the changes and generates a commit-id. The commit command without an argument will open the default text editor and ask for a commit message. Here’s ours:

$ git commit  

It’ll open a default editor and ask for a commit message. We’ve changed newfile1.txt and want to commit it.

Here’s what it looks like:

Git Local Repository

When we run the command, a default text editor will pop up and ask for a commit message.

Git Local Repository

Then press Esc after that ‘i’ for insert mode. Type whatever you want. Press Esc after that ‘:wq!‘ to save and exit from the editor.

Check the commit by running git log. Here’s what we get:

Git Local Repository

Log option displays commit-id, author details, date and time, and commit message in the above output.

Git commit -a

You can specify some commits by using the -a option. This option only considers files that have already been added to Git. It will not commit newly created files. Consider the following scenario:

The staged file newfile3 has been updated and a new file newfile4.txt has been created. Run the commit command and check the status of the repository:

$ git commit -a  

Consider the output:

Git Local Repository

This command will prompt our default text editor for a commit message. Type the commit message and save and exit the editor. Only the files that are already added will be committed. The files that haven’t been staged won’t be committed. Consider the output below:

Git Local Repository

According to the above output, newfile4.txt has not been committed.

Git commit -m :

When you use the -m option of commit, you can write the commit message directly on the command line. This will not prompt the text editor.

$ git commit -m "Commit message."  

With the above command, a commit will be made with the given commit message. Below is the output:

Git Local Repository

A newfile4.txt file is committed to our repository with a commit message in the output above.