tags:

views:

160

answers:

3

I'm using git and been working heavily on my local repository the last week.

I like to make many small commits (like 2 o 3 files) instead of big ones, but this time between time pressure and that the change involved many files, I've got a whole lot of files unstaged.

My question is, what is the best practice in this scenario? Is there any tool that lets me divide the work in small commits?

Note that I do know how to stage files one at a time and make the small commits by hand, but I'm afraid to make a mistake and create a commit that leaves the code in an inconsistent state (like not compiling or working badly).

Thanks!

+2  A: 

You can stage part of a file with git add --patch. This should allow you to commit sets of related changes, without committing other unrelated changes that occur in the same files.

And if you want to test the current HEAD without losing all your uncommitted work, git stash is your friend.

Ben James
Also note: git stash -p
William Pursell
+8  A: 

git add -i should help you divide commits to chunks, then git stash rest of code, check if everything works, git commit, git stash pop and loop.

MBO
The key here is `git stash`; the OP says he already knows how to divide commits, though do note that `-p` (`--patch`) is often more useful than `-i` (`--interactive`).
Jefromi
Jefromi, the OP says he knows how to stage *files on at a time*.
Ben James
I like to use `git add -i` and then select `p (patch)` from the interactive menu... this way you can stage chosen hunks of various files all in one go. Then, double-check with `git diff --cached` before the commit.
grossvogel
A: 

Personally, I find git add -i a bit cumbersome to work with as I find it hard to see what it is actually doing.

However, git gui turns out to be quite good at per-hunk and also per-line staging and unstaging. Occasionally it needs a little support by doing a quick temporary edit before staging and then immediately reversing the edit, but most of the time git gui works really well for me.

ndim