git add --patch instead of
git add whenever you can.
--patch switch lets you choose interactively the hunks you want to add for the next commit.
This means than you don’t get to choose between adding a file or not.
Your decisions can be more fine grained.
The thing is, while you code, you jump around files, edit stuff here and there, fix multiple problems at the same time, add a comment to existing code and so on.
However, you don’t want to put all of this stuff in one commit.
You should try to make every commit contain only changes belonging together semantically.
git add --patch helps you.
Going over every little change one by one allows to pick only those that should end up in the same commit.
This can be done iteratively until all changes end up in one or more commits.
$ git add --patch diff --git a/_drafts/git-add-patch.md b/_drafts/git-add-patch.md index 4b4772c..2af25ca 100644 --- a/_drafts/git-add-patch.md +++ b/_drafts/git-add-patch.md @@ -22,3 +22,10 @@ Git gives you some additional help while adding hunks one by one interactively. You can skip a hunk, edit it, skip the whole file, search for similar hunks etc. Just run ``git add --patch`` and press ``?`` on the any hunk to get an overview of the options. + +And of course, you'll have your efficient aliases set up: +alias ga='git add' +alias gap=-'git add --patch' + Stage this hunk [y,n,q,a,d,/,e,?]?
Git gives you some additional help while adding hunks one by one interactively.
You can skip a hunk, edit it, skip the whole file, search for similar hunks etc.
git add --patch and press
? on the any hunk to get an overview of the options.
And of course, you’ll have your efficient aliases set up:
It’s usually wise to run
git diff first to have an overview of all changes to be committed before starting to stage them individually.