Git:能够在不接触工作树的情况下暂存特定文件内容
我想修改一个(文本)文件的索引,而不必更改工作树文件状态。这可能吗?
I want to modify the index of one (text) file without having to change the working tree file state. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“更改索引中的文件而不更改工作目录”的另一种做法是仅对索引应用补丁。这通常是 GUI git 客户端仅暂存给定文件中选定行的方式。
首先(如果需要)清除该文件索引中的更改:
然后提取其完整补丁
编辑补丁文件以仅包含您要应用的更改,然后仅应用编辑的补丁:
请参阅:
Another take on "changing file in index without altering working dir" is to apply a patch to index only. This is often the way GUI git clients stage only selected lines from a given file.
You start out by (if you want) clearing out the changes from index for that file:
Then extracting the full patch for it
Edit the patch file to include only the changes you want to apply, and apply just the edited patch:
See:
是的,您可以在任何命令的 git 级别上使用 --work-tree 选项(这实际上不是真的。它应该适用于任何命令,但有边缘情况):
yes, you can use the --work-tree option on the git level of any (this is not actually true. It should work on any but there are edge cases) command:
是的,您可以使用 git update-index 在特定路径显式暂存 blob。
如果路径是分支新文件,您还需要使用
--add
。如果您要暂存的文件是 git 存储库中尚不存在的 blob,那么您可以使用 git hash-object 在 git 存储库中存储一个新的 blob,例如:
或者
您可以然后按上面的方法暂存 blob。
Yes, you can explicitly stage a blob at a particular path with
git update-index
.You will also need to use
--add
if the path is a branch new file.If the file that you want to stage is a blob that doesn't yet exist in the git repository then you can store a new blob in the git repository with
git hash-object
, e.g.:or
You can then stage the blob as above.