見出し画像

【Github】ファイルを指定してコミットする方法

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


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


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

$ 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/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/dto/code/Range.java

pushする

$ git push origin ブランチ名


この記事が気に入ったらサポートをしてみませんか?