【Git】变基

更改提交信息

确认目前状况

$ git log --oneline
e0136fa (HEAD -> master) add3
05674c6 add2
9d19a21 add1

编辑

$ git rebase -i HEAD~3

▼改变之前

../monaka/git_test/.git/rebase-merge/git-rebase-todo
pick 9d19a21 add1
pick 05674c6 add2
pick e0136fa add3

▼修改后(将第三个提交更改为编辑)

../monaka/git_test/.git/rebase-merge/git-rebase-todo
pick 9d19a21 add1
pick 05674c6 add2
edit e0136fa add3

保存并退出后,会显示接下来要做的事情。

$ git rebase -i HEAD~3
Stopped at e0136fa...  add3
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

修改提交信息

→打开提交消息编辑页面,进行修改。

(还有,在那个时候,我在一个神秘的地方)

$ git commit --amend 

修正完成后

$ git commit --amend 
[detached HEAD 6b9ea42] change add3 commit message
 Date: Sun Aug 14 12:07:04 2022 +0900
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 third.html

继续进行rebase。

执行以下操作后,回到原始分支。

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

确认

$ git log --oneline
e0136fa (HEAD -> master) change add3 commit message
05674c6 add2
9d19a21 add1

调整提交的顺序

确认目前状况

$ git log --oneline
e0136fa (HEAD -> master) change add3 commit message
05674c6 add2
9d19a21 add1

编辑

$ git rebase -i HEAD~3

▼修改前

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 9d19a21 add1
pick 05674c6 add2
pick 6b9ea42 change add3 commit message

▼变更后(第一次提交和第二次提交互换位置)

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 05674c6 add2
pick 9d19a21 add1
pick 6b9ea42 change add3 commit message

编辑完成之后

$ git rebase -i HEAD~3
Successfully rebased and updated refs/heads/master.

确认

$ git log --oneline -n 3
f4abf55 (HEAD -> master) change add3 commit message
5c93e54 add1
7777adb add2

整合提交

确定目前情况。

$ git log --oneline -n 3
f4abf55 (HEAD -> master) change add3 commit message
5c93e54 add1
7777adb add2

编辑

$ git rebase -i HEAD~3

▼之前更改

.../monaka/git_test/.git/rebase-merge/git-rebase-todo
pick 7777adb add2
pick 5c93e54 add1
pick f4abf55 change add3 commit message

▼改变后

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 7777adb add2
squash 5c93e54 add1
squash f4abf55 change add3 commit message

提交消息编辑页面(是否进行更改均可)

      /home/monaka/git_test/.git/COMMIT_EDITMSG      
# This is a combination of 3 commits.
# This is the 1st commit message:

add2

# This is the commit message #2:

add1

# This is the commit message #3:

change add3 commit message

确认 (què

$ git log --oneline
3ecbd1e (HEAD -> master) add2
96283d2 (origin/master) add feature 

将提交拆分

确认当前状况

$ git log --oneline
3ecbd1e (HEAD -> master) add2

编辑

$ git rebase -i HEAD~1

▼修改之前

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 3ecbd1e add2

修改后

...it_test/.git/rebase-merge/git-rebase-todo Modified
edit 3ecbd1e add2

取消被标记为”edit”的提交,将其恢复为未暂存状态。

    • git reset コマンド:コミットを取り消してステージングしていない状況まで戻す

 

    HEAD^(キャレット):editと記載したコミットを指す
$ git reset HEAD^

确认 (què

$ git status
interactive rebase in progress; onto 96283d2
Last command done (1 command done):
   edit 3ecbd1e add2
No commands remaining.
You are currently editing a commit while rebasing branch 'master' on '96283d2'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        first.html
        second.html
        third.html

nothing added to commit but untracked files present (use "git add" to track)

提交first.html和second.html文件

$ git add first.html second.html 
$ git commit -m 'add1&2'
[detached HEAD c8c96fd] add1&2
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 first.html
 create mode 100644 second.html

提交third.html文件

$ git add third.html 
$ git commit -m 'add3'
[detached HEAD eb18196] add3
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 third.html

重做

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

确认

$ git log --oneline
eb18196 (HEAD -> master) add3
c8c96fd add1&2

删除提交

确认

$ git log --oneline -n 3
eb18196 (HEAD -> master, origin/master) add3
c8c96fd add1&2
96283d2 add feature

编辑

$ git rebase -i HEAD~3

▼改动之前

.../monaka/git_test/.git/rebase-merge/git-rebase-todo
pick 96283d2 add feature
pick c8c96fd add1&2
pick eb18196 add3

▼改变之后

...it_test/.git/rebase-merge/git-rebase-todo Modified
pick 96283d2 add feature

确认

$ git log --oneline -n 3
96283d2 (HEAD -> master) add feature

请参阅