简体   繁体   English

如何在不删除这些文件的情况下切换到忽略文件的git分支?

[英]How to I switch to a git branch that ignores files without deleting these files?

I have branch master that ignores .idea in its .gitignore . 我有分支master ,在其.gitignore中忽略了.idea From that, I created branch noIgnore that has no .gitignore . 从那以后,我创建了没有.gitignore分支noIgnore

I found that checkout master deletes .idea . 我发现checkout master删除了.idea

That's not the behavior I want. 那不是我想要的行为。 I want to keep .idea , just not track it, not on master . 我想保留.idea ,不要跟踪它,不要跟踪master How do I do that? 我怎么做?

You could stop tracking changes for a file in repo 您可以停止跟踪repo中文件的更改

git update-index --skip-worktree .idea

Since you're tracking it in the other branch, then when you make changes you'll want to start tracking changes again 由于您在其他分支中跟踪它,因此当您进行更改时,您将希望再次开始跟踪更改

git update-index --no-skip-worktree .idea

When you get tired of doing this manually, you could create a post-checkout hook if on master, stop tracking, else start tracking. 当您厌倦了手动执行此操作时,您可以创建一个post-checkout挂钩,如果在master上,停止跟踪,否则开始跟踪。

It removes .idea because that's what checkout does, it checks out master where there's no .idea. 它删除了.idea,因为这就是checkout所做的,它会检查master没有.idea。 Try to add --no-track to your command. 尝试添加--no-track到您的命令。

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

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