我如何在本地删除文件,而又不将这些删除提交到远程存储库,同时保持“干净"?工作树? [英] How do I delete files locally without commiting these deletions to a remote repository while keeping a "clean" working tree?

查看:75
本文介绍了我如何在本地删除文件,而又不将这些删除提交到远程存储库,同时保持“干净"?工作树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与团队的遥控器上存在某些原因文件.它们并不经常更改,但这无关紧要:将它们放在我的本地存储库中会导致问题.

There are certain files that exist for reasons on the team's remote. They are not frequently changed, but that doesn't matter: having them in my local repository causes issues.

我可以很容易地删除这些文件,但这意味着我需要非常谨慎地对待提交的方式和内容.如果这些文件在这些分支之间已更改,则在这些文件已更改时切换分支可能会很痛苦.

I can very easily delete these files but it means I need to be very careful with how and what I commit. Switching branches when these files have changed can be a pain if the files have changed between those branches.

我如何才能在本地删除这些文件,并以干净的工作树的幻想将它们保留在远程位置?我希望能够在不存在这些文件的情况下切换分支并进行工作(登台/撤台/丢弃).如果它们在一个分支上发生更改,而我切换到该分支,则文件应保持删除状态.

How can I delete these files locally, keep them on the remote, with the illusion of a clean working tree? I'd like to be able to switch branches and do work (stage/unstage/discard) without the presence of these said files. If they change on one branch and I switch to that branch, the files should remain deleted.

到目前为止,我最好的解决方法:

My best workaround so far:

  1. 分支后删除所有上述文件(对于我所有的dev分支).
  2. 提交.
  3. 照常工作.
  4. 在打开拉取请求之前,重新设置基数并删除第一个提交.

第1步和第2步可以使用git钩子完成,但是我并没有为此烦恼.

Steps 1 and 2 can be done with a git hook, but I haven't bothered setting that up.

谢谢!

还请注意XY问题(为什么要这样做"):正确"的解决方案是与已经建立我的IDE的团队一起打开支持通知单.同时,一种变通方法也可能会回答这个问题,以防其他地方出现问题.

Also a note on the XY problem ("why would you want to do this"): the "correct" solution is to open a support ticket with the team that built my IDE which I'm already doing. A workaround in the meantime might also help answer this question in case it applies elsewhere.

我发现提出这个问题的两个原因:

I find two reasons for asking this:

  1. 我的同事希望Python 3.6项目的主干分支中存在本地.pyi文件. PyCharm使用这些.pyi文件作为事实来源,例如:如果向文件添加功能,则必须在相关的.pyi文件中添加存根,否则PyCharm将无法找到或建议新功能
  2. GitHub Actions要求您在发布ref作为Action版本时提交整个node_modules目录以及入口点.js文件.这对于TypeScript GitHub Action的多个分支来说是个问题,在该分支中,开发人员通常在提交之前运行tsc,因为在切换分支时,工作树中的已编译.js总是会冲突.藏匿是处理此问题的正确方法,但有时可能会带来不便.
  1. My colleague wants local .pyi files present on the trunk branch for a Python 3.6 project. PyCharm uses these .pyi files as a source of truth, e.g.: if you add a function to a file, you must add a stub to the associated .pyi file or PyCharm won't be able to find or suggest the new function.
  2. GitHub Actions require you to commit your entire node_modules directory as well as an entrypoint .js file when publishing a ref as an Action version. This is problematic with multiple branches of a TypeScript GitHub Action where the developer usually runs tsc before committing since the compiled .js in the working tree will always conflict when switching branches. Stashing is the correct way to handle this but it can be inconvenient at times.

推荐答案

您可以尝试:

git update-index --assume-unchanged -- unWantedFile
rm unWantedFile
# work
# ...
# later on
git update-index --no-assume-unchanged -- unWantedFile
git restore unWantedFile

这足以在没有该文件的工作树中工作,同时告诉Git该文件仍然存在.

That should be enough to work in a working tree without that file, while telling Git the file is still there.

它也可以与git update-index --skip-worktree -- unWantedFile一起使用.
我已经写过关于
这两者之间的区别.

It works too with git update-index --skip-worktree -- unWantedFile.
I have written about the difference between the two here.

这篇关于我如何在本地删除文件,而又不将这些删除提交到远程存储库,同时保持“干净"?工作树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆