In Git, the git commit
command is used to record the changes made to the repository. It’s a crucial step in the Git workflow as it captures a snapshot of the repository at a specific point in time. This ensures that all changes are saved and can be referenced or reverted to if needed.
Committing Changes in Git: Best Practices and Tips
Here are several examples illustrating how to use git commit
effectively:
Example 1: Committing with a Message
To commit all staged changes with a descriptive message:
git commit -m "Added new feature: authentication module"
Output: [main abc1234] Added new feature: authentication module
1 file changed, 5 insertions(+)
To verify: Use git log
to see the commit history and ensure the new commit is listed.
Example 2: Amending the Last Commit
If you need to add more changes to the last commit:
git commit --amend
No output displayed for amending a commit. Verify with git log
and ensure the previous commit is updated.
Example 3: Committing Specific Files
To commit only specific files:
git commit file1.txt file2.txt -m "Updated files"
Output: [main def5678] Updated files
2 files changed, 10 insertions(+)
To verify: Check the commit log for the specific files’ changes.
Example 4: Committing All Changes (Including Untracked)
To commit all changes, including untracked files:
git commit -am "Added and modified files"
Output: [main hij9012] Added and modified files
3 files changed, 20 insertions(+)
To verify: Ensure all changes, including untracked files, are committed using git status
.
Example 5: Committing Interactively
To interactively choose which changes to commit:
git commit -p
No output shown directly. Use git status
to verify the interactive commit process.
Example 6: Committing with Signed-off-by
To add a Signed-off-by line to the commit message:
git commit -s -m "Fix: issue #123"
Output: [main klm3456] Fix: issue #123
1 file changed, 1 insertion(+)
Verify the signed-off line using git log
or inspect the commit message.
Example 7: Committing and Skipping Staging Area
To commit changes without staging explicitly:
git commit -a -m "Commit without staging"
Output: [main nop7890] Commit without staging
2 files changed, 8 insertions(+)
Verification involves checking that changes were committed directly without git add
.
Example 8: Committing with a Specific Date
To commit with a custom date and time:
git commit --date="2023-06-21 10:30" -m "Commit at a specific time"
Output: [main qrs1234] Commit at a specific time
1 file changed, 2 insertions(+)
Verify the commit’s date and time using git log
and inspecting the commit details.
Example 9: Committing and Squashing
To squash multiple commits into one:
git commit --squash abc1234
No output displayed directly. Verify the squashed commit using git log
.
Example 10: Committing with Committer Details
To specify the committer’s name and email:
git commit --author="John Doe <johndoe@example.com>" -m "Commit with author details"
Output: [main stu5678] Commit with author details
1 file changed, 3 insertions(+)
Verify the author details in the commit using git log
.
Discussion about this post