言語
サーバ関連
ツール
API
読み物
その他
[AD]
備忘録用のWikiです。主にWeb技術方面や、iOSやAndroidなどスマホアプリ開発周りをメモしています(する予定ですw)。
= 2016/04/10:デザインを変更しました。 = 2016/03/27:2001年ごろに執筆していた「[[webp:|Webプログラミング]]」を掲載しました。 = 2016/03/27:日本語が入ったURLを、英数字の文字列に変更しました。旧URLからはリダイレクトがかかります。 = 2016/03/22:さぼっていた更新を再開しました = 2014/08/??:旧サイト(katsubemakito.net/cgiperl/)からの引越しを行いました
Gitはその仕様上、空のディレクトリを登録してくれません。
$ mkdir empty $ ls empty $ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean
git add
しても取り合ってくれません。
$ git add empty $ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean
そこで空のディレクトリを登録するためには、何か適当なファイルを設置する必要があるのです。Git界隈では慣習として .gitkeep
という名前の空ファイルを用意することが多いようですが、ファイル名は何でも良いです。
$ touch empty/.gitkeep
$ git status On branch master Your branch is up-to-date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) empty/ nothing added to commit but untracked files present (use "git add" to track)
ファイルを追加した時点で空ディレクトリではないやろというツッコミは、大人はしちゃダメですw
$ git mv foo.txt bar.txt
git mv [変更前のファイルのパス] [変更後のファイル]
でファイル名を変更できます。
$ git mv hello.txt imfine.txt
git status
で先ほどの変更した箇所が表示されれば成功です。
$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: hello.txt -> imfine.txt
別のディレクトリに移動させつつファイル名を変更することも可能です。
$ git mv hello.txt move/finetoo.txt $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: hello.txt -> move/finetoo.txt
忘れずにcommitしておきましょう。
別のファイルとして扱われるため、それまで記録し続けてきたログがその時点で途切れてしまいます。詳しくはこちらを参照ください。
$ git mv *.txt work/
git mv [ファイルのパス] [移動後のファイルのパス]
でファイルを移動できます。
$ git mv hello.txt move/
git status
で先ほどの変更した箇所が表示されれば成功です。
$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: hello.txt -> move/hello.txt
忘れずにcommitしておきましょう。
Linux(UNIX)のmv
と同様に、git mv [ファイル1] [ファイル2]…[ファイルn] [移動先]
のように一番最後に移動後のディレクトリを指定すれば複数ファイルをまとめて移動することができます。
$ git mv *.txt move/
$ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: Hi.txt -> move/Hi.txt renamed: hello.txt -> move/hello.txt renamed: yahoo.txt -> move/yahoo.txt
別のファイルとして扱われるため、それまで記録し続けてきたログがその時点で途切れてしまいます。
$ mv foobar.txt foo.txt
$ git status HEAD detached at 4f00e3b 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: foobar.txt Untracked files: (use "git add <file>..." to include in what will be committed) foo.txt no changes added to commit (use "git add" and/or "git commit -a")
過去の経緯などを調査することが非常に面倒になりますので、必ずgitを通しましょう。
$ git rm foobar.txt
git rm [ファイル名|ディレクトリ名]
でファイルを削除します。
$ git rm foobar.txt rm 'foobar.txt'
git status
でdeletedと表示されていれば成功です。
$ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: foobar.txt
通常、git rm
すると指定したファイルも即座に消えてしまいます。インデックスからは削除するけど、ファイル自体は残したいという場合には–cached
オプションを指定します。
$ git rm --cached foobar.txt rm 'foobar.txt' $ ls foobar.txt hello.txt
git status
するとインデックスからは削除されますが、ファイルは残っているのでUntracked fileとして表示されます。
$ git status HEAD detached at 4f00e3b Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: foobar.txt Untracked files: (use "git add <file>..." to include in what will be committed) foobar.txt
実際にやってみましょう。
$ ls foobar.txt hello.txt $ rm foobar.txt
git status
で見てみると削除されたことになっています。
$ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) 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: foobar.txt
そのままcommitも出来てしまいました。git log
git show
でログを見ても正常に削除されているのがわかります。
$ git commit -am "delete" [master 43a75c4] delete 1 file changed, 1 deletion(-) delete mode 100644 foobar.txt
git rm
で明示しないと何が問題かというと、git自身はあなたがどのようなコマンドを叩いたのかすべて把握しているわけではありません。例えば以下のように名前を変更した後にgit status
すると…
$ mv foobar.txt foo.txt
foobar.txtは削除され、foo.txtが新たに登場したと認識されてしまいます。実際には違いますよね?このような事故を防ぐために一度Gitに登録したファイルを操作する際には必ずGitのコマンドを用いることが求められます。
$ git status HEAD detached at 4f00e3b 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: foobar.txt Untracked files: (use "git add <file>..." to include in what will be committed) foo.txt no changes added to commit (use "git add" and/or "git commit -a")
$ git add foo.txt
git add [ファイルのパス]
でインデックスにファイルを追加できます。
$ git status On branch master Your branch is up-to-date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) foobar.txt $ git add foobar.txt
複数ファイルをまとめて追加することもできます。
$ git add foo.txt bar.txt ../path/to/hoge.txt
もちろんお使いのbashやzshなどシェルの機能を使っても大丈夫です。
$ git add *.html
ただ、ここで問題になるのは追加したいファイルが100個あり、ワイルドカードなどで指定しずらい場合、100回指定をしなければならないのかというと、そんなことはなく-A
を指定すると問答無用でまとめて登録してくれます。
$ git add -A