本文记录一个比拟不罕用的 git 操作,场景为将一个库的某个分支,笼罩另一个库的某个分支。
. git_test, branch dev : github.com:xiangbin1997/git_test.git
. git_test1, branch dev : github.com:xiangbin1997/git_test1.git
用 git_test1,branch dev 替换 git_test, branch dev
clone git_test1 的 dev 分支
[postgres@hgcndn git_test1_dev]$ git clone -b dev git@github.com:xiangbin1997/git_test1.git
Cloning into 'git_test1'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 16 (delta 1), reused 11 (delta 0), pack-reused 0
Receiving objects: 100% (16/16), done.
Resolving deltas: 100% (1/1), done.
[postgres@hgcndn git_test1_dev]$ cd git_test1/
查看.git/config
[postgres@hgcndn git_test1]$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:xiangbin1997/git_test1.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "dev"]
remote = origin
merge = refs/heads/dev
删除 git_test1,dev 分支对应的 origin
[postgres@hgcndn git_test1]$ git remote rm origin
[postgres@hgcndn git_test1]$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[branch "dev"]
批改 git_test1,dev 分支对应的 origin,指向 git_test
[postgres@hgcndn git_test1]$ git remote add origin git@github.com:xiangbin1997/git_test.git
[postgres@hgcndn git_test1]$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[branch "dev"]
[remote "origin"]
url = git@github.com:xiangbin1997/git_test.git
fetch = +refs/heads/*:refs/remotes/origin/*
强制 push 到 git_test 的 dev 分支下
[postgres@hgcndn git_test1]$ git push origin dev --force
Counting objects: 16, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (16/16), 1.97 KiB | 0 bytes/s, done.
Total 16 (delta 1), reused 13 (delta 1)
remote: Resolving deltas: 100% (1/1), done.
To git@github.com:xiangbin1997/git_test.git
+ 5a13464...418bf50 dev -> dev (forced update)
阐明,如果不强制 push 会报错
[postgres@hgcndn git_test1]$ git push origin dev
To git@github.com:xiangbin1997/git_test.git
! [rejected] dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:xiangbin1997/git_test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
参考
git-move-repository