gpt4 book ai didi

git - 用不同的远程分支替换 git 存储库中的所有现有文件,除了 .gitignore 中的文件

转载 作者:太空狗 更新时间:2023-10-29 14:17:07 26 4
gpt4 key购买 nike

我有几个存储库,我总是必须 merge 一个 core存储库到。 core存储库设置为 remote每个存储库中的存储库。

通常,我更新存储库的工作流程是这样的:

git reset --hard origin/master
git pull origin master
git fetch core master

然后我做一个 git merge --squash core/master并在将存储库推回 remote 之前修复所有冲突.

这很好,只是有点多余,因为除了 .gitignore 中的所有内容之外每个存储库的文件,这些存储库中的所有内容在技术上应该与 core 相同存储库。

随着存储库数量的增加,我想知道有什么更有效的方法来提取 core分支到这些存储库时,我需要替换所有现有文件,但在 .gitignore 中特别提到的文件除外。对于其中的每一个,同时保持 git 历史和日志的完整性。

最佳答案

您可以结合使用 git 和 bash 来完成。我已经编写了一个示例脚本来展示它是如何完成的。您可以随时对其进行修改并使其变得更好。我也提供了一些解释。此文件称为 adder.sh

#!/bin/bash
# $1 -> Files from the branch you want (core)
# $2 -> Branch you want to merge into (master)

git checkout $2
git diff --name-status $1..$2 | grep '^\(D\|M\)\s*' | cut -f2 > ~/.dummy
git checkout $1 -- $(cat ~/.dummy)
git add .

要调用它,只需使用$ sh adder.sh core master。在此之后,所有来自 core 分支的新添加和修改的文件都将添加到 master 存储库中。使用 git status,您可以查看新内容,然后相应地提交和推送。

$ git commit -m "Skipping Conflicts"
$ git push

对其工作原理的一些解释:

$ git diff --name-status master..core

产生以下输出:

M       public/stylesheets/main.css              # Modified
D public/templates/createUser.html # Present in core branch and not master (new file)
A public/templates/dashboard.html # Present in master and not in the core branch (don't touch)

因此编写一个简单的正则表达式来仅选择修改后的文件和新文件并将其修改为适当的格式,然后将其存储在临时文件中。

$ cat ~/.dummy

public/templates/createUser.html
public/stylesheets/main.css

然后我们需要将这些文件添加到我们当前的分支,所以我们使用 git checkout。参见 this answer了解如何使用 git checkout 执行此操作。


还有另一种方法可以做到这一点。官方方式,使用git rerere。来自 the man page :

In a workflow employing relatively long lived topic branches, the developer sometimes needs to resolve the same conflicts over and over again until the topic branches are done (either merged to the "release" branch, or sent out and accepted upstream).

This command assists the developer in this process by recording conflicted automerge results and corresponding hand resolve results on the initial manual merge, and applying previously recorded hand resolutions to their corresponding automerge results.

Note: You need to set the configuration variable rerere.enabled in order to enable this command.

This article对命令及其用例进行了适当的概述。

关于git - 用不同的远程分支替换 git 存储库中的所有现有文件,除了 .gitignore 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46204572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com