git commit
Git commit
Definition
Definition
The git commit command saves all currently staged changes of the project. Commits are created to capture the current state of a project. Committed snapshots are considered safe versions of a project because Git asks before changing them. Before running git commit command, git add command is used to promote changes to the project that will be then stored in a commit.
How it works
How it works
Git snapshots are committed to the local repository. Git creates an opportunity to gather the commits in the local repository, rather than making a change and commit it immediately to the central repository. This has many advantages splitting up a feature into commits, grouping the related commits, and cleaning up local history before committing it to the central repository. This also gives the developers an opportunity to work in an isolated manner.
Common options
Common options
类目 | 类目 |
---|---|
git commit -a | Commits a snapshot of all changes in the working directory. Only modifications to tracked files are included. |
git commit -m “commit message” | Creates a commit with a passed commit message. By default, git commit opens the locally configured text editor causing a commit message to be entered. |
git commit -am “commit message” | Combines the -a and -m options for creating a commit for all the staged changes and taking an inline commit message. |
git commit –amend | Modifies the last commit. Staged changes are added to the previous commit. This command opens the system’s configured text editor and changes the previously specified commit message. |
Saving changes with a commit
Saving changes with a commit
In the following example, we have the w3cdoc.txt file with changed content on the current branch. To commit the staged snapshot of the file, first, you should stage the file with git add command.
git add w3cdoc.txt
Running git add will move w3cdoc.txt file to the Git staging area. Use the git status command to see the output.
git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: w3cdoc.txt
The green output indicates that w3cdoc.txt will be saved with the next commit. The commit is created by executing:
git commit
Running git commit opens a text editor (we can set it via git config) asking for a commit log message and a list of what is being committed:
# Enter the commit message of your changes. Lines that start
# with '#' will be ignored, an empty message breaks off the commit.
# On branch master
# Changes needed to be committed:
# (use "git reset HEAD ..." to unstage)
#
#modified: w3cdoc.txt
The canonical format of Git is used for summarizing the whole commit on the first line in less than 50 characters, then it leaves a blank line, after which gives a detailed explanation of the changes. For example:
Change the message displayed by w3cdoc.txt
- Update the sayHello() function to get the username
- Change the sayGoodbye() function to a more welcoming message
The first line in the commit message is the subject line, the rest is its body.
How to update (amend) a commit
How to update (amend) a commit
The following example opens the configured text editor pre-filled with the already entered commit message. It means you are editing the last commit instead of creating a new one.
git add w3cdoc.txt
git commit --amend
Git commit vs svn commit
Git commit vs svn commit
SVN is a centralized application model while Git is a distributed application model. SVN commit pushes changes from the local client to a centralized repository. In Git the snapshots are committed to the local repository. Git commits can be pushed to arbitrary remote repositories.
Snapshots, not differences
Snapshots, not differences
Git is based on the snapshots whereas SVN tracks differences of a file. svn commit consists of a diff which is compared to the original file added to the repository. Git records the whole content of all the files in every commit. When committing or saving the state of the project, Git takes a snapshot of the current file look and stores a reference to that snapshot. If there is no change in the file, Git doesn’t store it again.