首先说明,这个操作是把远程仓库覆盖掉了。
条件:
1、有远程仓库 remote_url
2、本地代码有修改,但是没有关联远程仓库
下面是我的操作
1、在本地项目初始化
git init
2、添加远程地址
git remote add origin remote_url
3、将文件添加
git add .
4、提交
git commit -m "初始化"
5、此时执行 git push origin master 会报错
To https://gitee.com/12345/t.git ! [rejected] master -> master (fetch first) error: failed to push some ref to 'https://gitee.com/12345/t.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 integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
然后我执行了
git pull origin master
然后出现下面这个
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 1.72 KiB | 251.00 KiB/s, done.
From https://gitee.com/12345/t
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
然后,从网上找到了下面这句 去执行
git pull origin master --allow-unrelated-histories
就出现了下面这段
From https://gitee.com/123456/t
* branch master -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
我选择了 :git config pull.rebase true
git config pull.rebase true
到这里再重新进行3、4步操作
最后执行
git push origin master
成功推送