“This is the 19th day of my participation in the August Gwen Challenge.

During team development, it is common for a commit to cause the entire project to fail unaccountably, and the best solution is to remove the wrong commit

Scene one

1. Assume that there are two commit records

commit def5adef853da4cc05752bdb36577c127be71ba5 Author: xxx Date: Thu Dec 28 16:01:36 2017 + 0800 to optimize code commit f36801544670e00b2f59a28e19017d2786c4085e Author: XXX Date: Thu Dec 28 15:59:46 2017 +0800 repair (END)Copy the code

2. Now you need to roll back to the last commit

So we can use

git reset --hard HEAD^
Copy the code

or

git reset --hard HEAD~1
Copy the code

or

git reset --hard f36801544670e00b2f59a28e19017d2786c4085e
Copy the code

3. View the log after execution to see that there is only one commit left

The git log commit f36801544670e00b2f59a28e19017d2786c4085e Author: XXX Date: Thu Dec 28 15:59:46 2017 + 0800 to repair (END)Copy the code

4. If, at this point, you suddenly realize that the rollback version is wrong, use Git reflog to check the commit record

git reflog f368015 HEAD@{0}: reset: moving to f36801544670e00b2f59a28e19017d2786c4085e def5ade HEAD@{1}: reset: moving to def5ade f368015 HEAD@{2}: reset: moving to f36801544670e00b2f59a28e19017d2786c4085e def5ade HEAD@{3}: commit: Optimized code F368015 HEAD@{4}: COMMIT (initial): fixedCopy the code

5. Find the version you want to roll back and roll back it

git reset --hard def5ade
Copy the code

6. If you need to synchronize the remote warehouse, you need to use force push

git push -f
Copy the code

Because our local library HEAD points to an older version than the remote library

Git reset can be easily rolled back to the specified history commit.

However, it has an obvious disadvantage: once you roll back to the specified COMMIT, all subsequent commits are deleted

** Git Revert is another directive that you can use to keep the commit content for later

Scene 2

1. Assume that there are three commit records

commit def5adef853da4cc05752bdb36577c127be71ba5 Author: xxx Date: Thu 16:01:36 28 Dec 2017 + 0800 commit optimization code 853 dadef5adef4cc05752bdb36577c127be71ba5 Author: XXX Date: Thu 16:01:36 28 Dec 2017 + 0800 file commit f36801544670e00b2f59a28e19017d2786c4085e Author: XXX Date: Thu Dec 28 15:59:46 2017 +0800 repair (END)Copy the code

2. Now we don’t need the second commit operation, i.e., noAdd fileThis operation

You can do the reverse with git revert -n +

git revert -n 853dadef5adef4cc05752bdb36577c127be71ba5
Copy the code

Note: Conflicts may occur here, so you need to manually modify the conflicting files and then re-add the commit

Git add. Git commit -mCopy the code

3. View the log. The original commit record still exists, and a new commit version is added

git log commit 05752def5adef853da4ccbdb36577c127be71ba5 Author: xxx Date: Thu 16:01:36 28 Dec 2017 + 0800 redo files commit def5adef853da4cc05752bdb36577c127be71ba5 Author: XXX Date: Thu 16:01:36 28 Dec 2017 + 0800 commit optimization code 853 dadef5adef4cc05752bdb36577c127be71ba5 Author: XXX Date: Thu 16:01:36 28 Dec 2017 + 0800 file commit f36801544670e00b2f59a28e19017d2786c4085e Author: XXX Date: Thu Dec 28 15:59:46 2017 +0800 repair (END)Copy the code

4. If you need to synchronize the remote warehouse

git push
Copy the code

Git Revert allows you to delete a specific COMMIT while keeping subsequent ones

However, there is also an uncomfortable area, although the operation has been rewritten, but the record is still there, and a new record is added, which feels redundant

In order to solve this problem, I finally chose the third way, which is to usegit rebase Node biopsy

Scene three

1. Assume that there are three commit records

commit def5adef853da4cc05752bdb36577c127be71ba5 Author: xxx Date: Thu 16:01:36 28 Dec 2017 + 0800 commit optimization code 853 dadef5adef4cc05752bdb36577c127be71ba5 Author: XXX Date: Thu 16:01:36 28 Dec 2017 + 0800 file commit f36801544670e00b2f59a28e19017d2786c4085e Author: XXX Date: Thu Dec 28 15:59:46 2017 +0800 repair (END)Copy the code

2. Now we don’t need the second commit operation, i.e., noAdd fileThis operation

Git rebase –onto [startPoint] [endpoint]

git rebase --onto master~853dade master~def5ade master
Copy the code

or

git rebase --onto master~2 master~1 master
Copy the code

Or use

Git rebase f368015 / I pick to drop in front of the need to change the commit and then save the exitCopy the code

3. Check that the commit specified in logs is deleted

git log commit def5adef853da4cc05752bdb36577c127be71ba5 Author: xxx Date: Thu Dec 28 16:01:36 2017 + 0800 to optimize code commit f36801544670e00b2f59a28e19017d2786c4085e Author: XXX Date: Thu Dec 28 15:59:46 2017 +0800 repair (END)Copy the code

4. If you need to synchronize the remote warehouse

git push  -f
Copy the code

Note that doing this removes the commit altogether. The only way to retrieve it is through Git reflog.

5. If you just want to change the commit author

Here’s how it works:

git rebase -i f368015
Copy the code

Change the pick before the commit to Edit

then

git commit --amend --author="xxxxxxxx"
Copy the code

then

git rebase --continue
Copy the code

Finally, save and exit

conclusion

  • git revert

Application scenario: This method can be used if you want to revert to a previously committed version and we don’t want any later versions.

  • git revert

Application scenario: If we want to undo a previous version, but we want to keep the later version of the target version and record the entire version change process, we can use this method.

  • Git rebase — Onto and Git Rebase I

Application scenario: If we want to undo a previous version, but want to keep the later version of the target version, clean the COMMIT record completely, without adding new commit records, we can use this method