Gitに入門する -その3-

blog.takanabe.tokyo

ステップ2の「Gitを初めからていねいに」をやる。

「ひとりでつかう - どんどんコミット」より。

①ファイルをまとめてgit addする

piyo.txtとhoge.txtを新規で作業ディレクトリに作成する。
その状態で、

$ git add . 

するとまとめてステージングエリアに上げてくれる。

$ git status
On branch master
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)

       new file:   fuga.txt
       new file:   piyo.txt

さらにcommitする。

$ git commit
[master 42201c7] fuga.txtとpiyo.txtを追加
2 files changed, 2 insertions(+)
create mode 100644 fuga.txt
create mode 100644 piyo.txt

ばんざーいまとめてcommitされましたヽ(´ー`)ノ

②ファイル削除する

fuga.txtを作業ディレクトリから削除しておく。
その状態でgit status

$ git status
On branch master
Changes not staged for commit:
 (use "git add/rm <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)

       deleted:    fuga.txt

no changes added to commit (use "git add" and/or "git commit -a")

git rmする

$ git rm fuga.txt
rm 'fuga.txt'

git statusで確認

$ git status
On branch master
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)

       deleted:    fuga.txt

コミットする

$ git commit
[master 678dd05] fuga.txtを削除
1 file changed, 1 deletion(-)
delete mode 100644 fuga.txt

ばんざーいファイルの削除完了ヽ(´ー`)ノ

ちなみに作業ディレクトリのファイルを手動で削除して、
git rmして、git commitしてがめんどくせー場合は、
しょっぱなからgit rmしてみましょう。

$ git rm piyo.txt
rm 'piyo.txt'

作業ディレクトリを見るとpiyo.txtがなくなってる。

$ git status
On branch master
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)

       deleted:    piyo.txt


$ git commit
[master ea41e3c] piyo.txtも削除
1 file changed, 1 deletion(-)
delete mode 100644 piyo.txt

③消えたファイルを元に戻す

ファイルを間違えて削除してしまった場合、、、
fuga.txtだけでいいのにpiyo.txtも消しちまったよの場合。。。

ひとまずpiyo.txtも作業ディレクトリから削除してgit statusしてみる。

$ git status
On branch master
Changes not staged for commit:
 (use "git add/rm <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)

       deleted:    piyo.txt

no changes added to commit (use "git add" and/or "git commit -a")

その後git checkoutする。

$ git checkout -- piyo.txt

するとあら不思議piyo.txtが復活しているではありませんか!?
Gitちゃんいい子ね~(・∀・)