可以提交“空白版本”新文件? [英] Can git commit "empty versions" of new files?

查看:140
本文介绍了可以提交“空白版本”新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git可以提交某些文件的空白版本吗?这种情况下,我需要新的(未跟踪的)文件首先被添加并提交为文件,以便将其内容标记为新文件并进行审阅(完整的未跟踪文件应该 添加到索引中; git diff 应通过将文件与其提交的空版本进行比较来显示新添加的内容)。


$ b $

git add -N file ... ,它将 file 与索引中的空白内容,但这只是说文件 将被添加 git commit 抱怨文件没有被添加。问题是,当前的非空版本不是必须添加的,而只是新文件的空版本。



有没有办法做到这一点这是什么?



PS:这个问题是在程序的上下文中自动添加文件到git仓库(我的程序遵循学生编写的代码)。未提交的代码是我尚未批准的代码。因此,即使我的程序刚刚在主目录中发现了一个新的非空程序,由学生创建的程序的状态应该是状态;这是通过在git仓库中自动提交新的学生程序文件的新的空白版本来处理的。因此,与上一次提交的git修订版相比,它们编写的新代码行显示为新添加的内容。 诚实,我真的不明白这有什么用处。我会尝试修复审查过程而不是搞乱历史。但是,如果你真的想这样做,这里有几种方法:


  1. 务实的方法:

      mv文件超出部分
    触摸文件
    git增加文件
    mv超出文件


  2. 瓷器方法:

      git add -N文件
    git add -p文件



    <当问到是否应该添加单个人群时,回答否。

  3. 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:

  1. The pragmatic approach:

    mv file out-of-way
    touch file
    git add file
    mv out-of-way file
    

  2. The porcelain approach:

    git add -N file
    git add -p file
    

    ... and just answer "no" when asked whether the single hunk should be added.

  3. 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 by

    git update-index --add --cacheinfo 0644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file
    

这篇关于可以提交“空白版本”新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆