LoginSignup
1
0

More than 3 years have passed since last update.

[Git] Untrackedなファイルをadd --patchする流れがとても面倒なのでワンラインにしてみた

Last updated at Posted at 2021-04-10

untrackedファイルをadd --patchするには、
git add --intent-to-addを対象ファイルに対して行わなければならず、

尚且つgit add --intent-to-addを実行した対象ファイルを
一つ一つgit resetしなければgit commitができなくなるというもので、
本来であれば以下のような流れが必要になる。

  1. git add -N $UNTRACKED_FILES
  2. git add -p
  3. git reset $UNTRACKED_FILES
  4. git commit

これはファイル数が多くなれば、とても面倒。

ということで以下のようなワンラインを作成してみた。

$ git config --global alias.add-patch-with-untracked
! git status -s | cut -c4- | xargs git add -N && git add -p && git status -s | grep '^ ' | cut -c4- | xargs git reset

上記のような面倒な手順が、

  1. git add-patch-with-untracked
  2. git commit

で完結するようになる。

上記を追加するには、直接~/.gitconfigをいじってしまうのが早い

$ git config --global --edit
~/.gitconfig
[alias]
    add-patch-with-untracked = ! git status -s | cut -c4- | xargs git add -N && git add -p && git status -s | grep '^ ' | cut -c4- | xargs git reset

以上。

1
0
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
1
0