Rodrigo Rosenfeld Rosas

Adding parts of a modified file to git stage

Sat, 13 Aug 2011 15:35:00 +0000

Have you always wanted to add just part of your modified file to the index stage?

Usually, that happens when you're working in a feature or bug and then realizes another issue in the file. It could be another bug, interesting feature, documentation, comment or just code formatting.

If you're like me, you won't include both modifications into a single commit. Then what to do?

What I used to do when I realized this before actually fixing that bug was calling "git stash", fix the bug and "git stash pop". This works well for simple fixes, if you didn't change your database, so that the application will continue to work after "git stash".

But what if you have already fixed the code? You could undo the fix, save the file, add it to index, and then redo the fix. Believe me, I've done that several times.

But I won't do it anymore! Don't worry, I'll keep my commits separate. It's just that I found a better way of doing this: "git add -e" (or "git add -p" and choosing the "e" option). Go try it if you don't know this already. Much easier to try it than to try to explain it! ;) Also "git help add" will explain it better than me. See EDITING PATCHES section.

comments powered byDisqus