简体   繁体   English

创建新文件并通过一个命令将其添加到 git 版本控制

[英]Create new file and add it to git version control in one command

Is there a way in git to do the same thing as 2 shell commands would do:有没有办法在 git 中做与 2 个 shell 命令相同的事情:

touch somefile
git add somefile

in one command?在一个命令中?

git add copies a file from the work-tree into the index. git add将工作树中的文件复制到索引中。 This process requires that the file exist;这个过程要求文件存在; git add will use that same name in the index. git add将在索引中使用相同的名称。

Technically, what git add really does consists of two steps:从技术上讲, git add真正作用包括两个步骤:

  1. It must create or find the blob object in the repository database, that contains the contents of the file.它必须在存储库数据库中创建或查找包含文件内容blob对象。 If the file is empty, the object that goes into the database, or that is already in the database, has this hash:如果文件为空,则进入数据库或已在数据库中的对象具有以下哈希值:

     $ git hash-object -t blob --stdin < /dev/null e69de29bb2d1d6434b8b29ae775ad8c2e48c5391

    You can emulate this by running git hash-object with the -w flag, which tells Git to write the object to the database if needed, then gives you back the hash ID that represents those contents.您可以通过运行带有-w标志的git hash-object来模拟这一点,该标志告诉 Git 在需要时将对象写入数据库,然后返回代表这些内容的哈希 ID。

  2. Now that the object is in the database (freshly written if needed) and we have its hash ID, git add goes on to update the index.现在该对象在数据库中(如果需要,可以新鲜编写)并且我们有了它的哈希 ID, git add继续更新索引。 This update consists of deleting any higher stage entries with the same name, and writing to the stage-zero entry.此更新包括删除任何具有相同名称的更高阶段条目,并写入阶段零条目。 The entry contents are the desired mode of the file—either 100644 if the file should be marked read/write, or 100755 if it should be marked read/write/execute, the stage number (zero), the blob hash ID, and the path (represented as a UTF-8 string).入口内容是文件任一所希望的模式100644 ,如果该文件应被标记读/写,或100755 ,如果它应该被标记的读/写/执行,阶段数(零),则斑散列ID,以及路径(表示为 UTF-8 字符串)。

You can accomplish the second step using git update-info , either with --index-info (which reads from standard input) or --cacheinfo (which is limited to writing stage-zero entries, but that's what you want anyway).您可以使用git update-info完成第二步,可以使用--index-info (从标准输入读取)或--cacheinfo (仅限于写入阶段零条目,但无论如何这就是您想要的)。 For details, see the git update-index documentation .有关详细信息,请参阅git update-index文档

The problem with doing this instead of touch file; git add file这样做而不是touch file; git add file touch file; git add file is that unless you already know the hash ID for the contents, and that that hash ID is actually in the Git database already, it still takes two commands: git hash-object -w ... and git update-index ... . touch file; git add file是,除非您已经知道内容的哈希 ID,并且该哈希 ID 实际上已经在 Git 数据库中,否则它仍然需要两个命令: git hash-object -w ...git update-index ... And in that case, you might as well use the easy commands instead.在这种情况下,您不妨改用简单的命令。

(Note that while all repositories have the empty tree , they do not have the empty blob initially.) (请注意,虽然所有存储库都有空 tree ,但它们最初没有空 blob 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM