git 可以提交“空版本”吗?新的非空文件?

发布于 2024-10-03 20:22:15 字数 569 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不如归去 2024-10-10 20:22:15

说实话,我不太明白这有什么用。我会尝试修复审核过程而不是搞乱历史。但如果您真的想这样做,可以采用以下几种方法:

  1. 务实的方法:

    mv 文件不正常
    触摸文件
    git 添加文件
    mv 偏僻文件
    
  2. 瓷器的方法:

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

    ...当被问及是否应该添加单个大块时,只需回答“否”即可。(显然这在 2019 年不再起作用。)

  3. 管道方法:

    首先,确保对象数据库中存在空对象:

    git hash-object -w --stdin < /dev/空
    

    这将返回空 blob 的 SHA1(即 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391)。您只需创建该对象一次。现在您可以通过以下方式在索引中创建空文件

    git update-index --add --cacheinfo 0644 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:

  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. (Apparently this does not work anymore in 2019.)

  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
    
一笔一画续写前缘 2024-10-10 20:22:15

自动添加所有空文件和文件夹


强制添加所有文件

$ git add -Af

您可以在每个空文件夹中自动添加 .gitignore 文件。

$ find $PATH_TO_REPOSITORY -type d ! -path "*.git*" -empty -exec cp .gitignore '{}'/ \;

您可以在每个空文件夹中自动添加 .gitkeep 文件

$ find $PATH_TO_REPOSITORY -type d ! -path "*.git*" -empty -exec touch '{}'/.gitkeep \;

Automatically add all empty files and folders


Force add all files

$ git add -Af

You can automatically add a .gitignore file in every empty folder.

$ find $PATH_TO_REPOSITORY -type d ! -path "*.git*" -empty -exec cp .gitignore '{}'/ \;

You can automatically add a .gitkeep file in every empty folder.

$ find $PATH_TO_REPOSITORY -type d ! -path "*.git*" -empty -exec touch '{}'/.gitkeep \;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文