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
@@ -10,21 +10,11 @@ class ReportCard
@scores.map do |score|
- if score < 60
- elsif score < 70
- elsif score < 80
- elsif score < 90
+ def fetch(score)
+ @score ||= Score.new(score)
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.