目标: 本地仓库python_clone中最新代码,推送到远端github
step1: push报错,查询到:该本地仓库python克隆得新仓库python_clone
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main)
$ git push origin main
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: error: refusing to update checked out branch: refs/heads/main
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To D:/py/Python/PycharmProjects/gitproject/python
! [remote rejected] main -> main (branch is currently checked out)
error: failed to push some refs to 'D:/py/Python/PycharmProjects/gitproject/python'
step2:为本地库添加远端仓库=>已经存在“远端仓库”
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main)
$ git remote add origin git@github.com:bai-cao/python_clone.git
error: remote origin already exists.
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main)
$ git remote # 远程仓库别名
origin
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main)
$ git remote -v # 本地仓库克隆而来
origin D:/py/Python/PycharmProjects/gitproject/python (fetch)
origin D:/py/Python/PycharmProjects/gitproject/python (push)
step3:推送当前仓库到另一远端仓库==> mian分支推送失败
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main)
$ git push git@github.com:bai-cao/python_clone.git --all
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 16 threads
Compressing objects: 100% (15/15), done.
Writing objects: 100% (19/19), 5.72 KiB | 1.43 MiB/s, done.
Total 19 (delta 2), reused 3 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (2/2), done.
To github.com:bai-cao/python_clone.git
* [new branch] deom -> deom
* [new branch] django -> django
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:bai-cao/python_clone.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
step4: 先拉取代码获取最新=>不相关分支合并失败(两个独立仓库)
PS:本地代码修改前,先拉取最新代码,以减少冲突
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main)
$ git pull git@github.com:bai-cao/python_clone.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 (from 0)
Unpacking objects: 100% (4/4), 2.38 KiB | 406.00 KiB/s, done.
From github.com:bai-cao/python_clone
* branch HEAD -> FETCH_HEAD
fatal: refusing to merge unrelated histories
step5: pull命令后添加--allow-unrelated-histories =>文件冲突(同一文件两个分支都做了修改)
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main)
$ git pull git@github.com:bai-cao/python_clone.git --allow-unrelated-histories
From github.com:bai-cao/python_clone
* branch HEAD -> FETCH_HEAD
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
step6:手动处理冲突文件
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main|MERGING)
$ git checkout --ours README.md # 保留本地文件
Updated 1 path from the index
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main|MERGING)
$ git add README.md # 提交改动到暂存区
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main|MERGING)
$ git commit -m "merge conflict in README.md" # 暂存区改动提交到HEAD
[main ca7be4e] merge conflict in README.md
29447@LAPTOP-wlh MINGW64 /d/py/Python/PycharmProjects/gitproject/python_clone (main)
$ git pull git@github.com:bai-cao/python_clone.git --allow-unrelated-histories
From github.com:bai-cao/python_clone
* branch HEAD -> FETCH_HEAD
Already up to date.
$ git checkout --theirs README.md # 保留远端版本
* branch HEAD -> FETCH_HEAD??
参考
1、 Git 克隆项目并推送到新的仓库(包含所有的代码和提交记录)-CSDN博客
2、refusing to merge unrelated histories的解决方案(本地/远程)综合-CSDN博客
3、Git合并冲突处理指南:解决二进制文件冲突_git 二进制文件冲突-CSDN博客








网友评论