GIT COMMIT

Hemashree S

Git Commit

  • A commit is used to record changes in the repository after the git add command, containing index data and a commit message, and forming a parent-child relationship. When a file is added in Git, it goes to the staging area, and the commit command transfers updates from the staging area to the repository.
  • Staging and committing are closely connected. Staging lets us keep making changes to the repository, while committing records those changes when we're ready to share them with the version control system.
  • Commits act as snapshots of the project, each recorded in the master branch of the repository. They can be recalled or reverted to an older version. No two commits will ever overwrite each other because each has a unique commit ID, generated using the SHA (Secure Hash Algorithm).
  • Let's explore the various types of commits.

The git commit command

The commit command saves changes and generates a commit ID. If used without any arguments, it opens the default text editor for entering a commit message. You can specify your commit message in this editor. The process works as follows:
$ git commit
The command above will open the default editor, prompting you to enter a commit message. To commit the changes made to newfile1.txt, you can do the following:
Refer to the output below:

Git Commit

When you run the command, it will open the default text editor, prompting you to enter a commit message. The text editor will appear as follows:

Git Commit

Press the Esc key, then 'I' to enter insert mode, and type your commit message. Press Esc again, then type ':wq' to save and exit the editor. This completes the commit process successfully.
You can check the commit using the 'git log' command.
 Refer to the output below:

Git Commit

The output of the 'git log' command shows the commit ID, author details, date and time, and the commit message.
For more information about the log option, visit the Git Log documentation.

Git commit -a

The commit command also includes the '-a' option to commit all snapshots of changes. This option only affects files that have already been added to Git and does not include newly created files. Consider the following scenario:
We have updated the already staged file 'newfile3' and created a new file 'newfile4.txt'. Check the repository status and then run the commit command as follows:
$ git commit -a
Refer to the output:

Git Commit

The command will open the default text editor to prompt for a commit message. After typing your message, save and exit the editor. This process will only commit files that have already been staged and will not include files that haven't been staged. 
Refer to the output below:

Git Commit

As shown in the output above, the file 'newfile4.txt' has not been committed.

Git commit -m

The '-m' option for the commit command allows you to write the commit message directly on the command line, bypassing the need for a text editor. It is used as follows:
$ git commit-m "Commit message"
The command above will create a commit with the specified commit message. 
Refer to the output below:

Git Commit

In the output above, 'newfile4.txt' has been committed to the repository with the provided commit message.
You can also use the '-am' option for already staged files. This command will immediately commit the staged files with the provided commit message. It is used as follows:
$ git commit -am "Commit message".

Git Commit Amend (Change commit message)

The '--amend' option allows us to modify the most recent commit. If you accidentally use the wrong commit message, this feature is a useful way to correct it. It is used as follows:
$ git Commit -amend
The command above will open the default text editor, allowing you to edit the commit message.
There are additional essential operations related to commits, such as reverting or undoing a commit, which are not part of the commit command itself. These tasks are performed using other commands.

Some key operations include:

  • Git undo commit: Visit Git Reset
  • Git revert commit: Visit Git Revert
  •  git remove commit: Visit Git Rm
  





Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send