默认的git行为我不明白

时间:2011-02-22 02:17:30

标签: git git-branch

我还没有决定是否喜欢以下关于不受版本控制的文件/文件夹的行为。

特别是,当您签出不同的分支时,未版本化的文件似乎会跟随您,这似乎很奇怪。似乎所述文件应该只存在于它们创建的分支中。

有人可以帮助我理解为什么/这是否是可取的行为?

例如:

shopkins@shax:~/tmp/test$ ls
hello.txt
shopkins@shax:~/tmp/test$ git branch  
-
* master
my_branch
shopkins@shax:~/tmp/test$ 
shopkins@shax:~/tmp/test$ git checkout my_branch 
Switched to branch 'my_branch'
shopkins@shax:~/tmp/test$ mkdir adir
shopkins@shax:~/tmp/test$ touch adir/my_branch.txt
shopkins@shax:~/tmp/test$ git add adir/
shopkins@shax:~/tmp/test$ git commit -a -m "added adir with my_branch.txt"
[my_branch d36964c] added adir with my_branch.txt
0 files changed, 0 insertions(+), 0 deletions(-)
shopkins@shax:~/tmp/test$ git checkout my_branch
Switched to branch 'my_branch'
shopkins@shax:~/tmp/test$ tree
.
|-- adir
|   |-- my_branch.txt
|   `-- orphan.txt
`-- hello.txt

1 directory, 3 files

create mode 100644 adir/my_branch.txt
shopkins@shax:~/tmp/test$ touch adir/orphan.txt
shopkins@shax:~/tmp/test$ git checkout master
Switched to branch 'master'
shopkins@shax:~/tmp/test$ ls
adir  hello.txt
shopkins@shax:~/tmp/test$ tree
.
|-- adir
|   `-- orphan.txt
`-- hello.txt

1 directory, 2 files

修改     事实证明,在下面的第一次编辑中,分支之间的文件没有任何变化。感谢大家的帮助!

修改 似乎git在签出结束时不会编写版本化文件的修改。在以下示例中,another.txt不受版本控制:

shopkins@shax:~/tmp/test$ ls -l
total 4
drwxr-xr-x 2 shopkins shopkins 4096 2011-02-21 21:49 adir
-rw-r--r-- 1 shopkins shopkins    0 2011-02-21 21:47 another.txt
-rw-r--r-- 1 shopkins shopkins    0 2011-02-21 21:49 hello.txt
shopkins@shax:~/tmp/test$ git checkout my_branch
Switched to branch 'my_branch'
shopkins@shax:~/tmp/test$ ls -l
total 4
drwxr-xr-x 2 shopkins shopkins 4096 2011-02-21 21:49 adir
-rw-r--r-- 1 shopkins shopkins    0 2011-02-21 21:47 another.txt
-rw-r--r-- 1 shopkins shopkins    0 2011-02-21 21:49 hello.txt
shopkins@shax:~/tmp/test$ 

3 个答案:

答案 0 :(得分:4)

Git甚至不知道存在未版本控制的文件。从技术上讲,它们甚至不是存储库的一部分。这就是为什么它们被称为“非版本化文件”。除非被告知,否则Git不会修改,删除,存储或跟踪任何文件。

如果您的软件在构建过程中生成临时文件,或者编译后的可执行文件与源代码保留在同一目录中,那么这是理想的经典情况。显然,如果某人正在克隆您的存储库以便他们可以从您那里下载您的软件,那么您会希望所有这些文件都“落后”。但更重要的是,你不希望它们使版本历史变得混乱。只有在源代码更改时,这些文件何时更改无关紧要。因此,每次提交时都会看到这些文件的差异只会让您更难找到实际需要的信息。

答案 1 :(得分:1)

这可以使构建更快,因为未添加的输出在分支交换机之间挂起,并且您没有将make输出到某个外部目录。

答案 2 :(得分:1)

为什么git会删除无法恢复的文件? Git只留下它不知道的文件。

相关问题