Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git状态对于未跟踪和已提交的文件表现不同_Git - Fatal编程技术网

Git状态对于未跟踪和已提交的文件表现不同

Git状态对于未跟踪和已提交的文件表现不同,git,Git,在git上玩树枝时,我注意到一些不明显的行为: 假设我的回购协议中有以下结构: master--- \---feature_branch--- \---test_branch 我正在清理工作树。 现在,我在test_分支上添加了一个文件: git checkout test_branch touch test_file.txt 如果现在运行git status,我会得到以下结果: On branch test_br

在git上玩树枝时,我注意到一些不明显的行为:

假设我的回购协议中有以下结构:

master---
         \---feature_branch---
                              \---test_branch
我正在清理工作树。 现在,我在test_分支上添加了一个文件:

git checkout test_branch
touch test_file.txt
如果现在运行
git status
,我会得到以下结果:

On branch test_branch
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test_file.txt

nothing added to commit but untracked files present (use "git add" to track)
git checkout test_branch
touch test_file_2.txt
git add .
git commit -m "Add a file"
echo "foobar" >> test_file_2.txt
清理工作树后,我运行以下操作:

On branch test_branch
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test_file.txt

nothing added to commit but untracked files present (use "git add" to track)
git checkout test_branch
touch test_file_2.txt
git add .
git commit -m "Add a file"
echo "foobar" >> test_file_2.txt
当我现在运行
git status
时,我将得到:

On branch test_branch
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   test_file_2.txt

no changes added to commit (use "git add" and/or "git commit -a")

error: Your local changes to the following files would be overwritten by checkout:
    test_file_2.txt
Please commit your changes or stash them before you switch branches.
Aborting
因此,当有一个未跟踪的文件时,我能够切换分支。我甚至可以从master将其添加到索引中,因为我将从中提交的分支将决定文件的命运。 但是,如果已提交文件,则无法切换分支

这种行为的原因是什么

让我烦恼的是,无论我在哪个分支机构,我都会得到同样的地位

为了便于说明,让我们假设工作区中只有一个文件。您所做的就是
gitinit
,创建文件,添加文件,然后提交

themini:gittest mattneubelcap$ git init
Initialized empty Git repository in /Users/mattneubelcap/gittest/.git/
themini:gittest mattneubelcap$ echo "test" > test.txt
themini:gittest mattneubelcap$ git add .
themini:gittest mattneubelcap$ git commit -m "test"
[master (root-commit) 1569fd7] test
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
您在branch master上,未经签出就创建了branch mybranch。现在更改第一个文件。您还可以创建另一个文件。然后你问一下情况

themini:gittest mattneubelcap$ git branch mybranch
themini:gittest mattneubelcap$ echo "test still" > test.txt
themini:gittest mattneubelcap$ echo "test2" > test2.txt
themini:gittest mattneubelcap$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   test.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test2.txt

no changes added to commit (use "git add" and/or "git commit -a")
test.txt在这里仍然是旧的形式,因为我们在将文本从“test”更改为“test still”之前进行了分支。我们的状况如何

themini:gittest mattneubelcap$ git status
On branch mybranch
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test2.txt

nothing added to commit but untracked files present (use "git add" to track)
themini:gittest mattnuebelcap$git status
在树枝上我的树枝
未跟踪的文件:
(使用“git add…”包含在将提交的内容中)
test2.txt
提交时未添加任何内容,但存在未跟踪的文件(使用“git add”跟踪)
没有关于test.txt的消息;它就在那里,但没有什么有趣的事要知道。另一方面,test2.txt仍然是一个异常。git不会碰它,但它会继续抱怨它对这个文件感到困惑

我们换回去吧

themini:gittest mattneubelcap$ git checkout master
Switched to branch 'master'
themini:gittest mattneubelcap$ cat test.txt
test still
themini:gittest mattneubelcap$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test2.txt

nothing added to commit but untracked files present (use "git add" to track)
themini:gittest mattnuebelcap$git checkout master
切换到“主”分支
themini:gittest mattnuebelcap$cat test.txt
蒸馏器
themini:gittest Mattnuebelcap$git状态
论分行行长
未跟踪的文件:
(使用“git add…”包含在将提交的内容中)
test2.txt
提交时未添加任何内容,但存在未跟踪的文件(使用“git add”跟踪)
还有另一个版本的test.txt。但是,我们的状态仍然没有提到test.txt,因为我们对此一无所知。但是test2.txt仍然是一个异常,git告诉我们这一点

你看到了吗?对于git正在跟踪的所有文件,当您切换分支时,您将获得属于该分支的该文件的版本。但是对于git没有跟踪的所有文件,什么都不会发生;当周围的世界发生变化时,文件就在那里。这是因为该文件未被跟踪。让它来来去去去不关git的事。git不需要知道(它也不知道)您在创建它之后是否对它进行了修改。git将继续提到该文件存在且未被跟踪,直到您:

(a) 添加它(从而告诉git开始跟踪它);或

(b) 移除它;或


(c) 告诉git忽略它。

未跟踪的文件根本不在git的索引中,因此git无法管理它们。提交的文件在Git的索引中,至少目前是这样,因为当前提交,而且您还没有将该文件从索引中取出。相关:问题不在于跟踪了文件,问题在于存储库中这两个分支之间的文件不同。所以您在分支A上,存储库中有文件的副本。然后修改文件,并尝试切换到分支B,但在分支B上,文件与分支A上的文件不同,这意味着git希望更新该文件。但是,由于您已经对该文件进行了未提交的更改,git不想冒险修改该文件并导致问题,从而丢失您的更改,因此它会阻止切换。如果您有两个分支,且文件存在,但文件在两个分支上的内容相同,然后,即使有未提交的更改,您也可以在分支之间切换。但既然文件不同,你就不一样了。
themini:gittest mattneubelcap$ git checkout master
Switched to branch 'master'
themini:gittest mattneubelcap$ cat test.txt
test still
themini:gittest mattneubelcap$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test2.txt

nothing added to commit but untracked files present (use "git add" to track)