解决failed to push some refs to git和Git Cannot rebase: You have unstaged changes.

本文介绍了一种常见的在使用Git推送代码到GitHub过程中遇到的问题及其解决办法。主要原因是README.md文件未同步到本地,通过使用特定命令进行代码合并并解决冲突后,可以成功将更改推送到远程仓库。


 1.
在使用git 对源代码进行pushgitHub时可能会出错,信息如下:

 


2.此时很多人会尝试下面的命令把当前分支代码上传到master分支上。

$ git push -u origin master

但依然没能解决问题



 

 

 

 

3.出现错误的主要原因是github中的README.md文件不在本地代码目录中



 

 

4.可以通过如下命令进行代码合并【注:pull=fetch+merge]

git pull --rebase origin master

 



 
5、此时又出现了另外一个问题:cannot pull with you have unstaged changes

这说明“有修改过的文件,但是没有放到暂存区”,因此使用
git stash

 

然后再git pull --rebase (每次push之前最好这样做一次)就可以正常提交了。
 

 

 

6.执行上面代码后可以看到本地代码库中多了README.md文件

 



 

 

7.此时再执行语句 git push -u origin master即可完成代码上传到github

 



 

didi@MacBook-Pro-5 gen4_production_test % git branch * dev-ota-merge-0903 master didi@MacBook-Pro-5 gen4_production_test % git remote add origin https://git.xiaojukeji.com/voyager-test-group/product-test/gen4_production_test.git error: remote origin already exists. didi@MacBook-Pro-5 gen4_production_test % git push -u origin HEAD:dev_ota_test To git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test.git ! [rejected] HEAD -> dev_ota_test (fetch first) error: failed to push some refs to 'git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test.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. didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test remote: Enumerating objects: 196, done. remote: Counting objects: 100% (53/53), done. remote: Compressing objects: 100% (23/23), done. remote: Total 196 (delta 39), reused 41 (delta 30), pack-reused 143 Receiving objects: 100% (196/196), 87.48 KiB | 914.00 KiB/s, done. Resolving deltas: 100% (125/125), completed with 12 local objects. From git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test * branch dev_ota_test -> FETCH_HEAD * [new branch] dev_ota_test -> origin/dev_ota_test hint: You have divergent branches and need to specify how to reconcile them. hint: You can do so by running one of the following commands sometime before hint: your next pull: hint: hint: git config pull.rebase false # merge hint: git config pull.rebase true # rebase hint: git config pull.ff only # fast-forward only hint: hint: You can replace "git config" with "git config --global" to set a default hint: preference for all repositories. You can also pass --rebase, --no-rebase, hint: or --ff-only on the command line to override the configured default per hint: invocation. fatal: Need to specify how to reconcile divergent branches. didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --rebase error: cannot pull with rebase: You have unstaged changes. error: please commit or stash them. didi@MacBook-Pro-5 gen4_production_test % git commit On branch dev-ota-merge-0903 Your branch is ahead of 'origin/dev-ota-merge-0903' by 5 commits. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: common_lib/web_lib/api_generate_ota_task.py no changes added to commit (use "git add" and/or "git commit -a") didi@MacBook-Pro-5 gen4_production_test % git add . didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --rebase error: cannot pull with rebase: Your index contains uncommitted changes. error: please commit or stash them. didi@MacBook-Pro-5 gen4_production_test % git commit -a Aborting commit due to empty commit message. didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --no-rebase From git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test * branch dev_ota_test -> FETCH_HEAD error: Your local changes to the following files would be overwritten by merge: common_lib/web_lib/api_generate_ota_task.py <stdin>:10: trailing whitespace. task_url, <stdin>:19: trailing whitespace. task_url, warning: 2 lines add whitespace errors. Merge with strategy ort failed. didi@MacBook-Pro-5 gen4_production_test % git add common_lib/web_lib/api_generate_ota_task.py didi@MacBook-Pro-5 gen4_production_test % git commit -m "feat: 添加 OTA 任务生成接口调用" [dev-ota-merge-0903 78aa496] feat: 添加 OTA 任务生成接口调用 1 file changed, 2 insertions(+), 2 deletions(-) didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --no-rebase From git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test * branch dev_ota_test -> FETCH_HEAD Auto-merging cases/ota_test/test_general_ota.py CONFLICT (content): Merge conflict in cases/ota_test/test_general_ota.py Auto-merging common_lib/web_lib/api_generate_ota_task.py CONFLICT (content): Merge conflict in common_lib/web_lib/api_generate_ota_task.py Automatic merge failed; fix conflicts and then commit the result. didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --no-rebase From git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test * branch dev_ota_test -> FETCH_HEAD Already up to date. didi@MacBook-Pro-5 gen4_production_test % git branch * dev-ota-merge-0903 master didi@MacBook-Pro-5 gen4_production_test % git push To git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test.git ! [rejected] dev-ota-merge-0903 -> dev-ota-merge-0903 (fetch first) error: failed to push some refs to 'git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test.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. didi@MacBook-Pro-5 gen4_production_test % git branch * dev-ota-merge-0903 master didi@MacBook-Pro-5 gen4_production_test % git switch dev_ota_test branch 'dev_ota_test' set up to track 'origin/dev_ota_test'. Switched to a new branch 'dev_ota_test' didi@MacBook-Pro-5 gen4_production_test % git branch dev-ota-merge-0903 * dev_ota_test master didi@MacBook-Pro-5 gen4_production_test % git push -u origin dev_ota_test branch 'dev_ota_test' set up to track 'origin/dev_ota_test'. Everything up-to-date didi@MacBook-Pro-5 gen4_production_test % git branch dev-ota-merge-0903 * dev_ota_test master didi@MacBook-Pro-5 gen4_production_test % git add . didi@MacBook-Pro-5 gen4_production_test % git commit -m "提交机器人通知优化以及报文录制等" On branch dev_ota_test Your branch is up to date with 'origin/dev_ota_test'. nothing to commit, working tree clean didi@MacBook-Pro-5 gen4_production_test % git push Everything up-to-date didi@MacBook-Pro-5 gen4_production_test % git branch dev-ota-merge-0903 * dev_ota_test master didi@MacBook-Pro-5 gen4_production_test % git revert -m 1 HEAD [dev_ota_test 1564867] Revert "增加日志打印" 2 files changed, 1 insertion(+), 2 deletions(-) didi@MacBook-Pro-5 gen4_production_test % git log --oneline 1564867 (HEAD -> dev_ota_test) Revert "增加日志打印" a44efa5 (origin/dev_ota_test) 增加日志打印 501b72f 优化OTA前的磁盘空间清理 e5f7c7f voyager OTA前重启voy-ota,无需重启ACU df4c8a2 更新80101树莓派ip f0d3d5b voyager OTA前重启ACU 63f462d fix bug, 去掉注释,否则mdhil读不到version变量 09cceed Merge branch 'master' into dev-add_region_switch f53290a 解决空格引起的版本下载失败问题 a3d21f8 调整代码到lib dc7239f 增加改region代码 03a802a Merge branch 'master' into dev-add_region_switch b064d42 调试issue流程 e930019 切region自动化 097bfdd 服务器ip地址变更 b26a282 安装包文件增加匹配编译平台 bc1ee2a 调整升级方式为voy-ota-from-gift : a44efa5 (origin/dev_ota_test) 增加日志打印 501b72f 优化OTA前的磁盘空间清理 e5f7c7f voyager OTA前重启voy-ota,无需重启ACU df4c8a2 更新80101树莓派ip f0d3d5b voyager OTA前重启ACU 63f462d fix bug, 去掉注释,否则mdhil读不到version变量 09cceed Merge branch 'master' into dev-add_region_switch f53290a 解决空格引起的版本下载失败问题 a3d21f8 调整代码到lib dc7239f 增加改region代码 03a802a Merge branch 'master' into dev-add_region_switch b064d42 调试issue流程 e930019 切region自动化 097bfdd 服务器ip地址变更 b26a282 安装包文件增加匹配编译平台 bc1ee2a 调整升级方式为voy-ota-from-gift : a44efa5 (origin/dev_ota_test) 增加日志打印 501b72f 优化OTA前的磁盘空间清理 e5f7c7f voyager OTA前重启voy-ota,无需重启ACU df4c8a2 更新80101树莓派ip f0d3d5b voyager OTA前重启ACU 63f462d fix bug, 去掉注释,否则mdhil读不到version变量 09cceed Merge branch 'master' into dev-add_region_switch f53290a 解决空格引起的版本下载失败问题 a3d21f8 调整代码到lib dc7239f 增加改region代码 03a802a Merge branch 'master' into dev-add_region_switch b064d42 调试issue流程 e930019 切region自动化 097bfdd 服务器ip地址变更 b26a282 安装包文件增加匹配编译平台 bc1ee2a 调整升级方式为voy-ota-from-gift 9c086b3 kill-onboard-teleassis 413559e is_perf_test 立即落盘 2e55884 Change 80101 raspberry ip 2f6ca8a fenggaofeng:替换 apt install方式为 voy-ota-from-gift 4e13464 fenggaofeng:重启x86方式兼容A样B样以及C样,不再需要单独切分支进行测试 c8526f6 Merge branch 'master' of git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test into dev_gen4_b_sp _reboot f601c35 fenggaofeng:lite_hil 碰撞节点增加perf flag,rtm、semantic、e2e增加启动动作。 bb9cf28 Merge branch 'master' of git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test into dev_gen4_b_sp: dc7239f 增加改region代码 03a802a Merge branch 'master' into dev-add_region_switch b064d42 调试issue流程 e930019 切region自动化 097bfdd 服务器ip地址变更 b26a282 安装包文件增加匹配编译平台 bc1ee2a 调整升级方式为voy-ota-from-gift 9c086b3 kill-onboard-teleassis 413559e is_perf_test 立即落盘 2e55884 Change 80101 raspberry ip 2f6ca8a fenggaofeng:替换 apt install方式为 voy-ota-from-gift 4e13464 fenggaofeng:重启x86方式兼容A样B样以及C样,不再需要单独切分支进行测试 c8526f6 Merge branch 'master' of git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test into dev_gen4_b_sp_reboot f601c35 fenggaofeng:lite_hil 碰撞节点增加perf flag,rtm、semantic、e2e增加启动动作。 bb9cf28 Merge branch 'master' of git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test into dev_gen4_b_sp didi@MacBook-Pro-5 gen4_production_test % 帮我看一下是哪步将代码提交了我合并冲突之后原来的代码不见了想退回到原来的代码上
11-21
wyl@ubuntu:~/NVR/tp_package$ git reset HEAD~ Unstaged changes after reset: M u_packages/mqtt-cloud/src/mqtt_check_link_status.c M u_packages/netifd/files/lib/netifd/dhcp.script wyl@ubuntu:~/NVR/tp_package$ git add u_packages/mqtt-cloud/src/mqtt_check_link_status.c u_packages/netifd/files/lib/netifd/dhcp.script wyl@ubuntu:~/NVR/tp_package$ git commit -m "[nvr810p]" -m "Change-Id: I051f910122b53502365b757ba5d915c2e88f280f" [nvr_iterator_v15_2016v2 ee3fa84] [nvr810p] 2 files changed, 4 insertions(+), 2 deletions(-) wyl@ubuntu:~/NVR/tp_package$ git commit --amend [nvr_iterator_v15_2016v2 836a75b] [nvr810p][mqtt-cloud/mqtt_check_link_status] bug fix: 1009608,999623.If the NVR switches the WAN port network cable to another router or changes the internal network segment of the connected router, the NVR will go offline, affecting performance testing (which will result in ineffective automatic switching of the pull link);NVR sets dynamic IP mode, which cannot automatically obtain a new IP address when the front-end network segment is changed, causing the device to go offline and the camera to disconnect Date: Tue Dec 2 19:02:17 2025 -0800 2 files changed, 4 insertions(+), 2 deletions(-) wyl@ubuntu:~/NVR/tp_package$ git push origin HEAD:refs/for/nvr_iterator_v15_2016v2 Counting objects: 131, done. Delta compression using up to 2 threads. Compressing objects: 100% (107/107), done. Writing objects: 100% (131/131), 21.00 KiB | 0 bytes/s, done. Total 131 (delta 112), reused 26 (delta 24) remote: Resolving deltas: 100% (112/112) remote: Processing changes: refs: 1, done To ssh://wuyilun1@gerrit-ce.tp-link.com:29418/IPC/NVR/Proprietary ! [remote rejected] HEAD -> refs/for/nvr_iterator_v15_2016v2 (change http://gerrit-ce.tp-link.com/c/IPC/NVR/Proprietary/+/302948 closed) error: failed to push some refs to 'ssh://wuyilun1@gerrit-ce.tp-link.com:29418/IPC/NVR/Proprietary'
最新发布
12-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值