とあるエンジニアの闇歴史帳

コピペで使える便利Wiki ※現在構築中です。

ユーザ用ツール


サイドバー

言語

サーバ関連

ツール

API

読み物

その他

start

TOP

このサイトは?

備忘録用の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] 空のディレクトリを追加する

実行例

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ポケットリファレンス
岡本 隆史 武田 健太郎 相良 幸範
技術評論社
売り上げランキング: 103,005

2016/04/26 19:03 · katsubemakito

[Git]ファイルを名を変更する(git mv)

コマンド

$ 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を通さないで名前を変更するとどうなる?

別のファイルとして扱われるため、それまで記録し続けてきたログがその時点で途切れてしまいます。詳しくはこちらを参照ください。

参考

関連書籍

Gitポケットリファレンス
岡本 隆史 武田 健太郎 相良 幸範
技術評論社
売り上げランキング: 103,005

2016/04/26 18:47 · katsubemakito

[Git]ファイルを移動する(git mv)

コマンド

$ 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

Gitを通さないで名前を変更するとどうなる?

別のファイルとして扱われるため、それまで記録し続けてきたログがその時点で途切れてしまいます。

$ 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ポケットリファレンス
岡本 隆史 武田 健太郎 相良 幸範
技術評論社
売り上げランキング: 103,005

2016/04/26 18:29 · katsubemakito

[Git]インデックスからファイルを削除する(git rm)

コマンド

$ 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

gitを通さずに削除するとどうなる?

実際にやってみましょう。

$ 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ポケットリファレンス
岡本 隆史 武田 健太郎 相良 幸範
技術評論社
売り上げランキング: 103,005

2016/04/25 14:47 · katsubemakito

[Git]インデックスにファイルを追加する(git add)

コマンド

$ 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

参考

関連書籍

Gitポケットリファレンス
岡本 隆史 武田 健太郎 相良 幸範
技術評論社
売り上げランキング: 103,005

2016/04/25 14:19 · katsubemakito

関連ページ

start.txt · 最終更新: 2020/06/23 14:10 (外部編集)