Git分支无法按预期工作 [英] Git branch does not work as expected
问题描述
我正在阅读本教程: http://gitref.org/branching/
据我了解,本教程说,如果我在 Branch A
中并进行一些更改,则不会影响其他分支.
As far as I understand, this tutorials says that if I am in Branch A
and make some changes, it is not going to affect other branches.
所以我正在测试:
$ mkdir test
$ cd test/
$ git init
Initialized empty Git repository in /root/test/.git/
$ echo 'foo' > foo.txt
$ ls
foo.txt
$ git add *
$ git commit -m 'added foo.txt'
[master (root-commit) 25f6eb7] added foo.txt
1 file changed, 1 insertion(+)
create mode 100644 foo.txt
$ git branch testing # created a new branch
$ git branch
* master
testing
# I am still in 'master' branch
$ echo 'bar' > bar.txt # Add a file to 'master' branch
$ ls
bar.txt foo.txt # makes sense, because we are in 'master'
$ git checkout testing # Let's go to 'testing' branch
Switched to branch 'testing'
$ git branch
master
* testing
# OK, we are in 'testing'
$ ls
bar.txt foo.txt # WHY??
因此,我更改了"master"分支,并影响了"testing".这是什么问题?
So, I changed 'master' branch and it affected 'testing'. What is the problem here?
PS:我的git版本1.7.10.4
PS: My git version 1.7.10.4
推荐答案
将文件添加到主"分支
---这是您的假设开始错误的步骤.
Add a file to 'master' branch
--- this is the step when your assumptions start being wrong.
通过使用 echo'bar'>创建文件bar.txt
命令您请勿将其添加到分支.
By creating a file with echo 'bar' > bar.txt
command you DO NOT add it to a branch.
您应该登台并承诺这样做.
You should stage it and commit to do so.
因此,如果您:
git add bar.txt
git commit -m "Added a new file"
在签出进行 testing
之前,它会按预期工作.
before checking out to testing
then it will work as you expect.
这篇关于Git分支无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!