代码之家  ›  专栏  ›  技术社区  ›  Raymundo Hernández

在合并请求中排除文件

  •  0
  • Raymundo Hernández  · 技术社区  · 2 年前

    我在GitLab中有一个项目,它由3个分支组成,每个分支都对应一个部署环境:测试、测试和生产。在每个分支中,我都有一个Dockerfile,它的内容因分支而异。我需要在创建合并请求时排除该Dockerfile,以便每个分支的Dockerfiles保持不同。这可能吗?

    我尝试将.gitattributes文件与此内容一起使用,但不起作用:

    Dockerfile merge=ours
    
    1 回复  |  直到 2 年前
        1
  •  -1
  •   eftshift0    2 年前

    如果文件不随时间变化 ,你可以通过做第一件事来完成它 合并 在两个分支之间进行(真正的合并),然后将文件更正回您正在处理的分支上的文件……之后,当您与同一分支合并时,git将不会修改该分支的文件。

    所以……假设你有这个:

    * III Sixth commit in main branch... I also modify hello.txt (main)
    * HHH Fifth commit in main branch
    * GGG Fourth commit in main branch
    | * FFF Third commit in branch2... I change file hello.txt (branch2)
    | * EEE Second commit in branch2
    | * DDD First commit in branch2
    |/
    * CCC Third commit
    * BBB Second commit
    * AAA First commit
    

    所以,我们跑

    git checkout main
    git merge --no-commit branch2
    

    无论是否存在冲突,你都希望 hello.txt 回到原来的样子 main 所以你会:

    git restore --source=HEAD -- hello.txt
    git commit "merging branch2"
    

    有了这个,我们就可以拿回文件并完成提交,现在我们有了这个:

    * JJJ Merging branch2 (HEAD -> main)
    |\
    * | III Sixth commit in main branch... I also modify hello.txt
    * | HHH Fifth commit in main branch
    * | GGG Fourth commit in main branch
    | * FFF Third commit in branch2... I change file hello.txt (branch2)
    | * EEE Second commit in branch2
    | * DDD First commit in branch2
    |/
    * CCC Third commit
    * BBB Second commit
    * AAA First commit
    

    从这一刻起, 只要没有变化 hello.txt 在里面 branch2 ,你可以肯定git会 将任何更改带入 hello.txt 合并时 分支2 进入之内 主要的 支。