首页 > 其他分享 >Git错误non-fast-forward后的冲突解决

Git错误non-fast-forward后的冲突解决

时间:2023-03-22 16:01:31浏览次数:33  
标签:non git remote fast Git master branch push


当要push代码到git时,出现提示:

error:failed to push some refs to ...

Dealing with “non-fast-forward” errors
From time to time you may encounter this error while pushing:



  1. $ git push origin master  
  2. To ../remote/  
  3.  ! [rejected]        master -> master (non-fast forward)  
  4. error: failed to push some refs to '../remote/'  



To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.
This error can be a bit overwhelming at first, do not fear. Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch. You can remedy this by fetching and merging the remote branch, or using pull to perform both at once.
In other cases this error is a result of destructive changes made locally by using commands like git commit --amend or git rebase. While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don’t force-push.

 

问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。于是你有2个选择方式:

1,强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容

git push -f

2,先把git的东西fetch到你本地然后merge后再push

$ git fetch

$ git merge

这2句命令等价于



  1. $ git pull  



可是,这时候又出现了如下的问题:

 

上面出现的 [branch "master"]是需要明确(.git/config)如下的内容
[branch "master"]
    remote = origin

    merge = refs/heads/master

这等于告诉git2件事:

1,当你处于master branch, 默认的remote就是origin。

2,当你在master branch上使用git pull时,没有指定remote和branch,那么git就会采用默认的remote(也就是origin)来merge在master branch上所有的改变

如果不想或者不会编辑config文件的话,可以在bush上输入如下命令行:



  1. $ git config branch.master.remote origin  
  2. $ git config branch.master.merge refs/heads/master  



之后再重新git pull下。最后git push你的代码吧。it works now~

标签:non,git,remote,fast,Git,master,branch,push
From: https://blog.51cto.com/u_2650279/6142860

相关文章

  • 'NoneType' object has no attribute 'name'
     某个外键允许设置为空,null=True,blank=True,同时on_delete=models.SET_NULL当在Djangoadmin后台自定义标签的时候进行判断的时候,如果默认为空,提交数据库后会出现'NoneT......
  • fastadmin添加自定义button按钮
    fastadmin添加自定义button按钮  fastadmin添加自定义按钮1、首先在初始化表格参数配置中添加URL,添加所需要的按钮地址 2、然后再初始化表格中,这这个位......
  • git 相关知识
    清除缓存清除缓存的账号密码等信息,下次gitpush或gitpull的时候需要重新输入账号密码gitconfig--local--unsetcredential.helpergitconfig--global--unset......
  • Git 入门教程之分支策略
    默认情况下合并分支常常直接使用​​gitmerge​​​命令,是最方便快速的合并方法.其实这种情况下​​git​​​采用的是​​fastforward​​​模式,特点是删除分支......
  • git一个本地仓库,配置两个远程仓库
    git一个本地仓库,配置两个远程仓库,gitpush的时候会推送到两个远程仓库方法:找到并打开git的配置.git/config。把url改为如下 ......
  • 【git】解决gogs迁移后报错remote: hooks/pre-receive: line 2: /data/gogs/gogs: No
    【gogs迁移后错误提示】remote:hooks/pre-receive:line2:/data/gogs/gogs:NosuchfileordirectoryTohttp://192.168.1.129:53000/yin.xia/sgs-vue.git![rem......
  • Git 记录在非工作分支中 做了开发工作 - 挽回
    之前切到了测试分支“develop”,由于改bug比较急一时忘记了是在哪个分支的事。当要推送到自己的分支的时候突然发现目前所有的工作竟然在测试分支上,我的天啊一时懵了,想......
  • 把项目放在github
    一.右上角加号新建一个newrepository填入时license下拉框选择Apache为需要验证,MIT为公开随意拉取二.gitclone拉取代码报错fatal:unabletoaccess'https://github.c......
  • 解决“fatal: ‘origin‘ does not appear to be a git repository...”
    当使用Git进行代码push提交时,出现报错信息“fatal:'origin'doesnotappeartobeagitrepository...”,$gitpush-uoriginmasterfatal:'origin'doesnotappear......
  • IDEA集成Gitee
    配置Git在设置里面点击Git,点击选择git安装目录下的bin目录下的git.exe,点击Test,出现版本号,证明配置成功。配置码云在设置里面按照下图步骤,即可成功配置码云安装Git......