可以提交“空白版本”新文件? [英] Can git commit "empty versions" of new files?
问题描述
git diff
应通过将文件与其提交的空版本进行比较来显示新添加的内容)。 $ b $
有 git add -N file ...
,它将 file
与索引中的空白内容,但这只是说文件
将被添加,
有没有办法做到这一点这是什么?
PS:这个问题是在程序的上下文中自动添加文件到git仓库(我的程序遵循学生编写的代码)。未提交的代码是我尚未批准的代码。因此,即使我的程序刚刚在主目录中发现了一个新的非空程序,由学生创建的程序的状态应该是空状态;这是通过在git仓库中自动提交新的学生程序文件的新的空白版本来处理的。因此,与上一次提交的git修订版相比,它们编写的新代码行显示为新添加的内容。 诚实,我真的不明白这有什么用处。我会尝试修复审查过程而不是搞乱历史。但是,如果你真的想这样做,这里有几种方法:
-
务实的方法:
mv文件超出部分
触摸文件
git增加文件
mv超出文件
-
瓷器方法:
git add -N文件
git add -p文件
<当问到是否应该添加单个人群时,回答否。 b
首先,确保对象数据库中存在一个空对象:
git hash-object - w --stdin< / dev / null
这将返回空blob的SHA1( e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
)。您只需创建一次该对象。现在,您可以通过
git update-index --add --cacheinfo 0644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file $ b $在索引中创建空文件b
Can git commit empty versions of some files? The case in point is that I need new (untracked) files to first be added and committed as empty files, so as to mark their contents as being new and to be reviewed (the full, untracked files should not be added to the index; git diff
should show the newly added contents by comparing the file to its committed empty version).
There is git add -N file…
, which puts file
with an empty content in the index, but this only says that file
will be added, and git commit
complains that the file has not been added. The thing is that the current, non-empty version is not what has to be added, but only an empty version of the new file.
Is there a way to do this?
PS: This question is asked in the context of a program that automatically adds files to a git repository (my program follows what code students write). Uncommitted code is code that I have yet to approve. Thus, the state in which a program created by a student starts should be the empty state, even though my program just found a new, non-empty program in their home directory; this is handled by automatically committing a new, empty version of any new student program file in a git repository. Thus, new code lines that they write appear as being newly added contents, compared to the last committed git revision.
To be honest, I do not really understand what this is useful for. I would try to fix the review process instead of messing up the history. But if you really want to do this, here are several ways how:
The pragmatic approach:
mv file out-of-way touch file git add file mv out-of-way file
The porcelain approach:
git add -N file git add -p file
... and just answer "no" when asked whether the single hunk should be added.
The plumbing approach:
First, make sure an empty object exists in the object database:
git hash-object -w --stdin < /dev/null
This will return the SHA1 of an empty blob (which is
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
). You have to create this object only once. Now you can create empty files in the index bygit update-index --add --cacheinfo 0644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file
这篇关于可以提交“空白版本”新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!