主页

解决本地有代码没有提交,就直接拉取远程的代码,而不能再重新push/pull代码

2023-10-21 06:29PM

本地有代码没有提交,就直接拉取远程的代码,而不能再重新push/pull代码

使用 git pull origin main:main 就被拒绝:

$ git pull origin main:main
Username for 'https://github.com': xxxxx
Password for 'https://xxxxx@github.com': 
From https://github.com/sg552/baogongpingtai-pc
 ! [rejected]        main       -> main  (non-fast-forward)

使用 git push origin main:main 就报错:

$ git push origin main:main
Username for 'https://github.com': 347430
Password for 'https://347430@github.com': 
To https://github.com/sg552/baogongpingtai-pc.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/sg552/baogongpingtai-pc.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

解决方法:

1. 使用 git fetch 命令从远程仓库获取最新的更改: 

$ git fetch origin

Username for 'https://github.com': xxxxx
Password for 'https://xxxx@github.com': 

2. 切换到想要更新的分支:(如果失败,可以直接跳过这个命令)

$ git checkout main

error: pathspec 'main:main' did not match any file(s) known to git

3. 将获取到的更改合并到你的本地分支: 

# 如果存在冲突,需要手动解决它们
$ git merge origin/main

Merge made by the 'ort' strategy.
 app/controllers/managers/sessions_controller.rb | 2 +-
 app/views/managers/sessions/new.html.erb        | 4 ++--
 config/locales/zh-CN.yml                        | 5 -----
 3 files changed, 3 insertions(+), 8 deletions(-)

 

4. 最后,将合并后的更改推送到远程仓库:

$ git push origin main:main

Username for 'https://github.com': xxxxx
Password for 'https://xxxxx@github.com': 
Enumerating objects: 12, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 31.86 KiB | 7.96 MiB/s, done.
Total 6 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/sg552/baogongpingtai-pc.git
   d658ab9..8912f21  main -> main 

 然后就可以正常 push/pull 了

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论