Git问题(Your local changes to the followingcant checkout because of unmerged files)

问题1:Git出现冲突error: Your local changes to the following files would be overwritten by merge: xxx/

解决办法:
命令1:git stash
解释 :将工作区修改的内容保存到堆栈中(存在未add到git的文件时无法使用)
命令2:git pull
解释 :拉取远程库修改内容到当前分支的工作区
命令3:git stash pop
解释 :将堆栈中的内容恢复到工作区

解决问题1后切换分支时会引发问题2:cant checkout because of unmerged files,you have to resolve all merge conflicts before checkout.
解决办法:
命令1:git merge
解释 :抛弃合并过程并且尝试重建合并前的状态
命令2:git reset
解释 :撤销提交

命令3:git pull

警告:运行git-merge时含有大量的未commit文件很容易让你陷入困境,这将使你在冲突中难以回退。因此非常不鼓励在使用git-merge时存在未commit的文件。
建议使用git-stash命令将这些未commit文件暂存起来,并在解决冲突以后使用git stash pop把这些未commit文件还原出来。

# 将工作区所有的数据提交到暂存区
git add . 

# 将暂存区的数据commit
git commit -m "说明"

# 将上面2歩合并执行
git commit -am "说明"

# 误add单个/多个文件 ->将file退回到unstage区
git reset HEAD
# 将所有文件保存到暂存区
git commit

# 撤销commit的最后一次提交
# --soft:不删除工作空间改动代码,撤销commit,不撤销git add . 操作;
# --mixed:不删除工作空间改动代码,撤销commit,并且撤销git add . 操作;
# HEAD^的意思是上一个版本、也可写为上上个版本HADE^2...
git reset --soft HADE^1

# 因为cmd控制台中换行符默认是^,而不是 ,所以^符号被git编译为换行符了,解决方案: 
git reset --hard "HEAD^1"
# 更改提交代码的注释
git commit --amend

# 查看提交操作的日志,包括撤销的的记录
git reflog

# 查看当前分支
git branch

# 列出远程库所有分支
git branch -a

# 切换分支
git switch [name]

# 本地创建新的分支
git switch -c [name]

# 删除分支,不能删除当前分支
git branch -d [name]

# 将name分支与当前分支合并
git merge 【from name】

# 查看当前状态、列出所有新的修改、暂存区文件修改情况
git status

# 显示最近n次的commit记录
git log -n

# 回滚版本
# 删除工作空间改动代码,撤销commit,撤销git add . 
git reset --hard commit_id

# 查看工作区和暂存区的差别
git diff【file】

# 切换到master分支
git chechout master

# 情况一:未使用 git add 缓存代码时
# 放弃单个文件修改,注意不要忘记中间的"--",不写就成了检出分支了
git checkout -- filepathname

# 放弃所有的文件修改
git checkout .  

# 情况二:已经使用 git add 缓存代码时
# 此命令用来清除 git 对于文件修改的缓存。相当于撤销 git add 命令所在的工作。在使用本命令后,本地的修改并不会消失,而是回到了情况一所示的状态。继续用情况一中的操作,就可以放弃本地的修改
# 放弃指定文件的缓存
git reset HEAD filepathname

# 放弃所有的缓存
git reset HEAD .

# 情况三:已经用 git commit 提交了代码
git reset --hard commitid


注明:图片来源:https://www.bilibili.com/video/BV1ni4y1t7jK
git命令教程:https://www.yiibai.com/git/git_fetch.html

原文地址:https://www.cnblogs.com/ding08/p/14700743.html