git远程仓库已有提交文件,如何提交本地仓库覆盖远程仓库

在这里插入图片描述


首次提交到远程仓库的正常流程:

  1. 新建(初始化本地仓库)

    命令:git init

    结果:根目录出现 .git 文件(即本 地仓库)

  2. 跟踪文件

    命令:git add .

    是跟踪根目录下所有文件

  3. 提交到本地仓库

    命令:git commit -m “这里可以写备注以便代码管理”

  4. 连接远程仓库

    命令:git remote add origin httpXXXXX.git

    origin 的意思是指“远程仓库”,就是远程仓库链接的别名,是在 clone 一个托管在 Github 上代码库时,git 默认创建的指向这个远程代码库的标签,origin 指向的就是本地的代码库托管在 Github 上的版本。

  5. 提交到本地仓库

    命令:git push -u origin master

    提交到远程仓库的 master 分支

报错出现仓库提交失败信息如下:

To https://gitee.com/XXXXXX.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://gitee.com/XXXXXX.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 push -f 远程仓库名  远程仓库分支

使用本地仓库文件覆盖远程仓库,即强制推送。

以上代码中的远程仓库名分支,获取方式如下:

  • 远程仓库名,查看:git remote show

  • 远程仓库分支,查看:git branch(带有星号的分支,代表当前分支)

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值