R.A. Epigonos et al.

[git] 一部のファイルだけをcommit対象にするにはgit addの後にgit commit

仕事中に別の案件が割り込んできて、別の案件用に編集したファイルだけをコミットしたい。もしくは、完全に独立した2つの作業をしたので、それぞれの作業ごとにcommitログを書きたい。こんな場合。

git commit -aでコミットされるファイルを確認するにはgit status。この状態でgit commit -aすると、po/ja.poとsrc/gui/curses/gui-curses-chat.cの両方の変更に対してコミットログを書くべき。だけど、src/gui/curses/gui-curses-chat.cに対する仕事はまだ完了していないので、仕事完了したpo/ja.poの変更内容だけをコミットしたい。

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   po/ja.po
#       modified:   src/gui/curses/gui-curses-chat.c
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       ************
#       *************
#       ************
#       *********
#       *************************************
#       ****
no changes added to commit (use "git add" and/or "git commit -a")

git addでコミットするファイルを指定。この状態でgit statusを見ると、src/gui/curses/gui-curses-chat.cはコミットから外れることがわかる。

$ git add po/ja.po
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   po/ja.po
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   src/gui/curses/gui-curses-chat.c
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       ************
#       *************
#       ************
#       *********
#       *************************************
#       ****

実際にコミットする。このとき、git commit -aではなく、git commitであることに注意。

$ git commit
translation: update
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   po/ja.po
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   src/gui/curses/gui-curses-chat.c
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       ************
#       *************
#       ************
#       *********
#       *************************************
#       ****

リファレンス

  1. Gitを使いこなすための20のコマンド - SourceForge.JP Magazine : オープンソースの話題満載
  2. git-add(1)

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2010-04-24T21:43:54+09:00
  2. Modified: 2010-04-24T21:43:54+09:00
  3. Generated: 2023-08-27T23:09:15+09:00