Git Commit
The git commit
command captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as “safe” versions of a project. Git will never change them unless you explicitly ask it to. You should make new commits often, based around logical units of change. Over time, commits should tell a story of the history of your repository and how it came to be the way that it currently is. Commits include lots of metadata in addition to the contents and message, like the author, timestamp, and more.
Prior to the execution of git commit
, the git add
command is used to promote or 'stage' changes to the project that will be stored in a commit. These two commands git commit
and git add
are two of the most frequently used.
Git Commit Syntax:
git commit -m "descriptive message"
Git Commit Common Usage
git commit
: The command will starts the commit process, but since it doesn't include a-m
flag for the message, your default text editor will be opened for you to create the commit message. If you haven't configured anything, there's a good chance this will be VI or Vim. (To get out of VI or Vim editor, press esc, then:wq
, and then Enter.)Example:
touch hello-world.txt
git add hello-world.txt
git commitgit commit -m "descriptive commit message"
: This command will starts the commit process, and allows you to include the commit message at the same time.Example:
touch hello-world.txt
git add hello-world.txt
git commit -m "first commit"git commit -am "descriptive commit message"
: In addition to including the commit message, this option allows you to skip the staging phase. The addition of-a
will automatically stage any files that are already being tracked by Git (changes to files that you've committed before).Example:
git commit -am "second commit"
git commit --amend
: Replaces the most recent commit with a new commit.Example:
git commit --amend
The above command will prompt the default text editor and allow us to edit the commit message.
You can learn more about the
git commit
command and its options in git-scm's documentation.
We may need some other essential operations related to commit like revert commit, undo a commit, and more, but these operations are not a part of the commit command. We can do it with other commands. Some essential operations are as follows:
- Undo commit: Visit Git Reset
- Revert commit: Visit Git Revert
- Remove commit: Visit Git Rm