LoginSignup
11
10

More than 5 years have passed since last update.

Gitで新規(Untracked)なファイルをadd patch(-p)する方法

Posted at

表題の通りのことがしたい。っておもったんだが。

はじめに

普通にやると以下のようになる。

$ git add -p someNewFile.txt
No changes.

解決方法

事前に git add -N しておくと良いらしい。

$ git add -N soneNewFile.txt
$ git add -p someNewFile.txt

ちなみに、1行に2つのオプションを繋げて指定してもうまくいかない。参考にした以下のStackOverflowの回答にあるように、事前にまとめてgit add -N .しておいた方が使い勝手はいいかもしれない。

git add -Nとは

git-addのmanより引用。(読みやすいように多少整形)

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

つまり、gitの管理上にあるということにしておくオプションということみたい。ステージングの手前。
"Untracked files" から "Changes not staged for commit" になって、"new file"として扱われる。なので、diffなどをすることができるようになる。

元に戻したいときは git resetすれば元に戻る。

参考

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10