Git implements merge undo and clears the merged local files
git tutorial column introduces how to clear merge
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 files
ls
<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 log --oneline --graph
-
Withdraw merge
- git reset --merge 7f811bf
- Or execute
git reset --merge HEAD^
View the local fileHEAD^In this case it is 7f811bf, the above commit history can be See that 7f811bf is the hash string submitted before merge
ls - and view the file content
View the commit record again git log --oneline --graph -
View -
The difference between the workspace and the staging area
git diff-
The difference between the workspace and the repository
git diff HEAD -
The difference between the staging area and the repository
git diff --cached -
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 -
Verification method two
Undo the merge
git reset 7f811bf-
lsView 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
Copy after login阶段结果:可以看到将会被删除的文件只剩下 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

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

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

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.

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.

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

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 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/<remote branch name>. Resolve conflicts arising from mergers. Use git commit -m "Merge branch <Remote branch name>" to submit merge changes and apply updates.
