How to use the command 'git commit' (with examples)
The ‘git commit’ command is used to create a new commit, which is a snapshot of the project’s current state. It is an essential command in Git that allows developers to save their changes and track the project history.
Use case 1: Commit staged files to the repository with a message
Code:
git commit --message "message"
Motivation: A developer has made changes to their project and needs to save those changes in the repository with a descriptive message.
Explanation:
git commit
is the base command.--message
or-m
is an option used to specify the commit message."message"
is the actual commit message enclosed in quotes.
Example output:
[master abcdefg] message
1 file changed, 1 insertion(+)
Use case 2: Commit staged files with a message read from a file
Code:
git commit --file path/to/commit_message_file
Motivation: The commit message is lengthy or includes multiple lines, so it is more convenient to keep it in a separate file.
Explanation:
git commit
is the base command.--file
or-F
is an option used to specify the path to the file containing the commit message.
Example output:
[master abcdefg] Add new feature
3 files changed, 100 insertions(+), 10 deletions(-)
Use case 3: Auto stage all modified and deleted files and commit with a message
Code:
git commit --all --message "message"
Motivation: The developer wants to stage all the modified and deleted files automatically and commit them with a descriptive message.
Explanation:
git commit
is the base command.--all
or-a
is an option used to automatically stage all modified and deleted files.--message
or-m
is an option used to specify the commit message.
Example output:
[master abcdefg] message
3 files changed, 100 insertions(+), 10 deletions(-)
Use case 4: Commit staged files and sign them with the specified GPG key
Code:
git commit --gpg-sign key_id --message "message"
Motivation: The developer wants to sign their commits to ensure their authenticity and integrity using a specific GPG key.
Explanation:
git commit
is the base command.--gpg-sign
is an option used to specify the GPG key to sign the commit.key_id
is the ID of the GPG key to be used for signing.--message
or-m
is an option used to specify the commit message.
Example output:
[master abcdefg] message
GPG signature verified
3 files changed, 100 insertions(+), 10 deletions(-)
Use case 5: Update the last commit by adding the currently staged changes
Code:
git commit --amend
Motivation: The developer realizes that they forgot to include some changes in the last commit and wants to add them without creating a new commit.
Explanation:
git commit
is the base command.--amend
is an option used to update the last commit by adding the currently staged changes.
Example output:
[master abcdefg] Add more changes
2 files changed, 50 insertions(+)
Use case 6: Commit only specific (already staged) files
Code:
git commit path/to/file1 path/to/file2
Motivation: The developer wants to commit only specific files rather than all the staged changes in the repository.
Explanation:
git commit
is the base command.path/to/file1 path/to/file2
are the paths of the files to be committed.
Example output:
[master abcdefg] Update file1 and file2
2 files changed, 20 insertions(+), 5 deletions(-)
Use case 7: Create a commit, even if there are no staged files
Code:
git commit --message "message" --allow-empty
Motivation: The developer wants to create a commit with a descriptive message but without any staged changes.
Explanation:
git commit
is the base command.--message
or-m
is an option used to specify the commit message.--allow-empty
is an option used to allow creating an empty commit.
Example output:
[master abcdefg] Empty commit
Conclusion:
The ‘git commit’ command is a powerful tool for saving changes to a repository. With various options, developers can provide meaningful commit messages, sign their commits, update the last commit, commit specific files, and even create empty commits. Understanding and utilizing these different use cases will help developers effectively manage their project’s history and collaborate with others.