git 常见问题处理方法

最近自动化编写过程,对于代码提交,遇到了不少问题,主要是 通过百度进行解决,以下是记录当时百度到的解决方法。

问题1:本地修改未commit和push,从远程分支pull下来的代码时,提示本地会被覆盖

问题2:git commit到本地仓库后,没有push到远程仓库,如何撤销本地commit这笔代码

问题1:本地修改未commit和push,从远程分支pull下来的代码时,提示本地会被覆盖

报错内容:

14:44:03.846: [python_web_sysmanagement] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false -c core.commentChar= pull --no-stat -v --progress origin main
POST git-upload-pack (315 bytes)
From https://github.com/superlhy-2015/python_web_sysmanagement
 * branch            main       -> FETCH_HEAD
 = [up to date]      main       -> origin/main
error: Your local changes to the following files would be overwritten by merge:
    Common/plugs/basepage.py
Please commit your changes or stash them before you merge.

Aborting

这种情况下,如何保留本地的修改同时又把远程的合并过来呢?

处理一:保留本地,并且将远程的合并到本地

    1、将本地修改的内容,以快照方式进行保存在本地    
    git stash          

    2、去远程分支拉取代码,pull完之后,发现之前修改的代码并没有保留
    git pull origin master 或 git pull origin main   

    3、执行你去本地看会发现发生冲突的本地修改还在
    git stash pop            

执行上述执行中,遇到了一个问题:git pull origin master提示找不到远程分支

$ git pull origin master 

fatal: couldn‘t find remote ref master

此时,查看项目的主线不是叫master,而是叫main,所以执行$ git pull origin main,然后拉取代码成功

 处理二:不保留本地修改,将远程分支代码合并到本地

    1、将本地的状态恢复到上一个commit id 
    git reset --hard 

    2、拉取远程的代码直接覆盖本地
    git pull origin master

以上就是解决方法,感谢作者【Git】pull遇到错误:error: Your local changes to the following files would be overwritten by merge:_转身雪人的博客-CSDN博客

问题2:git commit到本地仓库后,没有push到远程仓库,如何撤销本地commit这笔代码

撤销commit一般用git reset ,语法如下:

git reset [ --mixed | --soft | --hard] [<commit ID>]

1.使用参数--mixed(默认参数),如git reset --mixed <commit ID>或git reset <commit ID>

撤销git commit,撤销git add,保留编辑器改动代码

2.使用参数--soft,如git reset --soft<commit ID>

撤销git commit,不撤销git add,保留编辑器改动代码

3.使用参数--hard,如git reset --hard <commit ID>——此方式非常暴力,全部撤销,慎用

撤销git commit,撤销git add,删除编辑器改动代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值