Home  >  Article  >  Development Tools  >  Git implements merge undo and clears the merged local files

Git implements merge undo and clears the merged local files

coldplay.xixi
coldplay.xixiforward
2020-12-10 17:29:396209browse

git tutorial column introduces how to clear merge

Git implements merge undo and clears the merged local files

Recommendation: git tutorial

1. Get straight to the point

Solution
Method 1:git reset --merge the hash string submitted before merge
Note 1

  • If the workspace has not been changed after merge, use this method boldly.
  • If the workspace has been modified after merge, this method will reset all modifications to the workspace, Use with caution. However, changes in the staging area will be retained.

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

2. Construction environment

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 加入暂存区

3. Undo merge

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">

  • ## View the difference between the workspace and the repository

    git diff HEAD

Git implements merge undo and clears the merged local files

##View commit history
    git log --oneline --graph

  • Git implements merge undo and clears the merged local files

  • ##Verification method one

Withdraw merge
    git reset --merge 7f811bf
  • Or execute

    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

    View the local file
  • ls
  • and view the file content

    Git implements merge undo and clears the merged local files

    View the commit record again
  • git log --oneline --graph

  • Git implements merge undo and clears the merged local files

    View
  • The difference between the workspace and the staging area

    git diff
    • The difference between the workspace and the repositorygit diff HEAD
    • The difference between the staging area and the repositorygit diff --cached

    • Git implements merge undo and clears the merged local files

    ##Final result:
  • All local file changes have been reset (that is, the ones added by developer No. 1 after the merge--- and was deleted), but there is still content in the temporary storage area. Therefore, there are changes after the merge in the workspace
Use with caution

Verification method two


Undo the merge
git reset 7f811bf
  • View the current local file again

    ls

Git implements merge undo and clears the merged local files##View the commit record again
git log --oneline --graph


  • Git implements merge undo and clears the merged local files##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).

Before deleting, you can first see what content will be deleted by performing the deletion operation (

Pre-deletion) git clean -n

    • 注意:这里看到本地原来的文件 test.txt 也将被删除,这不是我所期望的。我只希望可以删除 meger 的文件。

    • 将 test.txt 文件加入 .gitignore 再执行预删除

    echo test.txt > .gitignore
    git add .gitignore
    git clean -n

    Git implements merge undo and clears the merged local files

    阶段结果:可以看到将会被删除的文件只剩下 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!

Statement:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete