Wszystko zaczyna się w linii 51 git checkout test
. Powstaje katalog a/
i aaa/
albo aa/
i aaa/
.
$ mkdir git && cd git && git init && mkdir a && touch a/a.txt
Initialized empty Git repository in /home/inc/moje/wysypisko/tymczasowe/git/.git/
$ git add .
$ git commit -m 'Initial commit'
[main (root-commit) e997459] Initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a/a.txt
$ echo Text > a/a.txt
$ git checkout -b test
Switched to a new branch 'test'
$ git checkout main
M a/a.txt
Switched to branch 'main'
$ git mv a aa
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: a/a.txt -> aa/a.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: aa/a.txt
$ ls
aa
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: a/a.txt -> aa/a.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: aa/a.txt
$ git commit -m 'Second commit'
[main 50452c5] Second commit
1 file changed, 0 insertions(+), 0 deletions(-)
rename {a => aa}/a.txt (100%)
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: aa/a.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git checkout test
error: Your local changes to the following files would be overwritten by checkout:
aa/a.txt
Please commit your changes or stash them before you switch branches.
Aborting
$ git mv aa aaa
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: aa/a.txt -> aaa/a.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: aaa/a.txt
$ git checkout test
A aaa/a.txt
Switched to branch 'test'
$ ls
a aaa
$ git status
On branch test
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: aaa/a.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: aaa/a.txt
$ git checkout main
A aaa/a.txt
Switched to branch 'main'
$ ls
aa aaa
$ cat aa/a.txt
$ cat aaa/a.txt
Text
Jak się zabezpieczyć przed takim zachowaniem gita?
Poza tym udało mi się również zrobić tak, że w katalogu o starej nazwie znalazły się pliki zmodyfikowane ostatnio, ale nie umiem teraz tego odtworzyć.
Jeśli ktokolwiek myśli, że to jest błahy problem, to warto zauważyć, że wystarczy nie wykonać polecenia git add .
(lub podobnego) przed git commit
i wykonać git mv
.