Fork远程仓库到私有仓库

young 372 2023-08-30

工作中需要将github一个开源项目迁移至内网git上,进行二次开发并能更新到github最新代码变动。

  1. 拉取github仓库
git clone git@github.com/xxxxxx
  1. 进入仓库目录
cd xxxxxx
  1. 拉取全部分支
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
  1. 重命名远程名称为upstream
git remote rename origin upstream
  1. 在内网git上创建项目同名仓库

  2. 添加新的远程配置

git remote add origin git@<git-server>/<path-to-your-repo>.git
# 如果git无法配置ssh,在使用http的情况下
git remote add origin http://<git-server>/<path-to-your-repo>.git
# 如果路径写错了,需要删除,删除后重新添加即可
git remote remove origin
  1. 推送所有分支及tag
git push -u origin --all
git push -u origin --tags

当远程仓库更新时

git pull upstream master
git push origin main