git rebase -i command details

Git rebase-i details

[official document [(https://help.github.com/en/github/using-git/about-git-rebase ා commands-available-while-rebasing)

Commands available at base change

There are six commands available when base changing:

  • pick

    Pick just means include commit. When you redo a command, rearranging the order of the pick commands changes the order of commits. If you choose not to include submissions, the entire row should be deleted.

  • reword

    The reword command is similar to pick, but when used, the reset process pauses and gives you the opportunity to change the submitted message. Any changes made by the commit will not be affected.

  • edit

    If you choose edit commit, you will have the opportunity to modify the commit, which means you can add or change the commit completely. You can also commit more before you proceed with the rebasing. This allows you to split a large commit into smaller ones, or to remove erroneous changes made in the commit.

  • squash

    This command allows you to combine two or more commits into one. The commit is compressed into the commit above it. Git gives you the opportunity to write a new commit message that describes these two changes.

  • fixup

    This is similar to square, but the commit to merge has dropped its message. Commit is only merged into the commit above it, and older committed messages are used to describe the two changes.

  • exec

    This allows you to run any Shell command on the commit.

Get ready

# We initialize a project
git init


## Make some submissions
touch base.txt
git add .
git commit -m "add base"

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

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

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

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

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


## View current submissions
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

Now that we've got some, we've submitted them, so let's practice

pick change submission order, delete submission

Pick just means include commit. When the command is rescheduled, the order of the pick command will change the order of submission. If you choose not to include submissions, the entire row should be deleted.

Suppose we want to change the order of submitting 5.txt and 4.txt now. What should we do

The change involves two commits, the earliest commit is 2 (4.txt is the last commit)

  1. Tell git I want to change the commit after the penultimate
git rebase -i HEAD~2

Then, git will give you a text to tell you that I know. You can do the following for these

Here's what it looks like after executing the command

pick 8b485bb add 4
pick a75ed74 add 5

# Rebase 63ce9fb..a75ed74 onto 63ce9fb (2 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~
~
~

Explanation: in the command we just executed, HEAD~2 means to select the two closest submissions to HEAD

The note below is a hint. We don't need to worry, just focus on the first two lines

  1. Exchange the first and second lines in order

Tips: This is the vi editor. First, Esc enters the command mode. Move to the first line, press dd, and the line will be cut. pick a75ed74 add 5 becomes the first line. Then press p, and the line just cut will become the second line. Fast exchange order

Will not use vi to suggest Baidu to learn a course

Become the following

pick a75ed74 add 5
pick 8b485bb add 4

Then Esc,:wq save exit

Success!

git log view, the order of 4 and 5 has changed

[the external link image transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-a1wkhMKq-1583299216695)(D:\typoraImages83289148057.png))

Suppose we want to delete a submission now. What should we do

Let's delete the submission of add 4

git rebase -i HEAD~2

Appear as follows

pick a75ed74 add 5
pick 8b485bb add 4

# Rebase 575fd8b..bb2a77d onto 575fd8b (1 command(s))
# ... slightly

We delete the second line

Then Esc,:wq save exit

git log view, the order of 4 and 5 has changed

record modify submit message (the submitted content remains unchanged)

The reword command is similar to pick, but when used, the reset process pauses and gives you the opportunity to change the submitted message. Any changes made by the commit will not be affected.

Let's say that we need to modify a submitted message now. What should we do

Modify the submit message of add 2

git log is used to check the distance between add 2 and HEAD, and the result is: 3

git rebase -i HEAD~3

Appear as follows

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

# Rebase 77bd0eb..575fd8b onto 77bd0eb (3 command(s))
# ... slightly.

We just need to modify the first line, add 2, and keep the rest unchanged

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

# Rebase 77bd0eb..575fd8b onto 77bd0eb (3 command(s))
# ... slightly.

r is short for record

Then Esc,:wq save exit

git will say start execution, and then an edit window will pop up

add 2

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Mar 4 09:58:45 2020 +0800
#
# interactive rebase in progress; onto 77bd0eb
# Last command done (1 command done):
#    r 9cd34c4 add 2
# Next commands to do (2 remaining commands):
#    pick 63ce9fb add 3
#    pick 575fd8b add 5
# You are currently editing a commit while rebasing branch 'master' on '77bd0eb'.
#
# Changes to be committed:
#       new file:   2.txt
#

We can modify add 2

The following notes are some descriptions of the current state

Roughly speaking, the last command executed was r 9cd34c4 add 2

Here are two more commands

The file of committed changed by the current command is 2.txt, etc

Modify add 2

add 2 ~ new comment

Then Esc,:wq save exit

Success

git log, the message "add 2" changes to "add 2 ~ new comment"

[the external link image transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-CNVnOqe9-1583299216700)(D:\typoraImages83290791964.png))

As long as you don't move the pick order, you don't do anything

edit modification submission

If you choose edit commit, you will have the opportunity to modify the commit, which means you can add or change the commit completely. You can also commit more before you proceed with the rebasing. This allows you to split a large commit into smaller ones, or to remove erroneous changes made in the commit.

What if I want to add another submission between two submissions

Suppose we add a commit between add 3 and add 5

git rebase -i HEAD~2
pick 6934312 add 3
pick 5ce6dde add 5

# Rebase 7f9d45d..5ce6dde onto 7f9d45d (2 command(s))
# ....

Modified to

e 6934312 add 3
pick 5ce6dde add 5

Then Esc,:wq save exit

As follows

$ git rebase -i HEAD~2
Stopped at 6934312135c150bf74bead26e371df1443273ca4... add 3
You can amend the commit now, with

        git commit --amend

Once you are satisfied with your changes, run

        git rebase --continue


xxxxxx MINGW32 ~/Desktop/git-demo (master|REBASE-i 1/2)

As you can see, our master branch has more REBASE-i 1/2

Let's try to make some changes, add some content to 3.txt, and then submit

git add 3.txt

git commit -m "edit 3.txt"
[detached HEAD 7262a57] edit 3.txt
 1 file changed, 1 insertion(+)


Then, let's move on to rebase

git rebase --continue
Successfully rebased and updated refs/heads/master.

In git log view, the changes we just made are added between add 5 and add 3

Suppose I want to simply modify the submitted content and message, what should I do

Refer to the above to do this step. We choose the submission method and add a parameter git commit --amend

git add 3.txt

git commit --amend

# This way, there will be no more submissions
# This modification to 3.txt will be recorded in the add 3 submission record

And then finish this rebase

git rebase --continue
Successfully rebased and updated refs/heads/master.

Square merge submission

squash

This command allows you to combine two or more commits into one. The commit is compressed into the commit above it. Git gives you the opportunity to write a new commit message that describes these two changes.

Suppose I want to merge some submissions

as follows

We combine add 5 and add 3

git rebase - i HEAD~2
pick 6934312 add 3
pick 6fa47e4 add 5

# Rebase 7f9d45d..6fa47e4 onto 7f9d45d (2 command(s))

Modified to

pick 6934312 add 3
s 6fa47e4 add 5

Then Esc,:wq save exit

Start implementation of changes

Then write the submission in the pop-up edit box

# This is a combination of 2 commits.
# The first commit's message is:

add 3

# This is the 2nd commit message:

add 5

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Mar 4 09:59:04 2020 +0800
#
# interactive rebase in progress; onto 7f9d45d
# Last commands done (2 commands done):
#    pick 6934312 add 3
#    s 6fa47e4 add 5
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on '7f9d45d'.
#
# Changes to be committed:
#       new file:   3.txt
#       new file:   5.txt

We can modify the submitted message. The default is to merge both messages

Then Esc,:wq save exit

git log view, merge succeeded

fixup merge commit, keep only the earlier commit information

This is similar to square, but the commit to merge has dropped its message. Commit is only merged into the commit above it, and older committed messages are used to describe the two changes.

Last two submissions

git rebase -i HEAD~2
pick 7f9d45d add 2 ~ new comment
pick 311adc9 add 3

# Rebase 77bd0eb..311adc9 onto 77bd0eb (2 command(s))
#  ----------------------
# Change to
# -----------------------

pick 7f9d45d add 2 ~ new comment
f 311adc9 add 3

# Preservation

exec executes arbitrary shell commands

git rebase -i HEAD~3


# Pop up edit box
#----------------------------
pick 81fe4d0 Add to test2.txt and test3.txt
pick 77bd0eb add 1
pick e7c68b8 add 2 ~ new comment

# Rebase 258a059..e7c68b8 onto 258a059 (3 command(s))
#....

# ----------------
# Add a line of command
# ---------------
x echo "Hello is echo do ......."
pick 81fe4d0 Add to test2.txt and test3.txt
pick 77bd0eb add 1
pick e7c68b8 add 2 ~ new comment


## Executed the command we just typed
Executing: echo "Hello is echo do ......."
Hello is echo do .......
Successfully rebased and updated refs/heads/master.

Other

# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

drop

Delete submit

Usage consistent with p,r,e,s,f,x

What to do if the order is wrong

Look at the hint

You can fix this with 'git rebase --edit-todo'.
# Use git rebase --edit--todo to re edit the command
Published 45 original articles, won praise 5, visited 4244
Private letter follow

Tags: git shell REST github

Posted on Tue, 03 Mar 2020 23:39:46 -0800 by blindtoad