Table of Contents
1. Get straight to the point
2. Construction environment
3. Undo merge
Undo the merge
Home Development Tools git Git implements merge undo and clears the merged local files

Git implements merge undo and clears the merged local files

Dec 10, 2020 pm 05:29 PM
git merge

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
Copy after login

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
Copy after login

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
Copy after login

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

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="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/270/754/732/1607592020999359.png" class="lazy" 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
    Copy after login

    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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
How to download git projects to local How to download git projects to local Apr 17, 2025 pm 04:36 PM

To download projects locally via Git, follow these steps: Install Git. Navigate to the project directory. cloning the remote repository using the following command: git clone https://github.com/username/repository-name.git

How to update code in git How to update code in git Apr 17, 2025 pm 04:45 PM

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

How to use git commit How to use git commit Apr 17, 2025 pm 03:57 PM

Git Commit is a command that records file changes to a Git repository to save a snapshot of the current state of the project. How to use it is as follows: Add changes to the temporary storage area Write a concise and informative submission message to save and exit the submission message to complete the submission optionally: Add a signature for the submission Use git log to view the submission content

How to solve the efficient search problem in PHP projects? Typesense helps you achieve it! How to solve the efficient search problem in PHP projects? Typesense helps you achieve it! Apr 17, 2025 pm 08:15 PM

When developing an e-commerce website, I encountered a difficult problem: How to achieve efficient search functions in large amounts of product data? Traditional database searches are inefficient and have poor user experience. After some research, I discovered the search engine Typesense and solved this problem through its official PHP client typesense/typesense-php, which greatly improved the search performance.

How to merge code in git How to merge code in git Apr 17, 2025 pm 04:39 PM

Git code merge process: Pull the latest changes to avoid conflicts. Switch to the branch you want to merge. Initiate a merge, specifying the branch to merge. Resolve merge conflicts (if any). Staging and commit merge, providing commit message.

What to do if the git download is not active What to do if the git download is not active Apr 17, 2025 pm 04:54 PM

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

How to delete a repository by git How to delete a repository by git Apr 17, 2025 pm 04:03 PM

To delete a Git repository, follow these steps: Confirm the repository you want to delete. Local deletion of repository: Use the rm -rf command to delete its folder. Remotely delete a warehouse: Navigate to the warehouse settings, find the "Delete Warehouse" option, and confirm the operation.

How to update local code in git How to update local code in git Apr 17, 2025 pm 04:48 PM

How to update local Git code? Use git fetch to pull the latest changes from the remote repository. Merge remote changes to the local branch using git merge origin/&lt;remote branch name&gt;. Resolve conflicts arising from mergers. Use git commit -m "Merge branch &lt;Remote branch name&gt;" to submit merge changes and apply updates.

See all articles