LoginSignup
13
16

More than 3 years have passed since last update.

Gitでファイルを指定してコミットする方法

Posted at
  • 環境
    • git version 1.8.3.1
    • CentOS Linux release 7.6.1810 (Core)

参考 : [git] 一部のファイルだけをcommit対象にするにはgit addの後にgit commit - 綾小路龍之介の素人思考

コミットするブランチを確認する

既存ブランチの場合 : 現在のブランチを確認する

うっかり違うブランチにコミットしないように現在のブランチを確認して、コミットしたいブランチでなかったら切り替える。

$ git branch --contains=HEAD
* target-branch

新規ブランチの場合 : ブランチを作る

$ git checkout -b target-branch
M   my-project/.classpath
A   my-project/src/main/java/ponsuke/dto/code/Range.java
M   my-project/src/main/webapp/resources/js/1_03/Update.js
M   my-project/src/main/webapp/user/sectionEdit.xhtml
M   my-project/src/main/webapp/user/sectionView.xhtml
Switched to a new branch 'target-branch'

変更されているファイルを確認する

変更したり新規作成したファイルを確認する方法
$ git status

Changes not staged for commit のところに変更されたファイル、
Untracked files のところに新規作成(まだGitの管理外)したファイル
が表示される。

$ git status
# On branch target-branch
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# 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:   my-project/.classpath
#   modified:   my-project/src/main/webapp/resources/js/1_03/Update.js
#   modified:   my-project/src/main/webapp/user/sectionEdit.xhtml
#   modified:   my-project/src/main/webapp/user/sectionView.xhtml
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
#   build/
#   my-project/src/main/java/ponsuke/dto/code/Range.java
#   my-project/src/main/resources/jdbc.properties
#   my-project/target/

変更内容を確認する

ローカルでの変更内容を確認する方法
$ git diff HEAD {ファイルパス}
$ git diff HEAD my-project/src/main/webapp/resources/js/1_03/Update.js
$ git diff HEAD my-project/src/main/webapp/user/sectionEdit.xhtml
$ git diff HEAD my-project/src/main/webapp/user/sectionView.xhtml
# 新規作成したファイルはcatで見る
$ cat HEAD my-project/src/main/java/ponsuke/dto/code/Range.java

コミットしたいファイルを指定する

任意のファイルをコミット対象にする方法
$ git add {ファイルパス}

変更内容を確認して意図通りになっていたらコミット対象にする。

$ git add my-project/src/main/webapp/resources/js/1_03/Update.js
$ git add my-project/src/main/webapp/user/sectionEdit.xhtml
$ git add my-project/src/main/webapp/user/sectionView.xhtml
$ git add my-project/src/main/java/ponsuke/dto/code/Range.java

# コミットされるファイルを確認する
$ git status
# On branch target-branch
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   my-project/src/main/java/ponsuke/dto/code/Range.java
#   modified:   my-project/src/main/webapp/resources/js/1_03/Update.js
#   modified:   my-project/src/main/webapp/user/sectionEdit.xhtml
#   modified:   my-project/src/main/webapp/user/sectionView.xhtml
#
# 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:   my-project/.classpath
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
#   build/
#   my-project/target/

コミットしてプッシュする

# Commitする
$ git commit -m "Commitコメント"
[target-branch 6cfc522] Commitコメント
 7 files changed, 376 insertions(+)
 create mode 100644 my-project/src/main/java/ponsuke/dto/code/Range.java

# Pushする
$ git push
Counting objects: 72, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (32/32), done.
Writing objects: 100% (41/41), 5.48 KiB | 0 bytes/s, done.
Total 41 (delta 22), reused 0 (delta 0)
remote: Resolving deltas: 100% (22/22), completed with 22 local objects.
To https://github.com/ponsuke/my-project.git
   6cfc522..72b5a16  target-branch -> target-branch
13
16
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
13
16