将包含所有分支和完整历史的Git存储库从Bitbucket移动到GitHub的最佳方法是什么?

是否有我必须使用的脚本或命令列表?


当前回答

我做了下面的Bash脚本,以克隆我所有的Bitbucket(用户)存储库到GitHub作为私有存储库。


要求:

jq(命令行JSON处理器)| macOS: brew install jq

步骤:

转到个人访问令牌并创建一个访问令牌。我们只需要“回购”范围。 将move_me.sh脚本保存在工作文件夹中,并根据需要编辑该文件。 别忘了chmod 755 快跑!。/ move_me.sh 享受你节省下来的时间吧。


注:

它会在脚本所在的目录(你的工作目录)中克隆Bitbucket存储库。 这个脚本不会删除你的Bitbucket存储库。


需要转移到GitHub上的公共存储库?

找到并更改下面的“private”:true为“private”:false。

移动组织的存储库?

请查看开发人员指南。还需要几次编辑。


快乐的运动。

#!/bin/bash

BB_USERNAME=your_bitbucket_username
BB_PASSWORD=your_bitbucket_password

GH_USERNAME=your_github_username
GH_ACCESS_TOKEN=your_github_access_token

###########################

pagelen=$(curl -s -u $BB_USERNAME:$BB_PASSWORD https://api.bitbucket.org/2.0/repositories/$BB_USERNAME | jq -r '.pagelen')

echo "Total number of pages: $pagelen"

hr () {
  printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
}

i=1

while [ $i -le $pagelen ]
do
  echo
  echo "* Processing Page: $i..."
  hr
  pageval=$(curl -s -u $BB_USERNAME:$BB_PASSWORD https://api.bitbucket.org/2.0/repositories/$BB_USERNAME?page=$i)

  next=$(echo $pageval | jq -r '.next')
  slugs=($(echo $pageval | jq -r '.values[] | .slug'))
  repos=($(echo $pageval | jq -r '.values[] | .links.clone[1].href'))

  j=0
  for repo in ${repos[@]}
  do
    echo "$(($j + 1)) = ${repos[$j]}"
    slug=${slugs[$j]}
  git clone --bare $repo
  cd "$slug.git"
  echo
  echo "* $repo cloned, now creating $slug on GitHub..."
  echo

  read -r -d '' PAYLOAD <<EOP
  {
    "name": "$slug",
    "description": "$slug - moved from Bitbucket",
    "homepage": "https://github.com/$slug",
    "private": true
  }
  EOP

  curl -H "Authorization: token $GH_ACCESS_TOKEN" --data "$PAYLOAD" \
      https://api.github.com/user/repos
  echo
  echo "* mirroring $repo to GitHub..."
  echo
  git push --mirror "git@github.com:$GH_USERNAME/$slug.git"
  j=$(( $j + 1 ))
  hr
  cd ..
  done
  i=$(( $i + 1 ))
done

其他回答

最简单的方法:

git remote rename origin repo_bitbucket

git remote add origin https://github.com/abc/repo.git

git push origin master

一旦推送到GitHub成功,通过运行以下命令删除旧的远程:

git remote rm repo_bitbucket

我做了下面的Bash脚本,以克隆我所有的Bitbucket(用户)存储库到GitHub作为私有存储库。


要求:

jq(命令行JSON处理器)| macOS: brew install jq

步骤:

转到个人访问令牌并创建一个访问令牌。我们只需要“回购”范围。 将move_me.sh脚本保存在工作文件夹中,并根据需要编辑该文件。 别忘了chmod 755 快跑!。/ move_me.sh 享受你节省下来的时间吧。


注:

它会在脚本所在的目录(你的工作目录)中克隆Bitbucket存储库。 这个脚本不会删除你的Bitbucket存储库。


需要转移到GitHub上的公共存储库?

找到并更改下面的“private”:true为“private”:false。

移动组织的存储库?

请查看开发人员指南。还需要几次编辑。


快乐的运动。

#!/bin/bash

BB_USERNAME=your_bitbucket_username
BB_PASSWORD=your_bitbucket_password

GH_USERNAME=your_github_username
GH_ACCESS_TOKEN=your_github_access_token

###########################

pagelen=$(curl -s -u $BB_USERNAME:$BB_PASSWORD https://api.bitbucket.org/2.0/repositories/$BB_USERNAME | jq -r '.pagelen')

echo "Total number of pages: $pagelen"

hr () {
  printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
}

i=1

while [ $i -le $pagelen ]
do
  echo
  echo "* Processing Page: $i..."
  hr
  pageval=$(curl -s -u $BB_USERNAME:$BB_PASSWORD https://api.bitbucket.org/2.0/repositories/$BB_USERNAME?page=$i)

  next=$(echo $pageval | jq -r '.next')
  slugs=($(echo $pageval | jq -r '.values[] | .slug'))
  repos=($(echo $pageval | jq -r '.values[] | .links.clone[1].href'))

  j=0
  for repo in ${repos[@]}
  do
    echo "$(($j + 1)) = ${repos[$j]}"
    slug=${slugs[$j]}
  git clone --bare $repo
  cd "$slug.git"
  echo
  echo "* $repo cloned, now creating $slug on GitHub..."
  echo

  read -r -d '' PAYLOAD <<EOP
  {
    "name": "$slug",
    "description": "$slug - moved from Bitbucket",
    "homepage": "https://github.com/$slug",
    "private": true
  }
  EOP

  curl -H "Authorization: token $GH_ACCESS_TOKEN" --data "$PAYLOAD" \
      https://api.github.com/user/repos
  echo
  echo "* mirroring $repo to GitHub..."
  echo
  git push --mirror "git@github.com:$GH_USERNAME/$slug.git"
  j=$(( $j + 1 ))
  hr
  cd ..
  done
  i=$(( $i + 1 ))
done

根据@MarMass的回答,如果GitHub导入器不断将你重定向到身份验证屏幕,你需要在BitBucket中创建一个应用程序密码,以便导入你的私有存储库:

去Bitbucket >个人设置>应用程序密码。 创建具有存储库读访问权限的应用程序密码。 当在GitHub导入器中提示输入您的用户名/密码时,输入您的BitBucket用户名,并将上面创建的令牌作为密码。

在设法解决身份验证问题后,我的导入也出现了以下消息:“有一个错误将提交推到GitHub.”。

这里的问题是,至少对我来说,我的GitHub帐户被设置为“阻止暴露我的电子邮件的命令行推送”,而我试图从Bitbucket导入的存储库包含来自我个人电子邮件地址的提交。在暂时禁用这个设置(GitHub >设置>电子邮件)后,我就可以去了。

你可以参考GitHub页面“复制存储库”

它使用:

Git克隆——镜像:克隆每个引用(提交,标签,分支) Git push—mirror:用于推送所有内容

这将给予:

git clone --mirror https://bitbucket.org/exampleuser/repository-to-mirror.git
# Make a bare mirrored clone of the repository

cd repository-to-mirror.git
git remote set-url --push origin https://github.com/exampleuser/mirrored
# Set the push location to your mirror

git push --mirror

正如L S在评论中指出的那样:

使用MarMass描述的GitHub导入代码功能更容易。 参见https://github.com/new/import 除非…您的回购包括一个大文件:问题是,导入工具将失败,没有一个明确的错误消息。只有GitHub支持才能诊断出发生了什么。

Se移动存储库从Bitbucket到GitHub。

这帮助我从一个Git提供者切换到另一个提供者。最后,所有提交都在目标Git存储库中。简单明了。

Git远程重命名原点bitbucket git远程添加origin https://github.com/edwardaux/Pipelines.git Git push origin master 一旦我很高兴在GitHub上的推送成功了,我就可以了 通过以下命令删除旧的远程: Git远程rm bitbucket