How to stage only part of a new file with git?

I love git add –interactive. It is now part of my daily workflow.

The problem seems that it does not work with untracked files. What I want to do is track a new file, but only add part of it, i.e. some parts of this new file are not yet ready to be staged.

For example, with git add -i, I can chose the patch option and even edit individual hunks in order to stage parts of the new code, leaving debug code comments unstaged. I love working this way because it makes it obvious which places of the mega patch I am currently working on still need work.

Unfortunately, I don’t seem to be able to do the same with an untracked file. Either I stage the whole file, or nothing. The workaround I have been using is staging or even committing a new file when it is empty, and then staging individual changes in the usual way. But this solution feels like a dirty hack and when I forget, or change my mind, it creates more troubles than there should be.

So the question is: How to stage only part of a new file, so that this new file gets tracked but leaving the whole or parts of its content unstaged?

5 Answers
5

Leave a Comment