LoginSignup
4
4

More than 5 years have passed since last update.

'ABC'を含むファイルだけをさっと'git add'

Posted at

入力が面倒

この状態で

$ git status
# On branch master
# Changes not staged for commit:
#
#       modified:   sub/testABC.txt
#       modified:   sub/testDEF.txt
#       modified:   testABC.txt
#       modified:   testDEF.txt
#

ABCを含む2ファイルのみ追加したい。

  • sub/testABC.txt
  • testABC.txt

git add *ABC*とした場合はカレントディレクトリのみが対象となる。
sub/testABC.txtは追加されない (場合によっては便利)

$ git add *ABC*
$ git status
# On branch master
# Changes to be committed:
#
#       modified:   testABC.txt
#
# Changes not staged for commit:
#
#       modified:   sub/testABC.txt
#       modified:   sub/testDEF.txt
#       modified:   testDEF.txt
#

正解はgit add "*ABC*"なのだが

  • そもそも*で囲むのがめんどくさい
  • "'で囲むのもめんどくさい

aliasで解決

以下のようにaliasを追加する。

[alias]
    ad = "!f() { git add \"*${1}*\" ${@:2};};f"

ファイルパスにABCを含むファイルをさっと追加できるようになる。

$ git ad ABC
$ git st
# On branch master
# Changes to be committed:
#
#       modified:   sub/testABC.txt
#       modified:   testABC.txt
#
# Changes not staged for commit:
#
#       modified:   sub/testDEF.txt
#       modified:   testDEF.txt
#

後ろならオプション指定も使える。

$ git ad ABC -p

ついでにresetにも

[alias]
    re = "!f() { git reset HEAD \"*${1}*\" ${@:2};};f"

git re ABCでステージングされたファイルをさっと戻すのに使用している。

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