LoginSignup
0
0

More than 3 years have passed since last update.

gitでハマったお話

Posted at

やってみれば当然っちゃ当然のことにハマってしまったので、誰かのために残しておきます。

概要

結論をまず言います。
git statusコマンドを実行し、下記のような表示がでるようなとき、git addgit commitgit pushはできません。

# On branch master
nothing to commit, working directory clean

git管理下におかれたディレクトリ内で、「変更されたものはないから、コミットするものはないよ」ということです。

(・д・)?「は?なにをあたりまえのことを」

って、なりますよね…。わかります。

では、今回おちいった(といってもすぐに解決した程度のレベルですが)ことを簡単な例をおって説明していきます。

解説

実験用のディレクトリを作り、git initします

$ mkdir git_test
$ cd git_test/
$ git init
Initialized empty Git repository in /home/hogeuser/git_test/.git/

ディレクトリを作成、その中に適当なファイルを作成します

$ mkdir test_dir
$ touch test_dir/test_file

# ディレクトリ構成はこんな感じ
$ tree git_test/
git_test/
└── test_dir/
    └── test_file

ひとまずgit commitまでします

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test_dir/
nothing added to commit but untracked files present (use "git add" to track)

$ git add .
$ git commit -m "first commit"
[master (root-commit) f28650e] first commit
1 file changed, 0 insertions(+), 0 deletions(-)

新規ファイルを作成します(ここで作成するファイル名は先ほど作成したファイルと同名にします。ここがハマりポイント)

$ touch test_file
$ ls
test_dir/  test_file <-- こいつを追加した

この時点でgit statusすると…

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test_file
nothing added to commit but untracked files present (use "git add" to track)

はい、「追跡対象になってない(git addしていない)ファイルがあるよ」と。そりゃ新しいファイル作ったんだもんね。そうだよね!ってなる。

追加したファイルをtest_dirディレクトリへ移動。test_fileファイルを上書きします

$ mv test_file test_dir/

同じファイルとはいえ、タイムスタンプとか違うから、状態としては更新されたことになってるよな!と、考えてました。はい…

再度、git status

$ git st
# On branch master
nothing to commit, working directory clean

!!!なぬ?なんもコミットするものはないだと…?

はい、これがハマりポイントでした。ただの認識ミスです…。

推測ですが、diffで内容を比較して違いがないのなら、git的には「それ内容は変わってないよね?同じだよね」という扱いになるのですね。
てっきり更新時間が変われば、gitで変更されたと認識される(git addしてね状態になる)と思ってました。なりません!

手動で順を追ってやってみれば気づくものも、パイプライン中のシェルスクリプトの中にgitコマンドを記述してて、「おかしいな、git pushされてないな…」みたいになってました。

どなたかの「へぇ~」くらいになればうれしいですが…ほとんどポエムですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0