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(带有星号的分支,代表当前分支)

猜你喜欢

转载自blog.csdn.net/puhuihui/article/details/125163407