Nowe posty

Autor Wątek: ‚Ro­zdwoje­nie’ ka­talo­gu przy uży­ciu git  (Przeczytany 390 razy)

Offline overcq

  • Nowy na forum
  • *
  • Wiadomości: 32
    • Zobacz profil
    • ‛overcq’
Wszystko zaczyna się w linii “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”.

Offline Paweł Kraszewski

  • Administrator
  • Guru
  • *****
  • Wiadomości: 3062
  • Lenistwo jest matką potrzeby = babcią wynalazku
    • Zobacz profil
Odp: ‚Ro­zdwoje­nie’ ka­talo­gu przy uży­ciu git
« Odpowiedź #1 dnia: 2023-10-29, 10:03:25 »
Przed git checkout test nie dodałeś i zakomitowałeś zmiany zawartości a.txt, tylko zmianę nazwy. Polecenie git mv nie robi rm/add tylko zmienia metadane pliku - jeżeli modyfikowana zmiana zawartości nie była zakomitowana to dalej nie jest.

Po git commit -m 'Second commit' w repo jest zmiana nazwy, ale dalej nie ma wyniku działania echo Text > a/a.txt (bo to są dwie niezależne operacje zrobione na working tree, masz jasno napisane, które operacje są commited, które not staged)

Stąd masz błąd git checkout test "Your local changes to the following files would be overwritten by checkout: aa/a.txt" - bo tak, checkout musiałby usunąć aa/a.txt z niezakomitowanymi zmianami.

Potem jeszcze raz zmieniasz nazwę ale tego nie komitujesz i branch test traci "tożsamość" aa/a.txt i checkoutuje goły a/a.txt bo nie robi to konfliktu.

Hint na przyszłość: przełączenia branchy robisz na czystym drzewie.
« Ostatnia zmiana: 2023-10-29, 10:07:51 wysłana przez Paweł Kraszewski »
Paweł Kraszewski
~Arch/Void/Gentoo/FreeBSD/OpenBSD/Specjalizowane customy