0

I have a problem which was described in post 'git status' shows changed files, but 'git diff' doesn't.. My current state is the following:

wakatana@ubuntu:~/magic_repo$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   1.txt
        new file:   3.txt

# get content of file in working area
wakatana@ubuntu:~/magic_repo$ cat 1.txt
22

# get content of file in staging area
wakatana@ubuntu:~/magic_repo$ git show :1.txt
22

# diff file in working area and staging area
wakatana@ubuntu:~/magic_repo$ git diff 1.txt
wakatana@ubuntu:~/magic_repo$

As you can see both files in working area and staging has the same content. Based on mentioned thread I've created patch which shows the differences. If I understand it correctly it shows that that the file is different because of its content not because of its file mode (which is common case of this problem).

wakatana@ubuntu:~/magic_repo$ git format-patch HEAD^
0001-1.txt-changed-2.txt-remains-the-same.patch

wakatana@ubuntu:~/magic_repo$ cat 0001-1.txt-changed-2.txt-remains-the-same.patch
From f26c665bc9ffd5b704040656f7ecb17db85ccbf6 Mon Sep 17 00:00:00 2001
From: wakatana <my_secret_email@secret.com>
Date: Thu, 14 Jul 2022 17:07:15 +0200
Subject: [PATCH] 1.txt changed, 2.txt remains the same

---
 1.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/1.txt b/1.txt
index d00491f..b4de394 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1 @@
-1
+11
--
2.25.1

My question is: How did I get into this state, and why? I mean what command sequence I have to issue in new repo to get same results as I've described above? Is it some git weird behavior or just my misunderstanding of how GIT works?

Peter Krebs
  • 3,831
  • 2
  • 15
  • 29
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
  • 1
    `git diff` shows the difference between the repo and changed files. If all changes have been staged it will show nothing – mousetail Jul 15 '22 at 11:17
  • What bearing is `git format-patch HEAD^` imagined to have? – matt Jul 15 '22 at 11:40
  • @matt It was recommended in mentioned tree as command which should clear up what is happening. I do not really understand what is going on here, that is why I've posted it on SO. @mousetail what do you mean by `repo` and `changed files`? AFAIK there are 3 git areas : 1. working tree 2. staging area 3. history (aka Git directory) – Wakan Tanka Jul 15 '22 at 11:50
  • *typo correct: It was recommended in mentioned thread (not tree) ... – Wakan Tanka Jul 15 '22 at 11:57
  • Okay, let me put it another way. `git format-patch HEAD^` has absolutely nothing to do with the matter. – matt Jul 15 '22 at 11:57
  • You are not in any "state". Everything that's happening in the first part of your question is perfectly normal right after `git add`, and the whole second part of the question is totally irrelevant. And https://stackoverflow.com/questions/14564946/git-status-shows-changed-files-but-git-diff-doesnt is irrelevant too; it is about something else. – matt Jul 15 '22 at 12:16
  • Note that `git diff --staged` or `git diff --cached` (these are the same command, the `--staged` and `--cached` are synonyms, use whichever you remember easier) will show you what you've actually changed. In this case that would be replacing `11` with `22`. – torek Jul 15 '22 at 14:09

1 Answers1

0

How did I get into this state, and why?

You are not in any "state". Everything that's happening in the first part of your question is perfectly normal right after git add, and the whole second part of the question is totally irrelevant. And 'git status' shows changed files, but 'git diff' doesn't is irrelevant too; it is about something else.

I mean what command sequence I have to issue in new repo to get same results as I've described above?

That command sequence would be

git add .

It causes the staging area to look just like the working tree, but different from HEAD (the most recent commit).

I'll demonstrate:

% git init
% echo 11 > 1.txt
% git add .
% git commit -m11
% echo 22 > 1.txt 
% git add .

Okay, let's look around in the same way you did:

% git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   1.txt

% cat 1.txt
22

% git show :1.txt
22

% git diff
% 

See? Exactly the same. Totally normal. Nothing to see here, folks, move along.

matt
  • 515,959
  • 87
  • 875
  • 1,141