1. The beginning of the story
The code under the remote master branch was accidentally submitted with a lot of junk code or the project was deleted. I wanted to roll back to a previous version and delete the commit log. what to do? The scenario is as shown in the picture:
The scenario is very simple. My boss uploaded a file and I deleted it. There is a way to push the file again, but you don’t want him to see the comment in the picture (ps: this way I won’t be fired). The code to implement the above scenario is as follows:
vim A.txt
git add .
git commit -a -m "add A.txt"
git push
rm A.txt
git commit -a -m "I deleted the boss's stuff"
git push
————–Separating line————–
What should a confused newbie do? what to do? what to do?
The submission pushed to the remote cannot be modified by default, but it must be modified:
git push -f
2. Solution
2.1 Workspace, staging area, local repository & remote Version library
No pic say 78. . .
》》Workspace: It is the directory where we operate
》》Staging area: a snapshot of the operating directory
》》Local repository: the essence of Git, everyone is the central warehouse. That is to say, the benefits of Git distribution are naturally compared to the centralized version like SVN
》》Remote version library: a central warehouse like Github can achieve sharing.
Commonly used operations are also shown in the picture, which is self-evident.
2.2 Practical solution
Talk is cheap, Show me the code or money~ The code is as follows:
git log
git reset --soft ${commit-id}
git stash
git push -f
The detailed explanation is as follows:
Line 1: git log View the commit history, and then find the version to be rolled back. The history is as follows,
commit 84686b426c3a8a3d569ae56b6788278c10b27e5b
Author: JeffLi1993
Date: Fri Apr 8 19:1 1:32 2016 +0800
I deleted my boss’s stuff
commit 72bd6304c3c6e1cb7034114db1dd1b8376a6283a
Author: JeffLi1993
Date: Fri Apr 8 19:05:23 2016 +0800
add A.txt
The version we want to roll back to That is: 72bd6304c3c6e1cb7034114db1dd1b8376a6283a
Line 2: git reset –soft 72bd6304c3c6e1cb7034114db1dd1b8376a6283a
Undo the previous version Modify and return to the temporary storage area (I don’t know how to look at beautiful pictures~). The difference between soft and hard parameters is that hard modification records are lost, while soft will retain modification records.
Line 3: Temporarily saved for safety reasons.
Line 4: git push -f
Push the local master to the remote repository, -f forces overwriting.
3. Summary
git reset rolls back to a certain version
git push -f forces push overwrite