git realizes merge cancellation and clears merged local files

The git tutorial section introduces how to clear merge


recommendations: git tutorial
1. Get straight to the point.
Solution
1: git reset ---merge any submitted hash string before merge.
Note 1:

If there is no change in the workspace after the merge, use this boldly method.
If the workspace is changed after the merge, then this method will reset all changes in the workspace, use it with caution. However, changes to 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 "|MERGING" next to the branch) git merge --abort is the same as this method.
Method 2:

1

2

3

4

5

Any submitted hash string before git reset merge

git clean -n #预 Delete

# Add pre-deleted files that you don’t want to delete to .gitignore

git add .gitignore

git clean -f

2. Construction environment
convention: Use of remote warehouse URL remote url instead of
1, simulation developer number one

1

2

3

4

5

6

7

8

9

10

mkdir gitTest #Add a new folder gitTest

cd gitTest

git init

git remote add origin "remote url"

echo "It's too long to hide your tears, and the people's livelihood is so difficult."> lyrics.txt #Newlyrics.txt and write in Enter text

git add lyrics.txt #Add lyrics.txt to the temporary storage area

git commit -m "lyrics.txt from user 1"

git push origin master

git checkout -b dev

git push origin dev:dev

2. Simulate developer number two

1

2

3

4

5

6

7

8

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 1

2

3

4

5

git checkout master

git merge origin/dev

#Merge the remote dev branch echo "Yu Shouhao repairs the scorpion to be shackled, and replaces it at night." >> lyrics.txt #Modify the file lyrics.txt

echo "Yu Shouhao Xiuyi took the lead and replaced him as a fool."> test.txt #New test and write content

git add test.txt #Add test.txt to the temporary storage Area

3.
The situation faced by developer No. 1 of revoking merge : The local master merges the contents of the remote dev, and a Suffer.txt file is added locally. But it is found that the wrong branch is combined, and the merge operation just now needs to be undone. But the local file has changed again.

View the current local file ls



View the difference between the workspace and the version library git diff HEAD



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


Verification method 1
Withdraw merge git reset --merge 7f811bf or execute git reset --merge HEAD^
HEAD^this In the example, it is 7f811bf. The above submission history shows that 7f811bf is the hash string submitted before the merge.

View the local file ls and view the content of the file.



View the submission record again git log --oneline --graph



view

The difference between the workspace and the staging area The difference between the git diff
workspace and the version library The difference between the git diff HEAD
staging area and the version library The


final result of git diff --cached : The local file changes have been reset (ie developer number one Added after merge---though Yu is good for repairing and replacing it with banquets, it is deleted), but the temporary storage area still has content. Therefore, if there are changes in the workspace after merge, use it with caution.

Verification method 2:
Withdraw merge git reset 7f811bf

Check the current local file



again. Check the submission record again. git log --oneline --graph



Phase result: It is obvious that the merge has been rolled back. But the files merged locally are still there. Also delete the redundant merged file (Suffer.txt).

Before deleting, you can take a look at the contents that will be deleted when you execute the delete operation (pre-delete). git clean -n


Note: Here we see that the local original file test.txt will also be deleted, which is not what I expected. I just hope that meger files can be deleted.

Add the test.txt file to .gitignore and perform pre-delete

1

2

3

echo test.txt> .gitignore

git add .gitignore

git clean -n



phase result: you can see that the files that will be deleted are only the redundant files of merge .

Perform the delete operation git clean -f
final result

Guess you like

Origin blog.csdn.net/hdl17822307857/article/details/112798648