git and local modifications

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

I am discovering how to use git.

I just made the following test:

  1. create a folder and 2 files
  2. then git init, git add ., git commit -m "initial commit"
  3. create a branch: git branch experimental, git checkout experimental
  4. then change the name of the folder and delete one of the files, then git add ., git commit -m "experimental"
  5. go back to master: git checkout master

    Surprise: I do not find the master as I left it; the folder has disappeared. And I have to do a git -reset --hard to find my folder.

    I wonder if I did something wrong or if I have not understood how git manages local changes. Thanks for your help.

Detailed scenario:

mkdir GitTest cd GitTest/ mkdir Folder1 echo "master" > Folder1/File1.txt echo "master" > File2.txt git init git add . git commit -m "init" git branch expe git checkout expe mv File2.txt File3.txt  mv Folder1/ Folder1-exp/ echo "expe" >> Folder1-exp/File1.txt  git add .   git commit -m "expe" git checkout master ls git checkout expe ls 

回答1:

That would be because of the difference between git add . and git add -A.

Only git add -A will record new files and deleted files.

When you did your second git add ., here what git status had to say:

C:\HOMEWARE\git\tests\p2>git status # On branch exp # Changes to be committed: #   (use "git reset HEAD <file>..." to unstage) # #       new file:   f2/f1b.txt # # Changed but not updated: #   (use "git add/rm <file>..." to update what will be committed) #   (use "git checkout -- <file>..." to discard changes in working directory) # #       deleted:    f1/f1a.txt #       deleted:    f1/f1b.txt # 

The working tree had the first directory deleted.

So when you switched back to master, that working tree was preserved:

C:\HOMEWARE\git\tests\p2>git checkout master D       f1/f1a.txt D       f1/f1b.txt Switched to branch 'master' 

Hence the disappearance of 'f1'...



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!