主页

解决 git stash 恢复保存的对象之后出现代码冲突的问题

2025-09-03 03:45PM

我在项目中,把一些修改后的文件暂存起来之后,又对代码进行修改并提交,最后把代码释放出来,就出现了代码冲突的问题,我想要把代码恢复到最后一次提交的版本

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both modified:   src/pages/AlgorithmData/edit.jsx
        both modified:   src/pages/AlgorithmData/index.jsx
        both modified:   src/pages/AlgorithmImport/index.jsx

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .vscode/launch.json
        .yarn/
        .yarnrc.yml
        Ubuntu-22.04/

root@DESKTOP-8TI8PSU:~/workspace# git checkout src/pages/
error: path 'src/pages/AlgorithmData/edit.jsx' is unmerged
error: path 'src/pages/AlgorithmData/index.jsx' is unmerged
error: path 'src/pages/AlgorithmImport/index.jsx' is unmerged

恢复到最后一次提交版本的方法:

1. 把有冲突的代码进行 git restore --staged + 文件名 操作

2.  使用 git checkout + 文件名 操作

这样就把代码恢复到了最后一次提交的版本了

解决冲突的方法:

方法1:在编辑器里面手动合并

<<<<<<< HEAD
// 这是你当前分支的修改
console.log("我的修改");
=======
// 这是你要合并过来的分支的修改(例如,develop分支)
console.log("别人的修改");
>>>>>>> develop

删除所有 <<<<<<<=======>>>>>>> 这些标记行,决定保留哪一段代码,或者将两段代码整合成你想要的正确样子。

方法2:终止合并

如果你觉得现在处理这个冲突很复杂,或者这是一个误操作,你想回到合并前的状态,那么可以中止这次合并。

git merge --abort

 执行这个命令后,你的分支会完全回退到执行 git merge 或 git pull 之前的状态,所有冲突文件都会恢复原样。

你可以等准备好了再重新尝试合并。

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论