Skip to main content

Commits and History

Level: Beginner

ℹ️ What You'll Learn
  • Write good commit messages
  • View commit history
  • Inspect specific commits
  • Amend commits
  • Cherry-pick commits

Commit Messages

Format:

Subject line (50 chars)

Body (optional, wrap at 72 chars):
- Explain WHY not WHAT
- List key changes
- Reference issues/tickets

Footer:
Fixes #123
Relates-to #456

SMS Example:

git commit -m "Add fee payment tracking system

- Create FeePayment model with transaction ID
- Implement FeeService.RecordPayment(studentId, amount)
- Add FeePaymentController with POST endpoint
- Add unit tests for payment validation
- Update database schema with Payments table

Fixes #34
"

Good message = clear changelog.

View History

# Last 5 commits
git log -5

# One line per commit
git log --oneline -10

# With graph (branches)
git log --graph --oneline --all

Output:

* 696027e Add fee payment system
* b53bb8d Add exam results calculation
* 11012bf Fix student list filtering
* c545558 Add student registration form

Inspect Commit

git show 696027e
# Shows: author, date, message, all changes

Diff specific file:

git show 696027e -- StudentFees.cs
# Shows only StudentFees changes in that commit

Compare Branches

git log main..feature/exam-module
# Commits in exam-module not in main

Diff between:

git diff main feature/exam-module
# All changes in exam-module vs main

Amend Last Commit

Forgot to include file:

git add ForgottenFile.cs
git commit --amend
# Updates previous commit (don't use if already pushed)

Change message:

git commit --amend -m "Updated message"

Cherry-pick Commit

Copy specific commit from one branch to another:

git checkout main
git cherry-pick abc123
# Applies abc123 commit to main

SMS Use: Copy critical bugfix from develop → main without full merge.

Revert Commit

Undo specific commit:

git revert 696027e
# Creates new commit that undoes 696027e

Better than git reset (keeps history intact).

Find Commit

Search history:

git log --grep="fee"
# Commits mentioning "fee"

git log --author="Ravi"
# Commits by author Ravi

git log -S "ClassName"
# Commits that changed ClassName variable

Blame (Who changed what)

git blame StudentService.cs
# Shows who last modified each line

6f9a8d1 (TeamSahasra 2024-01-15) public string RollNumber { get; set; }
b53bb8d (TeamSahasra 2024-01-10) public string ClassName { get; set; }

Track changes to specific lines.

SMS Example

# Write good commit message
git commit -m "Implement exam result calculation

- Add ExamResult model with MarksObtained, IsAbsent
- Create ExamResultService for result processing
- Grade calculation: A+ (90+), A (80+), B (70+), C (60+)
- Add comprehensive unit tests
- API endpoint: POST /api/exams/{id}/results

Fixes #45: Exam grading system
"

# View full history
git log --oneline

# Show specific commit
git show 123abc

# Compare feature branch
git log main..feature/results

# Find related commits
git log --grep="grade"

Key Takeaways

  • Commit = checkpoint
  • Message = clear explanation
  • History = audit trail
  • Log = understand what happened
  • Blame = track changes
💡 Commit Pro Tip

Commit small, logical changes. Easier to revert, cherry-pick, understand.

🤖Use AI to Learn Faster

Use ChatGPT, Claude, or Copilot to go deeper on Git Commits. Try these prompts:

  • "What makes a good commit message?"
  • "How do I view the history?"
  • "How do I find who changed something?"
  • "Quiz me on commits"

💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.

nexcoding.in