Ответ 1
При внесении изменений эти изменения существуют только в рабочем дереве до тех пор, пока вы их не зафиксируете.
Когда вы переключаете ветки, Git будет переносить изменения в вашей рабочей группе на новую проверку. Это часто бывает полезно, если вы заметили, что работаете над неправильной ветвью.
Там даже недавняя дискуссия об этом "неожиданном" поведении в списке рассылки Git об этом. Чтобы процитировать Юнио:
"J.V." gmail.com > пишет:
ОК, поэтому "дерево работы" - это новый термин для меня. Я думал, что мы находимся в изоляции песочницы, называемые "ветками", и изменения, сделанные в ветке, будут оставаться в эта ветка независимо.
Не думайте о "ветвях" как о изолированных песочницах.
Скорее, "ветки" - это места, где независимые государства должны быть записано.
Записанные состояния существуют только в репозитории Git и использовать его содержимое (например, просмотр в пейджере или браузере, редактирование в редакторе, запуск компилятор включен,...), вам необходимо материализовать содержимое филиал где-то в файловой системе. Такой набор файлов на файловая система формирует рабочее дерево. Акт об этом называется "проверка ветки". [...]
Изменить
На всякий случай, если ссылка выше становится недействительной
Проблема
Unexpected git behaviour
---
# First create a local git repo
$mkdir gitexample
$git config --global user.name "my name"
$git config --global user.email "[email protected]"
$git init
$git add .
$git commit -m 'initial commit'
# Create/Edit an empty file
$vi readme.txt
# add a single line: "this was added in the master branch."
$git commit -a
# create and checkout a new branch (from master)
$git branch test
$git checkout test
# edit the readme.txt file and do not commit
# add the text: "this was added in the test branch.", save and exit
$vi readme.txt
#now switch back to master
$git checkout master
$cat readme.txt
#You will see both lines in the master.
Question #1:
Why was this line added in the *master branch?
--- even further surprising
In the master branch, now do a commit
$git commit -a
cat readme.txt ( you will see the line in the master now that was added in the test branch )
Question #2:
Why did this happen?
# Now switch back to the test branch
$git checkout test
$cat readme.txt
You will only see the one line: "This was added in the master branch"
Question #3:
Why did this happen?
and NOT the line added in that branch: "this was added in the test branch" <= this line is gone
What is the reason for this?
1) Why do I see uncommitted changes in the branches made off master in the master branch?
2) Why, if I commit them in the master, do the disappear in the branch in which they were made?
This is confusing, I would think the * master branch would be left untouched. This would solve issue #2.
Ответ
On Fri, Nov 11, 2011 at 12:55:04PM -0800, Jvsrvcs wrote:
> Unexpected git behaviour
>
[ ... switch branches with local modifications ...]
> #You will see both lines in the master.
>
> Question #1:
> Why was this line added in the *master branch?
>
It wasn't. that line was added in the working directory. When you
switch branches, if the file in the tip of the current branch and the
file in the tip of the target branch don't differ, it safe to keep
your local changes, so git does. This is to support the use-case where
you start editing a file when the wrong branch is checked out and want
to change to the right one.
>
> --- even further surprising
> In the master branch, now do a commit
> $git commit -a
>
> cat readme.txt ( you will see the line in the master now that was added in
> the test branch )
>
> Question #2:
> Why did this happen?
... [show rest of quote]
... [show rest of quote]
Because you told git to commit the file with that modification in it.
>
> # Now switch back to the test branch
> $git checkout test
> $cat readme.txt
>
> You will only see the one line: "This was added in the master branch"
>
> Question #3:
> Why did this happen?
Because the file in the 'test' branch only has that line. As you said
yourself, you edited the file but didn't commit.
>
> and NOT the line added in that branch: "this was added in the test branch"
> <= this line is gone
Again, that line wasn't added in any branch but in the working
directory. The active branch was 'test', but doesn't magically mean
that uncommitted changes travel with it.