修正したファイルをGitでインデックスに追加してみる
修正したファイルをインデックスに追加してみる。
現在の状態
前回の作成したリポジトリの中身を少し変更して、
現状sample1.txtとsample2.txtの2ファイルがある状態。
$ ls
README.md sample1.txt sample2.txt
$ git status # On branch master nothing to commit (working directory clean)
修正する
ファイルをいくつか修正する。
・sample1.txtを更新
・sample2.txtを削除
・sample3.txtを作成
$ vi sample1.txt $ rm sample2.txt $ touch sample3.txt
現在の状態は下記の通り。
$ ls
README.md sample1.txt sample3.txt
$ git status # On branch master # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: sample1.txt # deleted: sample2.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # sample3.txt no changes added to commit (use "git add" and/or "git commit -a")
インデックスに追加する
git add ファイル名
でインデックスに追加できる。
$ git add sample1.txt $ git add sample3.txt
インデックスに追加されてるのが確認できる。
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: sample1.txt # new file: sample3.txt # # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: sample2.txt #
ただし、削除したファイルをaddでインデックスに反映することはできない。
$ git add sample2.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: sample1.txt # new file: sample3.txt # # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: sample2.txt #
ファイルの削除する
git rm ファイル名
ファルの削除はgit rmでできる。
(ワークツリーにファイルがある場合、ワークツリーからも削除される)
$ git rm sample2.txt rm 'sample2.txt'
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: sample1.txt # deleted: sample2.txt # new file: sample3.txt #
インデックスの追加を取り消す
git reset
でインデックスへの追加を取り消すことができる
$ git reset Unstaged changes after reset: M sample1.txt M sample2.txt
元に戻ってるのが確認できる
$ git status # On branch master # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: sample1.txt # deleted: sample2.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # sample3.txt no changes added to commit (use "git add" and/or "git commit -a")
git reset HEAD ファイル名
で個別に取り消すこともできる。
インデックスにまとめて追加する
git add -u
「-u」を付けて追加すると、変更したファイルと削除したファイルが追加される。
$ git add -u
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: sample1.txt # deleted: sample2.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # sample3.txt
追加したファイルを元に戻す
$ git reset
git add .
次に、「.」を付けて追加すると、変更したファイルと作成したファイルが追加される。
$ git add .
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: sample1.txt # new file: sample3.txt # # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: sample2.txt #
追加したファイルを元に戻す
$ git reset
git add -A
次に、「-A(または--all)」を付けて追加すると、すべてのファイルが追加される。
$ git add -A
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: sample1.txt # deleted: sample2.txt # new file: sample3.txt #