Location>code7788 >text

git Re E -i

Popularity:561 ℃/2025-04-20 12:32:41

The general purpose of this command is to modify the local commit, which is applicable to the following situations:

  1. Local history troubleshooting and modification
  2. After the code review, click back to modify

Detailed explanation

  • pick, just means including commits. When re-ordering the command, rescheduling the order of the pick commands changes the order of commits. If you choose not to include commit, the entire row should be deleted.
  • reward, the command is similar to pick, but after use, the reset process pauses and gives you the opportunity to change the commit message. Any changes made in the submission will not be affected.
  • edit, if you choose edit commit, you will have a chance to modify the commit, which means you can add or change the commit entirely. You can also make more submissions before proceeding with the rebase. This allows you to split large commits into smaller commits, or delete the wrong changes you made in the commit.
  • squash, a command that allows you to merge two or more commits into one commit. The commit is compressed into the commit above it. Git gives you the opportunity to write new commit messages describing these two changes.
  • fixup, which is similar to squash, but the commit to merge has its message discarded. The commit is merged into the commit above it only, and the earlier commit message is used to describe the two changes.
  • exec, which allows you to run arbitrary shell commands on the commit.

test

git init

touch 
git add .
git commit -m "add base"

touch 
git add .
git commit -m "add 1"

touch 
git add .
git commit -m "add 2"

touch 
git add .
git commit -m "add 3"

touch 
git add .
git commit -m "add 4"

touch 
git add .
git commit -m "add 5"

git log

commit a75ed742838ebc1ef1073502623478f73e1ec21f
Author: 
Date:   Wed Mar 4 10:02:51 2020 +0800

    add 5

commit 8b485bb4768b2abf8f6400dcba069f1a650ed5ec
Author: 
Date:   Wed Mar 4 09:59:27 2020 +0800

    add 4

commit 63ce9fb010da550c668aca66758c45fbfad46e2b
Author:
Date:   Wed Mar 4 09:59:04 2020 +0800

    add 3

commit 9cd34c4d42f52cfb40026dae613c8ad29d7cbc66
Author: 
Date:   Wed Mar 4 09:58:45 2020 +0800

    add 2

commit 77bd0eb1a97e1676367ea236c1c47c155eaa8430
Author: 
Date:   Wed Mar 4 09:58:23 2020 +0800

    add 1

commit 443sdafdsafgdf123123dfh234546gjnhbjh344
Author: 
Date:   Wed Mar 4 09:58:23 2020 +0800

    add base

Get started

First things first

git rebase -i HEAD~2Heregit rebase-i HEAD~It is a common command, followed by2For a flashbackgit logSeveral submissions

for examplegit rebase -i HEAD~2, when entering vim mode, it displays:

pick a75ed74 add 5
pick 8b485bb add 4

Modified to

pick a75ed74 add 5

Save and deleteadd 4Commit

pick, sort or delete commit

passgit rebase -i HEAD~nAfter that, delete each line or replace the position up and down to recharge the commit order or delete.

record, modify the message that submits commit

entergit rebase -i HEAD~3, enter vim as follows:

pick 9cd34c4 add 2
pick 63ce9fb add 3
pick 575fd8b add 5

Put the first linepickChange torecordorr,hererforrecordAbbreviation, there is no difference between the two

r 9cd34c4 add 2
pick 63ce9fb add 3
pick 575fd8b add 5

After saving, you will enter vim as follows:

add 2

Modify this to

add 2 ~ new commit

Save and exit, usegit log

add 2 ~ new commit

edit, modify the submitted code

  • Scenario 1, you need to do something in between two submissions

git rebase -i HEAD~2

pick 6934312 add 3
pick 5ce6dde add 5

Change to

e 6934312 add 3
pick 5ce6dde add 5

After saving, the terminal displays the following two key sentences

git commit --amend
git rebase --continue

--amendTo add the modification to the current commit

--contineIt means that no processing will be done, and the next submission will be continued

For example, wevim And add contentedit testto the file and save.

git add 
git commit -m 'new edit '
git rebase --continue

At this timegit logfor

add 
new edit 
add 
  • Scenario 2: You need to intersperse a submission between two submissions to do something, but no new submissions are added.

git rebase -i HEAD~2

pick 6934312 add 3
pick 5ce6dde add 5

Change to

e 6934312 add 3
pick 5ce6dde add 5
git add 
git commit --amend

At this timegit logfor

add 
add 

Now the modification has comeadd This is being submitted

squash, merge and change to a commit

git rebase - i HEAD~2

Output

pick 6934312 add 3
pick 6fa47e4 add 5
pick 6934312 add 3
s 6fa47e4 add 5

Enter vim

add 3

add 5

The content is modified to

add 3 & add 5

git log

add 3 & add 5
add 2
add 1
add base

fixup, similar to squash, but directly deprecates the corresponding commit and merges the changes directly into earlier commits. It is not recommended to use them unless you are very skilled.

git rebase -i HEAD~2

pick 311adc9 add 1
pick 7f9d45d add 2 ~ new comment

Change to

pick 311adc9 add 1
s 7f9d45d add 2 ~ new comment

git log

add 1
add bash

exec, execute shell command, no actual purpose is found (for development)

git rebase -i HEAD~2

add 

Change to

exec echo "Hello World"
add 

save

Executing: echo "Hello World"
Hello World

drop, delete a submission directly

Just like pick deletion, pick is to delete this line, drop is to delete this line by using the command description.

Modify history submit user

git commit --amend --author "Iron <@>" --no-edit