Using git to tamper with history

Keywords: Programming git

⭐ more front-end technology and knowledge points, search subscription No. JS bacteria subscription

Sometimes we need to modify the contents of git historical submitted files. If it's only in the previous commit, just modify the files and execute -- amend:

If the last submitted document is modified:

// do something
git add .
git commit --amend --no-edit

In addition, you may need to modify the previously submitted files, so you need to use rebase:

git log check the previous submission records:

commit 084dbd48be6fff86b0d3de23220cff8cabddd9c6 (HEAD -> master)
Author: oli <oli@olideMacBook-Pro.local>
Date:   Thu May 9 23:20:09 2019 +0800

    echart how to use

commit 3358a5fd3078d7fb6794d8c2d468054db300a46f
Author: oli <oli@olideMacBook-Pro.local>
Date:   Wed May 1 11:26:12 2019 +0800

    edit Readme.md

commit c0b7ac77431ceb270b5f0aa0f97b13a79afca4b9
Author: oli <oli@olideMacBook-Pro.local>
Date:   Wed May 1 02:25:40 2019 +0800

    init

Suppose we need to modify the file of the item in the second record, then the line command:

git rebase 3358a5fd3078d7fb6794d8c2d468054db300a46f^ --interactive

Will pick

pick 3358a5f edit Readme.md
pick 084dbd4 echart how to use

Change to edit

edit 3358a5f edit Readme.md
pick 084dbd4 echart how to use

Then save

Stopped at 3358a5f...  edit Readme.md
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

According to the prompt, modify the file and then use git commit --amend to submit, and call git rebase --continue:

diff --git a/README.md b/README.md
index 73ae153..98b8201 100644
--- a/README.md
+++ b/README.md
@@ -1,29 +1 @@
-# vuebox
-
-## Project setup
-```
-yarn install
# ...
+Read the guide and start building things in no time!

Submit after modification:

git add . && git commit --amend --no-edit

 > running pre-commit hook: lint-staged
No staged files match any of provided globs.
[detached HEAD 4436ffc] edit Readme.md
 Author: oli <oli@olideMacBook-Pro.local>
 Date: Wed May 1 11:26:12 2019 +0800
 11 files changed, 177 insertions(+), 131 deletions(-)
# ...

Finally, call git rebase --continue.

Successfully rebased and updated refs/heads/master.

Get it done.

Please pay attention to my subscription number, and push technical articles about JS from time to time. Only talk about technology, not gossip

Posted by $username on Tue, 12 Nov 2019 12:04:55 -0800