在我们平时写程序或者管理项目的时候经常会改变本地git仓库里的文件,这样的话会使得github上的远程仓库和本地的仓库版本不同步,于是就需要我们使用强制命令去进行覆盖了。

强制push提交覆盖到远程仓库

首先我们使用git push提交本地仓库到github远程仓库的时候会有如下的问题:
fatal: The current branch wangxiao has no upstream branch.
To push the current branch and set the remote as upstream
git push --set-upstream origin master
或者:
error: failed to push some refs to ‘https://github.com/…’
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull …’) before pushing again.
于是我们使用强制命令:(master可以根据子的需要自定义,就是当前分支在远程分支对应的名称)

git push -u origin master -f

强制pull拉取覆盖本地仓库

在我们将远程仓库使用git pull拉取到本地的时候,又会出现这样的问题:

当前分支没有跟踪信息。
请指定您要合并哪一个分支。
详见 git-pull(1)。

git pull <远程> <分支>

于是可以执行:

git pull origin master

如果使用这个命令还是出现如下的问题:
$ git pull origin master
来自 https://github.com/itaken/python-login-demo

  • branch master -> FETCH_HEAD
    fatal: 拒绝合并无关的历史

可以使用:

git pull origin master --allow-unrelated-histories  

出现如下提示就表示合并成功了:

来自 https://github.com/itaken/python-login-demo

  • branch master -> FETCH_HEAD
    Merge made by the ‘recursive’ strategy.
    LICENSE | 21 +++++++++++++++++++++
    1 file changed, 21 insertions(+)
    create mode 100644 LICENSE

正确的使用方法

强制推拉当然是不太好的,特别是我们进行团队协作的时候
我们正确的步骤一般都是先pull合并远端再push提交:

git pull origin master
git push -u origin master
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐