【问题标题】:Maintain same commit history in multiple repositories在多个存储库中保持相同的提交历史
【发布时间】:2021-03-14 00:03:47
【问题描述】:

我有一个 ADO 存储库和一个 GitHub 存储库。两个存储库都有一个具有相同提交历史的“主”分支。 当我完成从任何分支到“master”分支的拉取请求并向 ADO 存储库中的“master”分支添加新提交时,我必须将相同的提交添加到 GitHub 存储库,反之亦然,这样提交历史记录将保持不变相同的。那可能吗?请举个例子。

更新:

我已经尝试了建议的解决方案:

git clone https://msit.visualstudio.com/<org>/_git/<repo-name>
git config --global user.name "abc@xyz.com"
git config --global user.name "ABC DEF"
git checkout master
git add .
git commit -m "abc"
git push https://github.com/xyz/project.git

在尝试相同时,我收到以下错误:

fatal: Cannot prompt because user interactivity has been disabled.
fatal: could not read Username for 'https://msit.visualstudio.com': terminal prompts disabled
Switched to a new branch 'master'
Branch 'master' set up to track remote branch 'master' from 'origin'.

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

【问题讨论】:

  • 如果一个不是另一个的镜像,虽然有可能,但工作量很大,而且很可能会损坏。如果一个人永远是另一个人的严格镜子,你会过得更好。
  • 能否请您告诉我您的意思是严格镜像对方?通过具有相同的提交历史,它应该是另一个正确的严格镜像?
  • “严格镜像”是指您只修改一个存储库并将其内容同步到另一个存储库,而不允许第二个存储库接收第一个存储库尚未进行的任何修改。跨度>
  • @user989988 无法获取您的最新信息。只是想检查以下答案是否对您有帮助?如果是,您可以accept the answer,这也可以使与您有相同困惑的其他人受益,我们可以存档此线程。另外,如果仍有任何疑问,请随时在下面发表评论:-)

标签: git github azure-devops git-commit


【解决方案1】:

您可以尝试以下方法来同步 azure devops repo 和 github repo。

首先创建一个以 github repo 为源的构建管道。然后在管道中添加一个 cmd 任务,将更新从 github repo 推送到 azure devops repo,请参见下面的命令。

git clone https://github.com/XXX/XXX.git
git config --global user.name "XXX"
git checkout master
git add .
git commit -m "abc"
git push https://{AzureDevopsPAT}@dev.azure.com/{org}/{pro}/_git/xxx.git

为了每次在主分支上发生代码更改时启动构建,我们需要转到触发选项卡并进行如下设置:

以上是关于将 github repo 同步到 azure devops repo,关于将 azure devops repo 同步到 github repo,您​​只需要以同样的方式创建另一个以 azure repo 作为源的构建管道,详细信息可以参考这里answer.

【讨论】:

  • 谢谢!你能帮我理解你提到的git命令吗?假设这是我的 GitHub 存储库 URL:github.com/xyz/project.git,这是我的 ADO 存储库 URL:microsoftit.visualstudio.com/DefaultCollection/OneITVSO/_git/…。我在 GitHub repo master 分支中做了一些更改。 1) 克隆 GitHub repo 2) checkout master 3) 添加新提交 4) 将更改推送到 ADO repo master 分支。是这个意思吗? AzureDevopsPAT 代表什么?
  • 我尝试了您的解决方案,但出现错误。请查看我更新的问题。
  • @user989988 您需要在推送 url 中提供您的 github PAT,例如 git push https://{yourPAT}@github.com/xxx/yyy.git 。这是关于 azure devops pat 的 document
【解决方案2】:

同意休的观点

  1. Go Project settings --> Repositories --> 点击你要操作的Repos -->勾选服务账号{project name} Build Service (xxx),确保权限设置为允许。您可以查看下面的图片。

  1. 在 Azure DevOps 中创建 yaml 管道,我们需要选择 GitHub 作为代码资源,然后选择 GitHub 存储库,它会将 yaml 文件保存在 GitHub Repo 中而不是 Azure DevOps 存储库中,然后我们可以配置 CI 触发器。

  1. 将脚本添加到 Sync GitHub Repo

它将 yml 文件保存在 GitHub 存储库中,然后将其同步到 Azure DevOps 存储库

 trigger:
    - master
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - checkout: self
      persistCredentials: true    #Allow scripts to access the system token
      clean: true 
    
    - task: Bash@3
      name: SyncRepo
      inputs:
        targetType: 'inline'
        script: |
          git config --global user.name "{email}"
          git checkout master
          git add .
          git commit -m "Sync GitHub Repo to Azure DevOps Repo"
          git push https://{PAT}@dev.azure.com/{org name}/{project name}/_git/{repo name}

结果:

另外,我们可以试试AccessToken,更多细节可以参考这个answer

trigger:
  branches:
    include:
    - '*'
variables:
  system_accesstoken: $(System.AccessToken)

pool:
  vmImage: 'ubuntu-latest'

steps:
- checkout: self
  persistCredentials: true    #Allow scripts to access the system token
  clean: true 

- task: Bash@3
  name: SyncRepo
  inputs:
    targetType: 'inline'
    script: |
      git config --global user.email "xxxx@xxx.com"
      git config --global user.name "sormita"
      git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
      git remote add vsts https://xxx.visualstudio.com/xxx/_git/xxxx
      git branch -r | grep -v '\->' | while read remote; do git -c http.extraheader="AUTHORIZATION: bearer $(system_accesstoken)" push -u vsts "${remote#origin/}"; done

【讨论】:

    猜你喜欢
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    相关资源
    最近更新 更多