Hacker News new | ask | show | jobs
by sham1 645 days ago
`git add -p` is such a nice utility. Sometimes I do wish that it could also be used for unstages files, so that if I'm introducing a new file, I could still break its contents up into multiple commits.

Of course, the workaround there is that one adds the initial file into the staging area and then `git add -p` the subsequent changes. It could just be a bit more convenient on that front, is all.

2 comments

It can, you just gotta do a magic incantation first.

  git add -N file
  git add -p file
The first command signals to git that you intend to add the file. That makes its entire content show up in the patch editor.
TIL! I seem to have just missed the `-N`/`--intend-to-add` while perusing through the `git-add(1)` manual.

Heh, it[0] even notes a similar use case:

> `-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`.

[0]: <https://git-scm.com/docs/git-add#Documentation/git-add.txt--...>

Alternately:

    git add file
    git reset -p file