跳到主要内容

Git仓库迁移指南

从Gitee迁移到GitHub

1. 克隆所有分支

# 克隆仓库
git clone git@gitee.com:username/repo-name.git
cd repo-name

# 检出所有远程分支
git branch -a # 查看所有分支
for branch in $(git branch -r | grep -v HEAD | sed 's/origin\///'); do
git checkout -b $branch origin/$branch
done

2. 更换远程仓库

# 移除原远程仓库
git remote remove origin

# 添加GitHub作为新远程仓库
git remote add origin https://github.com/username/repo-name.git

# 推送所有分支并设置跟踪
for branch in $(git branch --format='%(refname:short)'); do
git checkout $branch
git push -u origin $branch
done

3. 修改历史提交邮箱(用于GitHub Contributions)

# 修改所有历史提交的邮箱
git filter-branch --env-filter '
if [ "$GIT_COMMITTER_EMAIL" = "old-email@example.com" ]
then
export GIT_COMMITTER_NAME="Your Name"
export GIT_COMMITTER_EMAIL="github-email@example.com"
fi
if [ "$GIT_AUTHOR_EMAIL" = "old-email@example.com" ]
then
export GIT_AUTHOR_NAME="Your Name"
export GIT_AUTHOR_EMAIL="github-email@example.com"
fi
' --tag-name-filter cat -- --branches --tags

# 强制推送覆盖远程历史
git push origin --all --force

强制推送 - 会覆盖远程历史,确保无他人协作

4. 常用检查命令

# 查看分支状态
git branch -a # 所有分支
git branch -vv # 分支跟踪状态

# 查看提交历史
git log --oneline -10 # 简洁历史
git log --format="%h %an <%ae>" -10 # 查看提交邮箱

# 查看远程仓库
git remote -v # 远程仓库列表