Home > Article > Development Tools > Git implements merge undo and clears the merged local files
Solution
Method 1:git reset --merge the hash string submitted before merge
Note 1:
Note 2: When MERGE_HEAD is on the current commit (that is, when an error or conflict is encountered when merging branches, there will be an extra "|MERGING" next to the branch) )git merge --abort
Same as this method
Method 2:
git reset merge前的任何一次提交的hash串 git clean -n #预删除 #将预删除不想删除的文件加入.gitignore git add .gitignore git clean -f
Convention: Remote warehouse URL Use remote url
instead of
1. Simulation Developer No. 1
mkdir gitTest #新增文件夹gitTest cd gitTest git init git remote add origin "remote url" echo "长太息以掩涕兮, 哀民生之多艰。" > lyrics.txt #新建 lyrics.txt 并在里面写入文字 git add lyrics.txt #将 lyrics.txt加入暂存区 git commit -m "lyrics.txt from user 1" git push origin master git checkout -b dev git push origin dev:dev
2. Simulation Developer No. 2
mkdir gitTest2 cd gitTest2 git clone "remote url" cd gitTest echo "Don't make me suffer" > Suffer.txt git add Suffer.txt git commit -m "Suffer.txt from user2" git push origin dev
3. Simulate Developer No.1
git checkout master git merge origin/dev #合并远程dev分支 echo "余虽好修姱以鞿羁兮, 謇朝谇而夕替。" >> lyrics.txt #修改文件 lyrics.txt echo "余虽好修姱以鞿羁兮, 謇朝谇而夕替。" > test.txt #新建test并写入内容 git add test.txt #将 test.txt 加入暂存区
Situation faced by Developer No.1: Local master merge The content of the remote dev is removed, and a Suffer.txt file is added locally. However, it was found that the wrong branch was merged, and the merge operation just now had to be cancelled. But there are changes to the local files.
View current local filesls
<img src="https://img.php.cn/upload/image/270/754/732/1607592020999359.png" title="1607592020999359.png" alt="Git implements merge undo and clears the merged local files">
git diff HEAD
git reset --merge HEAD^HEAD^In this case it is 7f811bf, the above commit history can be See that 7f811bf is the hash string submitted before merge
The difference between the workspace and the staging area
git diffThe difference between the workspace and the repository
git diff HEAD The difference between the staging area and the repository
git diff --cached
Verification method two
View the current local file again
##View the commit record again
git log --oneline --graph
##Phase results:
It is obvious that the merge has been rolled back, but the locally merged files are still there. Also delete the redundant merged file (Suffer.txt).
Pre-deletion) git clean -n
注意:这里看到本地原来的文件 test.txt 也将被删除,这不是我所期望的。我只希望可以删除 meger 的文件。
将 test.txt 文件加入 .gitignore 再执行预删除
echo test.txt > .gitignore git add .gitignore git clean -n
阶段结果:可以看到将会被删除的文件只剩下 merge 的多余文件了。
git clean -f
The above is the detailed content of Git implements merge undo and clears the merged local files. For more information, please follow other related articles on the PHP Chinese website!