忽略git中文件的特定更改,而不是整个文件 [英] Ignore specific changes to a file in git, but not the entire file

查看:140
本文介绍了忽略git中文件的特定更改,而不是整个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在git存储库中有一个文件,其中有一个本地更改。我想让git永远忽略本地更改,但不是文件。特别是,


  • 如果除了这个改变,文件还没有被触及, git add。
  • 同样, git commit -a 不应该提交它。

  • 如果我对文件进行了额外的更改,我应该能够分阶段执行更改 - 但我忽略的更改不应该被暂存并提交。
  • >


有没有办法做到这一点?做一些研究,我读了关于污迹/清洁周期,如果我正确地阅读,那么文件将被标记为不变,

  • 在结帐时,我所做的更改将被覆盖,然后脚本会自动重新应用更改,然后将该文件标记为未更改

  • 尽管(我是C#和Java经验的实习生),但我对git和脚本非常陌生,所以如果这是我需要做的,你可以请发布详细的指导或指导如何设置涂抹/清洁周期的教程吗?



    背景: 我希望我的工作分支与主干不同步。有一个低优先级的错误只影响开发机器,所以不是修复它,我们只是评论出有问题的代码。很明显,我们不希望这段代码从生产中被移除,它的工作很好。

    ,你描述的情况是这样的:你想维护一个不同于本地存储库的工作目录。



    这不可取;因为这些更改没有提交,所以如果文件被意外删除或以您不想要的方式更改,您将无法追索。

    因此,建议您事实上提交所有更改。如果你想分开这些更改,你可以很容易地使用分支来做到这一点。示例

      git checkout -b新分支
    #现在您可以更改文件而不会影响主
    回显栏>> foo
    git add
    git commit
    #and back to master
    git checkout master


    I have a file in a git repository that has a local change on it. I want to have git ignore the local change forever, but not the file. In particular,

    • If the file isn't touched besides this change, git add . should never stage it.
    • Likewise, git commit -a shouldn't commit it.
    • If I ever make an additional change to the file, I should be able to stage and commit that change - but the change I'm ignoring should not be staged and committed.

    Is there a way to do this? Doing some research, I read about "smudge/clean cycles," where, if I read correctly,

    1. the file would be marked as unchanged,
    2. the change I made would be overwritten when I checkout,
    3. and then a script would automatically reapply the change and then mark the file as unchanged again.

    I am very new to git and scripting, though (I'm an intern with C# and Java experience), so if that's what I need to do, can you please post detailed directions or a link to a tutorial on how to set a smudge/clean cycle up?

    Background: I want my working branch to be out of sync with the trunk. There is a low priority bug that only affects development machines, so rather than fix it, we're just commenting out the offending code. Obviously, we don't want this code to be removed from production, where it works just fine.

    解决方案

    As I see it, the situation you are describing is this: you want to maintain a working directory that is different from the local repository.

    This is not advisable; because these changes are not committed you will have little recourse if a file is accidentally deleted or changed in a way you do not want.

    Therefore it is recommended that you in fact commit all your changes. If you would like to separate these changes you can easily do that using a branch. Example

    git checkout -b new-branch
    # now you can change the file without affecting master
    echo bar >> foo
    git add
    git commit
    # and back to master
    git checkout master
    

    这篇关于忽略git中文件的特定更改,而不是整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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