git改变仓库地址
在管理项目的时候,可能会遇到改变项目仓库地址的情况
为了尽可能的模拟实际开发情况 以github和gitee作为远程仓库
前提:同时为github和gitee配置公钥,并访问正常
为什么要进行地址替换,而不是直接下载新的项目,当改变项目仓库的时候,其实代码的没有太多的变动,新项目所要的相关配置,才是最麻烦的,比如maven仓库,idea设置等等
提前说明方法
方法一:直接修改远程仓库地址
# 查看当前远程仓库
$ git remote -v
origin git@github.com:iniwym/git.git (fetch)
origin git@github.com:iniwym/git.git (push)
# 修改远程仓库指向
$ git remote set-url origin git@gitee.com:iniwym/git.git
# 再次查看
$ git remote -v
origin git@gitee.com:iniwym/git.git (fetch)
origin git@gitee.com:iniwym/git.git (push)
此时上传和下载都只同步到 gitee
方法二:直接替换.git
文件
将源项目的.git
文件删除
clone 目标项目
将目标项目的.git
文件 直接拷贝到 源项目文件下
在误删(哭)了
.git
文件的情况下,该方法简单高效
如下是实操过程
确保 github和gitee 公钥 可以正常访问
在github上创建一个项目,并拷贝到本地
在README.md添加一行github
Mobini@iniwu MINGW64 /d/test
$ git clone git@github.com:iniwym/git.git
Cloning into 'git'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), done.
Mobini@iniwu MINGW64 /d/test
$ cd git/
Mobini@iniwu MINGW64 /d/test/git (main)
$ ll
total 1
-rw-r--r-- 1 Mobini 197121 5 Nov 29 21:55 README.md
Mobini@iniwu MINGW64 /d/test/git (main)
$ vim README.md
Mobini@iniwu MINGW64 /d/test/git (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Mobini@iniwu MINGW64 /d/test/git (main)
$ git add .
warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it
Mobini@iniwu MINGW64 /d/test/git (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
Mobini@iniwu MINGW64 /d/test/git (main)
$ git commit -m "github"
[main 170ac4e] github
1 file changed, 3 insertions(+), 1 deletion(-)
Mobini@iniwu MINGW64 /d/test/git (main)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:iniwym/git.git
0dd4a1f..170ac4e main -> main
使用gitee导入github的仓库,模拟更换后的仓库地址
下载gitee相同项目
并在README.md添加一行github基础上
添加一行 gitee
Mobini@iniwu MINGW64 /d/test/gitee
$ git clone git@gitee.com:iniwym/git.git
Cloning into 'git'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 7 (delta 0), reused 7 (delta 0), pack-reused 0
Receiving objects: 100% (7/7), done.
Mobini@iniwu MINGW64 /d/test/gitee
$ cd git/
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ vim README.md
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ git add .
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ git commit -m "gitee"
[main a3d7e72] gitee
1 file changed, 2 insertions(+)
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes | 289.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:iniwym/git.git
170ac4e..a3d7e72 main -> main
方法一 修改远程仓库指向
# 查看当前远程仓库
Mobini@iniwu MINGW64 /d/test/git (main)
$ git remote -v
origin git@github.com:iniwym/git.git (fetch)
origin git@github.com:iniwym/git.git (push)
# 修改远程仓库指向
Mobini@iniwu MINGW64 /d/test/git (main)
$ git remote set-url origin git@gitee.com:iniwym/git.git
# 再次查看
Mobini@iniwu MINGW64 /d/test/git (main)
$ git remote -v
origin git@gitee.com:iniwym/git.git (fetch)
origin git@gitee.com:iniwym/git.git (push)
#更新
Mobini@iniwu MINGW64 /d/test/git (main)
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 269 bytes | 22.00 KiB/s, done.
From gitee.com:iniwym/git
170ac4e..a3d7e72 main -> origin/main
Updating 170ac4e..a3d7e72
Fast-forward
README.md | 2 ++
1 file changed, 2 insertions(+)
Mobini@iniwu MINGW64 /d/test/git (main)
$ vim README.md
Mobini@iniwu MINGW64 /d/test/git (main)
$ git add .
Mobini@iniwu MINGW64 /d/test/git (main)
$ git commit -m "for github project"
[main 67fe3e9] for github project
1 file changed, 1 insertion(+), 1 deletion(-)
Mobini@iniwu MINGW64 /d/test/git (main)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:iniwym/git.git
a3d7e72..67fe3e9 main -> main
结果
方法二
直接替换 .git
文件
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ git remote -v
origin git@github.com:iniwym/git.git (fetch)
origin git@github.com:iniwym/git.git (push)
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ git pull
Already up to date.
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ vim README.md
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ git add .
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ git commit -m "for gitee project"
[main 7be8859] for gitee project
1 file changed, 3 insertions(+), 1 deletion(-)
Mobini@iniwu MINGW64 /d/test/gitee/git (main)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 299 bytes | 299.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:iniwym/git.git
170ac4e..7be8859 main -> main
结果
end