git提交解决冲突
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
git提交解决冲突
⼀:git命令在提交代码前,没有pull拉最新的代码,因此再次提交出现了冲突。
error: You have not concluded your merge (MERGE_HEAD exists).
hint: Please, commit your changes before merging.
fatal: Exiting because of unfinished merge.
解决⽅法如下两种:
1.保留你本地的修改
git merge --abort
git reset --merge
合并后记得⼀定要提交这个本地的合并(add-->commit-->push-->pull)
然后在获取线上仓库
git pull
2.down下线上代码版本,抛弃本地的修改
不建议这样做,但是如果你本地修改不⼤,或者⾃⼰有⼀份备份留存,可以直接⽤线上最新版本覆盖到本地
git fetch --all
git reset --hard origin/master
git fetch
⼆:从git远程仓库中pull最新的代码,出现如下错误:Please commit your changes or stash them before you merge.
解决⽅法如下:(git stash 可⽤来暂存当前正在进⾏的⼯作,⽐如想pull 最新代码,⼜不想加新commit,或者另外⼀种情况,为了fix ⼀个紧急的bug, 先stash, 使返回到⾃⼰上⼀个commit, 改完bug之后再stash pop, 继续原来的⼯作。
)
1: git stash //暂存代码
2: git pull 分⽀名//从远程仓库拉取最新代码
3: git stash pop //合并代码到本地仓库此时代码是将暂存的代码和远程仓库的代码合并,如下图:
4:这时候需要⼿动修改合并所需的代码即可。
5:git stash clear//需要清空git栈执⾏该命令
git stash: 备份当前的⼯作区的内容,从最近的⼀次提交中读取相关内容,让⼯作区保证和上次提交的内容⼀致。
同时,将当前的⼯作区内容保存到Git栈中。
git stash pop: 从Git栈中读取最近⼀次保存的内容,恢复⼯作区的相关内容。
由于可能存在多个Stash的内容,所以⽤栈来管理,pop会从最近的⼀个stash中读取内容并恢复。
git stash list: 显⽰Git栈内的所有备份,可以利⽤这个列表来决定从那个地⽅恢复。
git stash clear: 清空Git栈。
此时使⽤gitg等图形化⼯具会发现,原来stash的哪些节点都消失了
三:git push 报错,如下:
解决命令:git pull --rebase origin 你的分⽀名称,如下图所⽰
再次执⾏push命令:如下图所⽰:
四:git push 还会报下⾯的错(如图所⽰):这多是多⼈开发有了冲突。
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.
解决命令如下:
git push -u 代码所在的分⽀ -f //强制提交,此时远程上的修改已经被覆盖。
这种⽅法⼀般不建议使⽤,除⾮你把远程上修改的代码复制到本地。
五:本地回退历史版本,当提交代码发⽣冲突或者想回退到某⼀个版本,操作如下:
1:git log (获取提交的历史⽇志)
2:执⾏命令:git reset --hard 版本号(就是git log 中的 commit后⾯的哈希值(上图中的黄⾊部分 commit 后⾯的值))
3:想要修改远程上的代码还需要执⾏如下命令:
git push -u 代码所在的分⽀ -f //强制提交,此时远程上的修改已经被覆盖。
这种⽅法⼀般不建议使⽤,除⾮你把远程上修改的代码复制到本地。