10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

git addで新規ファイルのみ追加する

Posted at

今仕事で行なっているシステム移行でやったこと。
gitのバージョンは1.7.11.1。

$ git --version
git version 1.7.11.1

例えばこういうケース。

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   add1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	add2.txt
#	add3.txt
#	add4.txt
#	add5.txt
#	add6.txt
#	add7.txt
#	add8.txt
#	add9.txt
no changes added to commit (use "git add" and/or "git commit -a")

gitで管理しているファイルを変更するときに、追加する分と更新する分を分けてコミットしたい。

新規ファイルのみ追加するときは、git add-iオプションをつけて対話モードを実行する。

$ git add -i
           staged     unstaged path
  1:    unchanged        +1/-0 add1.txt

*** Commands ***
  1: status	  2: update	  3: revert	  4: add untracked
  5: patch	  6: diff	  7: quit	  8: help

ここで4: add untrackedを選び、対象ファイルを選択する。

What now> a
  1: add2.txt
  2: add3.txt
  3: add4.txt
  4: add5.txt
  5: add6.txt
  6: add7.txt
  7: add8.txt
  8: add9.txt
Add untracked>> *
added 8 paths

*ですべてを選択する。選択したら対話モードを終了する。

*** Commands ***
  1: status	  2: update	  3: revert	  4: add untracked
  5: patch	  6: diff	  7: quit	  8: help
What now> q
Bye.

こうすると、対話モードで選択したファイルのみをステージに追加できる。

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   add2.txt
#	new file:   add3.txt
#	new file:   add4.txt
#	new file:   add5.txt
#	new file:   add6.txt
#	new file:   add7.txt
#	new file:   add8.txt
#	new file:   add9.txt
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   add1.txt
#

ちなみに、更新したいファイルのみを追加するときは

$ git add -u .

git add-uオプションをつけて実行すると

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   add1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	add2.txt
#	add3.txt
#	add4.txt
#	add5.txt
#	add6.txt
#	add7.txt
#	add8.txt
#	add9.txt

更新分のみステージに追加できる。

参考

10
5
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
10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?