Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Whoa, all that update-index and hash-object business seems overly complicated. How about this instead:

git add -N new_file
git add -i  # or 'git add -p' if you prefer

From git help add:

-N, --intent-to-add
    Record only the fact that the path will be added later.  An entry
    for the path is placed in the index with no content.  This is useful
    for, among other things, showing the unstaged content of such files
    with git diff and committing them with git commit -a.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...