Git 忽略对被跟踪文件部分的本地更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21756531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git ignore local changes to portions of tracked files
提问by darnir
Specifically, I maintain a git repository of my dotfiles. I recently started working on a new machine and cloned my repository on the same.
具体来说,我维护我的 dotfiles 的 git 存储库。我最近开始在一台新机器上工作,并在同一台机器上克隆了我的存储库。
Now, I wish to make some changes to my dotfiles which are specific to this system only. These changes I wish to ignore in my repository.
现在,我希望对我的 dotfiles 进行一些更改,这些更改仅针对该系统。我希望在我的存储库中忽略这些更改。
Other changes that I make, should continue to be tracked and committed.
我所做的其他更改应该继续被跟踪和提交。
For example, in my .gitconfig, I have a setting as:
例如,在我的 .gitconfig 中,我有一个设置:
[push]
default = simple
Now, on my new machine, the version of git being used it very old. It still not not support the setting simple
for push. So, I'd like to change this, but only locally.
现在,在我的新机器上,使用的 git 版本很旧。它仍然不支持simple
推送设置。所以,我想改变这一点,但仅限于本地。
However, if I make any other changes to my .gitconfig, I'd like to keep track of those. Anyway I can achieve this?
但是,如果我对 .gitconfig 进行任何其他更改,我想跟踪这些更改。无论如何我能做到这一点吗?
EDIT:
I know of git update-index --assume-unchanged
. The problem with it is that git will no longer track my file at all, until I reverse it. And then, it will track all changes.
I wish to ignore certain changes and track the rest.
编辑:
我知道git update-index --assume-unchanged
. 它的问题是 git 将不再跟踪我的文件,直到我将其反转。然后,它将跟踪所有更改。
我希望忽略某些更改并跟踪其余更改。
采纳答案by Peter Bratton
I don't believe there's a specific command that will 'untrack' certain changes to a file. However, there's no reason that you couldn't create a local branch into which you pull changes from your remotes, but never send any changes back.
我不相信有一个特定的命令可以“取消跟踪”对文件的某些更改。但是,您没有理由不能创建一个本地分支,您可以在其中从远程设备中提取更改,但永远不会将任何更改发回。
回答by atupal
Try using this command:
尝试使用此命令:
git update-index --assume-unchanged FILENAME_TO_IGNORE
To reverse it (if you ever want to commit changes to it), use:
要反转它(如果您想对其进行更改),请使用:
git update-index --no-assume-unchanged
UPDATE:
更新:
Here's how to list 'assume unchanged'files under current directory:
以下是在当前目录下列出“假设未更改”文件的方法:
git ls-files -v | grep -E "^[a-z]"
As the -v
option will use lowercase letters for 'assume unchanged' files.
由于该-v
选项将使用小写字母表示“假定未更改”文件。
回答by Radon Rosborough
Here is an alternative solution to your specific problem. Place machine-config configuration in a ~/.gitconfig.local
file, and then put the following in your version-controlled ~/.gitconfig
:
这是针对您的特定问题的替代解决方案。将 machine-config 配置放在一个~/.gitconfig.local
文件中,然后将以下内容放入您的版本控制中~/.gitconfig
:
[include]
path = ~/.gitconfig.local
This will tell Git to treat anything it finds in ~/.gitconfig.local
as if it were in ~/.gitconfig
. Yes, you can override settings. No, it does not require the file to exist (Git will silently ignore the setting if there is no ~/.gitconfig.local
).
这将告诉 Git 将它在其中找到的任何内容~/.gitconfig.local
视为在~/.gitconfig
. 是的,您可以覆盖设置。不,它不需要文件存在(如果没有,Git 将默默地忽略该设置~/.gitconfig.local
)。
See herefor more information about [include]
.
有关更多信息,请参见此处[include]
。
I follow this strategy in my configurations for Emacs, Zsh, Git, Tmux, etc., so that they are customizable without the need to modify version-controlled files. To accomplish this, I have init.local.el
, .zshrc.local
, .gitconfig.local
, .tmux.local.conf
, and so on.
我在 Emacs、Zsh、Git、Tmux 等的配置中遵循此策略,以便它们可以自定义,而无需修改受版本控制的文件。为了实现这一点,我有init.local.el
、.zshrc.local
、.gitconfig.local
、.tmux.local.conf
等等。
回答by kynan
As suggested by Peter you can keep those machine specific changes in a branch. Obviously you will have to be careful to strictly separate changes to those machine specific files from the "mainline". There are 2 ways for doing this:
正如 Peter 所建议的,您可以将这些机器特定的更改保留在一个分支中。显然,您必须小心将那些机器特定文件的更改与“主线”严格分开。有两种方法可以做到这一点:
- Keep rebasing the branch whenever you change the mainline (this would be my preference)
- Keep merging mainline changes into the branch (but obviously nevermerge in the other direction)
- 每当您更改主线时,请继续重新设置分支(这将是我的偏好)
- 继续将主线更改合并到分支中(但显然永远不会在另一个方向合并)