需要切换分支修改代码出现的问题。
一开始非常正常: 显示一些需要commit的文件
但是结尾有
感觉问题不大 ,虽然有修改,但是不记录这些个文件的修改
然后进行正常的commit
push后报错:
报错类似:
! [rejected] <local/branch> -> <origin/branch> (fetch first)
error: failed to push some refs to <origin/url>
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.
看起来像是没有pull 和远端分支内容不一致导致的,那么就git pull同步一下远端的改动。
然后报错:
On branch <local-branch-name>
Your branch is up to date with 'origin/<branch-name>'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
pkg/test
abcd.log
意思是我本地修改了但是远端也有修改。
然后将其mv走 防止有啥问题还能恢复。
重新pull后只有几个带有冲突的文件。解决冲突之后,执行
git merge --continue
才同步了代码。
重新commit自己的改动即可
==========
报错的关键原因:
- 目标文件不在关注的改动列表中
- 远端的目标文件已修改
- 本地的该文件也修改
解决方案:
1. 本地文件做备份
2. 拉取远端代码
3. 解决冲突
4. push其他改动
该解决方案:
- 不会撤销本地改动
- 不会覆盖远端改动
如果需要使用远端代码强制覆盖本地代码改动操作:
git fetch --all
git reset --hard origin/<branch>
git pull