ゆるおたノート

Tomorrow is another day.

【Git勉強中】ステージに上げたり下げたり。

恐らくcommitと合わせてかなりお世話になるであろうコマンド…

はじめに

基本の流れ

Git自体はじめての方は、まずはこちらからどうぞ。

【Git勉強中】操作に慣れてきたので、流れを整理してみました。 - ゆるおたノート

凡例

記号・用語 意味 説明
$ コマンドライン」で使えるコマンド 通常、Gitを始めとする「シェルスクリプト」系の説明では、コマンドの文頭に$をつけて表現されることが多いようです。
当記事でもそれに倣って記載しています。

実際にコマンドを入力するときは、git~以下をコピペしてください。
# コメント 補足事項など。
(対話モードでは使えません、、、)
<> 編集点(という表現で良いのかな…?) 任意の値(名前、識別子など)を入力してください。
(コミットの)識別子 コミットごとに付与される半角英数字7桁の番号。 SHA-1というハッシュ値だそうです。
当記事では<コミット>と表記します。
ディレクト ファイルの入れ物、つまりフォルダのこと。 当記事では、(特に言及がない限りは)絶対パス相対パスどちらの指定でも可です。

▼コミット予定に追加(=ステージング)する

【A】管理対象にあるファイルをすべて追加

$ git add -A
$ git add --all

【B】ファイル名やディレクトリを指定して追加

$ git add <ファイル名>
$ git add <ディレクトリ>

【C】現在のディレクトリの変更をすべて追加

.(ピリオド1つ)を使って、相対パスでカレントディレクトリを表現します。

$ git add .

ステージから下ろす

【A】インデックスから除外

該当のディレクトリ内でファイルを削除した場合など。

$ git rm -r --cached <ファイル名>
$ git rm -r --cached <ディレクトリ>
分類 用語 説明
コマンド rm removeの略
オプション -r recursive(再帰的、帰納的 / 反復的)の略(?)
<2019/12/20追記>
--cached
ファイル自体は残して、追跡をやめる。
※今後も記録が不要であれば、.gitignoreにも追加しておく。

ステージされたファイルを確認

【A】ステージに不足が無いか確認

$ git status
[例]何も上げていない場合
# 入力
$ git status
 
# 結果
On branch master
Your branch is up to date with 'origin/master'.
 
Last commands done (4 commands done):
   pick 2757f4b fix directory hierarchy.
   pick 467d0e2 fix directory hierarchy.
  (see more in file .git/rebase-merge/done)
No commands remaining.
You are currently editing a commit while rebasing branch 'master' on 'f71eb8a'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)
 
nothing to commit, working tree clean

コミット予定ナシ、作業ツリーも空です。

[例].gitignoreREADME.txtを修正して、.gitignoreはステージ済みの場合
# 入力
$ git status
 
# 結果
On branch master
Your branch is up to date with 'origin/master'.
 
Last commands done (4 commands done):
   pick 2757f4b fix directory hierarchy.
   pick 467d0e2 fix directory hierarchy.
  (see more in file .git/rebase-merge/done)
No commands remaining.
You are currently editing a commit while rebasing branch 'master' on 'f71eb8a'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)
 
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
 
        modified:   .gitignore
 
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:   README.md

コミット予定の変更履歴:
(ステージから下ろす場合はgit reset HEAD <file>...してください)

  変更あり:.gitignore

ステージされていない変更履歴:
(コミットする場合は、git add <file>...してください。)
(作業中ディレクトリの変更をコミットから除外する場合は、git checkout -- <file>...してください)

  変更あり:README.md

【B】変更をステージしつつ、ステージの一覧を表示

$ git add -v .
$ git add --verbose .
[例]何も上げていない場合
# 入力
$ git add -v .
 
# 結果
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?

ステージには何も指定されてません。コマンド間違えてませんか?

[例].gitignoreREADME.txtを修正して、.gitignoreはステージ済みの場合
# 入力
$ git add -v .
 
# 結果
add 'README.md'

'README.md'をステージに追加済み

先にステージしているファイルは出力してもらえないみたいです。
それならgit statusを使おうかな、結果は長くなっちゃうけど。

このシリーズについて

Gitのコマンドについて、勉強しつつ記事にまとめています。

誤り、分かりづらい等ありましたら、ぜひコメント欄Twitterお問い合わせフォーム等でご教示ください!

次回

【Git勉強中】ステージした変更を記録する - ゆるおたノート

連載目次

  1. 【Git勉強中】操作に慣れてきたので、流れを整理してみました。 - ゆるおたノート
  2. 【Git勉強中】ブランチの移動を使い分けたい - ゆるおたノート
  3. 当記事【Git勉強中】ステージに上げたり下げたり。 - ゆるおたノート
  4. 【Git勉強中】ステージした変更を記録する - ゆるおたノート
  5. 【Git勉強中】あっ!そのコミット訂正させてください! - ゆるおたノート
  6. 【Git勉強中】自分のコミット履歴を提出する - ゆるおたノート
  7. 【Git勉強中】リポジトリの作成と接続 - ゆるおたノート

参照

Git - git-add Documentation

git add コマンドの使い方と、主要オプション | WWWクリエイターズ

recursiveの意味・使い方・読み方 | Weblio英和辞書