git commit with care

I use to stage my changes with `git add .` that pretty much grabs all changes from the current directory and pushes them up on Github, I’ve gotten into trouble by committing things that weren’t suppose to be included.

For example, I did some refactoring to the ReportCard class, It’ll open up something like:

✗ git add lib/report_card.rb -p

The `-p` is important, it’ll give you chance to approve/reject some changes you’ve made to the file you’re adding.

diff — git a/lib/report_card.rb b/lib/report_card.rb
index f80b08d..82d06d5 100644
— — a/lib/report_card.rb
+++ b/lib/report_card.rb
@@ -10,21 +10,11 @@ class ReportCard
def grade_scores
@scores.map do |score|
- grade_score(score)
+ fetch(score).rating
end
end
def grade_score(score)
- if score < 60
- ‘F’
- elsif score < 70
- ‘D’
- elsif score < 80
- ‘C’
- elsif score < 90
- ‘B’
- else
- ‘A’
- end
+ def fetch(score)
+ @score ||= Score.new(score)
end
end
Stage this hunk [y,n,q,a,d,/,s,e,?]? y

Now you can check your work, line by line, what was removed(-)/added(+), this way you’re 100% sure what’s being pushed off to git. If you see something you’re not sure of, you can enter `n` when asked Stage this hunk? and it’ll remain un-staged.

It may seem like adding this to your daily workflow is minor, but it can make a huge difference in the quality of code you’re committing.

Show your support

Clapping shows how much you appreciated Anonyo Hossain’s story.