git 可以提交“空版本”吗?新的非空文件?
git 可以提交某些文件的空版本吗?恰当的例子是,我需要首先添加新的(未跟踪的)非空文件并将其作为空文件提交,以便将其内容标记为新内容并进行审查(完整的,未跟踪的文件不应添加到索引中;git diff
应通过将文件与其提交的空版本进行比较来显示新添加的内容)。
有 git add -N file...
,它将索引中内容为空的 file
放入索引中,但这仅表示 file
将被添加,并且git commit
抱怨该文件尚未添加。问题是当前的非空版本不是必须添加的内容,而只是新文件的空版本。
有办法做到这一点吗?
PS:这个问题是在一个自动将文件添加到 git 存储库的程序的上下文中提出的(我的程序遵循学生编写的代码)。未提交的代码是我尚未批准的代码。因此,学生创建的程序启动时的状态应该是空状态,即使我的程序刚刚在其主目录中找到了一个新的非空程序;这是通过在 git 存储库中自动提交任何新学生程序文件的新的空版本来处理的。因此,与上次提交的 git 修订版相比,他们编写的新代码行看起来像是新添加的内容。
Can git commit empty versions of some files? The case in point is that I need new (untracked), non-empty 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 file 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
说实话,我不太明白这有什么用。我会尝试修复审核过程而不是搞乱历史。但如果您真的想这样做,可以采用以下几种方法:
务实的方法:
瓷器的方法:...当被问及是否应该添加单个大块时,只需回答“否”即可。(显然这在 2019 年不再起作用。)管道方法:
首先,确保对象数据库中存在空对象:
这将返回空 blob 的 SHA1(即
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
)。您只需创建该对象一次。现在您可以通过以下方式在索引中创建空文件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:
The porcelain approach:... and just answer "no" when asked whether the single hunk should be added.(Apparently this does not work anymore in 2019.)The plumbing approach:
First, make sure an empty object exists in the object database:
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 by自动添加所有空文件和文件夹
强制添加所有文件
您可以在每个空文件夹中自动添加 .gitignore 文件。
您可以在每个空文件夹中自动添加 .gitkeep 文件。
Automatically add all empty files and folders
Force add all files
You can automatically add a .gitignore file in every empty folder.
You can automatically add a .gitkeep file in every empty folder.