远程分支替换本地分支

方法一:

git pull” 强制覆盖本地文件 放弃本地修改,使用服务器代码覆盖本地的Git命令如下:

git fetch --all

git reset --hard origin/master 

git pull

上面代码使用master分支覆盖本地代码。如果需要使用其它分支覆盖本地代码,则更改第二条命令的参数。

 

方法二:

git 拉取命令(pull)的标准格式是:

git pull <远程主机名> <远程分支名>:<本地分支名>git pull <远程主机名> <远程分支名>:<本地分支名>

一般我们简写成

git pull

代表从远程分支拉取到当前的本地分支。

有的时候,已经知道远程分支与本地分支有不同的commit,比如本地分支有一个临时的commit,远程分支并没有。是不能简单执行git pull的,会报错。

此时如果只是想放弃本地的临时提交,强制将远程仓库的代码覆盖到本地分支。

就要用到--force参数,强制拉取功能 git manual中关于--force参数的说明

 --force
When git fetch is used with <src>:<dst> refspec it may refuse to update the local branch as discussed in the <refspec> part of the git-fetch(1) documentation. This option overrides that check.

命令格式如下:

git pull --force  <远程主机名> <远程分支名>:<本地分支名>

示例:

git pull --force origin master:master
From https://gitee.com/l0km/myprj
 + e072b6b...d5a5684 master     -> master  (forced update)/** 强制更新 */
warning: fetch updated the current branch head.
fast-forwarding your working tree from
commit e072b6bf59ab4d371b24966005b6d2b40e30bbw5.
Already up-to-date.

方法三: 

删除您的本地分行: git branch -d master 

获取最新的远程分支:  git fetch origin master 

重建基于远程的本地分支: git checkout -b master origin/master 

 

参考链接:

  1. 远程分支替换本地分支
  2. https://www.1024sou.com/article/529973.html

 

posted @   余生以学  阅读(2909)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示