Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
git commit with message
📖 Scenario: You are working on a small project and have made changes to a file. Now, you want to save these changes in your Git repository with a clear message explaining what you did.
🎯 Goal: Learn how to create a Git commit with a descriptive message to save your changes in the repository.
📋 What You'll Learn
Create a new file called example.txt with some text
Add the file to the Git staging area
Commit the changes with a message Initial commit with example.txt
Display the commit log to verify the commit
💡 Why This Matters
🌍 Real World
Saving changes with clear messages helps teams understand what was done and why, making collaboration easier.
💼 Career
Knowing how to commit with messages is a basic but essential skill for any developer or DevOps engineer working with version control.
Progress0 / 4 steps
1
Create a new file called example.txt with text
Create a file named example.txt and add the text Hello, Git! inside it.
Git
Hint
You can use the echo command to write text into a file.
2
Add example.txt to the Git staging area
Use the Git command to add the file example.txt to the staging area.
Git
Hint
Use git add example.txt to stage the file.
3
Commit the changes with a message
Create a Git commit with the message Initial commit with example.txt.
Git
Hint
Use git commit -m "your message" to commit with a message.
4
Display the commit log to verify
Use the Git command to show the commit log and verify your commit message appears.
Git
Hint
Use git log --oneline to see a short commit history.
Practice
(1/5)
1. What does the command git commit -m "Update README" do?
easy
A. Saves your changes with the message 'Update README'.
B. Deletes the README file from the repository.
C. Shows the commit history with the message 'Update README'.
D. Creates a new branch named 'Update README'.
Solution
Step 1: Understand the git commit command
The git commit command saves changes to the local repository.
Step 2: Understand the -m option
The -m option adds a message describing the changes.
Final Answer:
Saves your changes with the message 'Update README'. -> Option A
Quick Check:
git commit -m "message" saves changes with message [OK]
Hint: Remember: -m adds your commit message directly [OK]
Common Mistakes:
Thinking commit deletes files
Confusing commit with branch creation
Assuming commit shows history
2. Which of the following is the correct syntax to commit changes with a message in git?
easy
A. git commit -message "Fix bug"
B. git commit --msg "Fix bug"
C. git commit -m "Fix bug"
D. git commit message "Fix bug"
Solution
Step 1: Recall the correct flag for commit message
The correct flag to add a message is -m.
Step 2: Check each option's syntax
Only git commit -m "Fix bug" uses the correct flag and syntax.
Final Answer:
git commit -m "Fix bug" -> Option C
Quick Check:
Use -m for commit message [OK]
Hint: Use -m followed by quotes for commit messages [OK]
Common Mistakes:
Using -message instead of -m
Omitting quotes around the message
Using --msg which is invalid
3. What will be the output of the following commands?
git add file.txt
git commit -m "Add file.txt"
medium
A. Error: No files added to commit.
B. A new branch named 'Add file.txt' is created.
C. file.txt is deleted from the repository.
D. Changes in file.txt are saved with message 'Add file.txt'.
Solution
Step 1: Understand git add
The git add file.txt command stages the file for commit.
Step 2: Understand git commit with message
The git commit -m "Add file.txt" saves the staged changes with the message.
Final Answer:
Changes in file.txt are saved with message 'Add file.txt'. -> Option D
Quick Check:
git add + git commit -m saves changes [OK]
Hint: Add files before commit to save changes [OK]
Common Mistakes:
Committing without adding files first
Expecting commit to delete files
Confusing commit with branch creation
4. You run git commit -m Fix typo but get an error. What is the problem?
medium
A. The commit message must be in quotes.
B. The -m flag is missing.
C. You need to add files before committing.
D. The message 'Fix typo' is too short.
Solution
Step 1: Check the commit message syntax
Commit messages with spaces must be enclosed in quotes.
Step 2: Identify the error cause
Without quotes, git treats 'Fix' as the message and 'typo' as an invalid argument.