1. Overview
1.概述
We use git update-index when we want to manually operate on files in Git staging area. This command supports two options that are often misused: –assume-unchanged and –skip-worktree.
当我们想对git update-index中的文件进行手动操作时,我们使用Git>暂存区。这个命令支持两个经常被误用的选项。 -assume-unchanged和-skip-worktree。
In this tutorial, we’ll see how these two options differ and provide a use case for each.
在本教程中,我们将看到这两个选项有什么不同,并为每个选项提供一个用例。
2. What Does the assume-unchanged Option Do?
2.assume-unchanged选项有什么作用?
The –assume-unchanged option tells Git to temporarily assume that a tracked file has not been modified in the working tree. Therefore, changes made won’t be reflected in the staging area:
-assume-unchanged 选项告诉 Git 暂时假定一个被追踪的文件在工作树上没有被修改。因此,所做的修改不会反映在暂存区域。
$ git update-index --assume-unchanged assumeunchanged.txt
We can verify the file status with git ls-files:
我们可以用git ls-files验证文件状态。
$ git ls-files -v
$ h assumeunchanged.txt
Here, the h tag indicates that assume-unchanged.txt is marked with the assumed-unchanged option.
这里,h 标签表示assume-unchanged.txt被标记为assumed-unchanged选项。
Although mainly used for that purpose, the assume-unchanged option was never meant to ignore changes to tracked files. It’s designed for cases where it’s expensive to check whether a group of files have been modified. What happens if we want to optimize resource usage on slow filesystems: git omits any checking against the target file and won’t compare its versions in the working directory and in the index.
尽管主要用于这一目的,但assume-unchanged选项从来就不是为了忽略对被跟踪文件的修改。它是为那些检查一组文件是否被修改的成本很高的情况而设计的。如果我们想在慢速文件系统上优化资源使用,会发生什么情况:git省略了对目标文件的任何检查,不会比较其在工作目录和索引中的版本。
This feature is lost whenever the target file’s entry in the index changes. That can happen when the file is changed upstream. To unset this option, we can use –no-assume-unchanged:
只要目标文件在索引中的条目发生变化,这个功能就会丢失。这可能发生在文件被改变的上游。要取消这个选项,我们可以使用-no-assume-unchanged。
$ git update-index --no-assume-unchanged assumeunchanged.txt
3. What Does the skip-worktree Option Do?
3.skip-worktree选项有什么作用?
The –skip-worktree option ignores uncommitted changes in a file that is already tracked. Regardless of any modification made in the working tree, git will always use the file content and attributes from the staging area. This is useful when we want to add local changes to a file without pushing them to the upstream:
-skip-worktree 选项会忽略已经被跟踪的文件中未提交的修改。无论在工作树上做了什么修改,git都会使用暂存区的文件内容和属性。这在我们想给文件添加本地修改而不推送到上游时很有用。
$ git update-index --skip-worktree skipworktree.txt
We can verify the file status:
我们可以验证文件的状态。
$ git ls-files -v
$ S skipworktree.txt
Here, the S indicates that skip-worktree.txt is marked with the skip-worktree option.
这里,S表示 skip-worktree.txt被标记为skip-worktree选项.。
This option is automatically unset when the file changes in the index i.e., if the file’s been changed upstream and we pull it.
当文件在索引中发生变化时,这个选项会自动取消,也就是说,如果文件在上游发生了变化,我们就把它拉出来。
–no-skip-worktree is used to unset this option. It’s useful in case wrong files were flagged, or if the circumstances have changed and previously skipped files shouldn’t be ignored anymore:
-no-skip-worktree是用来取消这个选项的。它在错误的文件被标记的情况下很有用,或者如果情况发生了变化,以前跳过的文件不应该再被忽略。
$ git update-index --no-skip-worktree skipworktree.txt
4. Differences Between the Options
4.选项之间的差异
4.1. Branch Switching
4.1.分支机构切换
There’s no issue when checking out a branch when a file has the –skip-worktree option on. But, –assume-unchanged will raise an error:
当一个文件有-skip-worktree选项时,签出一个分支时没有问题。但是,-assume-unchanged会引发一个错误。
$ git checkout another-branch
error: Your local changes to the following files would be overwritten by checkout:
assumeunchanged.txt
Please commit your changes or stash them before you switch branches.
Aborting
We can unset the option to overcome this situation:
我们可以取消该选项来克服这种情况。
$ git update-index --no-assume-unchanged assumeunchanged.txt
$ git checkout another-branch
Switched to branch 'another-branch'
4.2. Precedence
4.2. 优先权
–skip-worktree takes precedence over —assume-unchanged bit when both are set. Let’s try to set both options on a file:
-skip-worktree优先于-assume-unchanged位,当两者都被设置时。 让我们试着在一个文件上设置这两个选项。
$ git update-index --assume-unchanged --skip-work-tree worktree-assumeunchanged.txt
The file’s status confirms skip-wortkree precedence:
该文件的状态确认了skip-wortkree的优先权。
$ git ls-files -v
$ S worktree-assumeunchanged.txt
5. Conclusion
5.总结
In this article, we discussed the difference in usage of Git’s —assume-unchanged and —skip-worktree options. We also discussed their precedence and how they interact with local and upstream branches.
在这篇文章中,我们讨论了Git的-assume-unchanged和-skip-worktree选项的用法差异。我们还讨论了它们的优先级以及它们与本地和上游分支的交互。