日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【git 整理提交】git rebase -i 命令详解

發布時間:2023/12/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【git 整理提交】git rebase -i 命令详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

git rebase -i 詳解

官方文檔

變基時可用的命令

變基時有六個命令可用:

  • pick

    pick只是意味著包括提交。重新進行命令時,重新安排pick命令的順序會更改提交的順序。如果選擇不包括提交,則應刪除整行。

  • reword

    該reword命令與相似pick,但是使用后,重新設置過程將暫停并為您提供更改提交消息的機會。提交所做的任何更改均不受影響。

  • edit

    如果您選擇edit提交,則將有機會修改提交,這意味著您可以完全添加或更改提交。您還可以進行更多提交,然后再繼續進行變基。這使您可以將大型提交拆分為較小的提交,或者刪除在提交中所做的錯誤更改。

  • squash

    該命令使您可以將兩個或多個提交合并為一個提交。提交被壓縮到其上方的提交中。Git使您有機會編寫描述這兩個更改的新提交消息。

  • fixup

    這類似于squash,但是要合并的提交已丟棄其消息。提交僅合并到其上方的提交中,并且較早提交的消息用于描述這兩個更改。

  • exec

    這使您可以對提交運行任意的Shell命令。

準備

# 我們初始化一個項目 git init## 制造一些提交 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"## 查看現在的提交 git log commit a75ed742838ebc1ef1073502623478f73e1ec21f Author: Date: Wed Mar 4 10:02:51 2020 +0800add 5commit 8b485bb4768b2abf8f6400dcba069f1a650ed5ec Author: Date: Wed Mar 4 09:59:27 2020 +0800add 4commit 63ce9fb010da550c668aca66758c45fbfad46e2b Author: Date: Wed Mar 4 09:59:04 2020 +0800add 3commit 9cd34c4d42f52cfb40026dae613c8ad29d7cbc66 Author: Date: Wed Mar 4 09:58:45 2020 +0800add 2commit 77bd0eb1a97e1676367ea236c1c47c155eaa8430 Author: Date: Wed Mar 4 09:58:23 2020 +0800add 1

現在我們已經有了一些,提交了,接下來開始練習

pick 更改提交順序、刪除提交

pick只是意味著包括提交。重新進行命令時,重新安排pick`命令的順序會更改提交的順序。如果選擇不包括提交,則應刪除整行。

假定,我們現在要改變提交 5.txt 和 4.txt 的順序,該怎么操作

更改涉及到了兩次提交,最早提交次數為2(4.txt是倒數第二次提交)

  • 告訴git 我要改變倒數第2次后的提交
  • git rebase -i HEAD~2

    接著,git給你一個文本,告訴你我知道了,你說的這些可以有以下操作

    下面是執行命令后的樣子

    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 ~ ~ ~

    解釋:我們剛剛執行的命令中 HEAD~2代表選擇離HEAD最近的兩條提交

    下面注釋的是提示,我們不需要管,只要專注前兩行就ok

  • 把 第一行 和 第二行 交換順序
  • 小技巧:這個是vi編輯器,首先Esc ,進入命令模式,移動到第一行 按dd,本行就被剪切,pick a75ed74 add 5就變成了第一行,接著按 p剛剛剪切的就成了第二行,快速交換順序

    不會用vi建議百度篇教程學習一哈

    變成下面的樣子

    pick a75ed74 add 5 pick 8b485bb add 4

    接著 Esc,:wq 保存退出

    成功!

    git log查看,4 和 5 的順序改變了

    假定,我們現在要刪除 某一個提交,該怎么操作

    我們來刪除 add 4 的那條提交

    git rebase -i HEAD~2

    出現如下

    pick a75ed74 add 5 pick 8b485bb add 4# Rebase 575fd8b..bb2a77d onto 575fd8b (1 command(s)) # .....略

    我們刪除 第二行

    接著 Esc,:wq 保存退出

    git log查看,4 和 5 的順序改變了

    record 修改提交消息(提交內容不變)

    該reword命令與相似pick,但是使用后,重新設置過程將暫停并為您提供更改提交消息的機會。提交所做的任何更改均不受影響。

    假定,我們現在要 修改某個提交的消息,該怎么操作

    修改一下 add 2 的提交消息

    git log 查看 add 2 距離 HEAD 有多少的距離,得:3

    git rebase -i HEAD~3

    出現如下

    pick 9cd34c4 add 2 pick 63ce9fb add 3 pick 575fd8b add 5# Rebase 77bd0eb..575fd8b onto 77bd0eb (3 command(s)) # .... 略

    我們只需要修改 第一行 add 2 ,其余保持不變

    r 9cd34c4 add 2 pick 63ce9fb add 3 pick 575fd8b add 5# Rebase 77bd0eb..575fd8b onto 77bd0eb (3 command(s)) # .... 略

    r 是 record簡寫

    接著 Esc,:wq 保存退出

    git會說 開始執行,接著彈出一個編輯窗口

    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 #

    我們就可以修改 add 2 了

    下面的注釋是對當前狀態的一些說明

    大致是說,上一次執行的命令 是 r 9cd34c4 add 2

    下面還有兩條命令

    當前命令改變的committed的文件 是 2.txt 等等

    修改 add 2

    add 2 ~ new comment

    接著 Esc,:wq 保存退出

    成功

    git log查看, 消息 “add 2” 變為了 “add 2 ~ new comment”

    只要不動pick的 順序,就代表什么都不做

    edit修改提交

    如果您選擇edit提交,則將有機會修改提交,這意味著您可以完全添加或更改提交。您還可以進行更多提交,然后再繼續進行變基。這使您可以將大型提交拆分為較小的提交,或者刪除在提交中所做的錯誤更改。

    假定 我想要在兩個提交之間 再加提交,怎么辦

    假定,我們要在 add 3 和 add 5 之間 添加一條提交

    git rebase -i HEAD~2 pick 6934312 add 3 pick 5ce6dde add 5# Rebase 7f9d45d..5ce6dde onto 7f9d45d (2 command(s)) # ....

    修改為

    e 6934312 add 3 pick 5ce6dde add 5

    接著 Esc,:wq 保存退出

    有如下

    $ git rebase -i HEAD~2 Stopped at 6934312135c150bf74bead26e371df1443273ca4... add 3 You can amend the commit now, withgit commit --amendOnce you are satisfied with your changes, rungit rebase --continuexxxxxx MINGW32 ~/Desktop/git-demo (master|REBASE-i 1/2)

    可以看到,我們的master分支多了REBASE-i 1/2

    我們嘗試做一些修改,給3.txt 增加一些內容,然后提交

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

    接著,我們繼續 rebase

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

    git log 查看,在 add 5 和 add 3 中間 增加了我們剛剛的修改

    假定 我想要單純的修改這次提交內容和消息,怎么辦

    參照上面的做到這一步,我們選擇提交的方式 加一個參數 git commit --amend修改

    git add 3.txtgit commit --amend# 這樣 就不會在多出一次提交 # 本次對 3.txt的修改會記錄 到 add 3 這次提交記錄中

    接著結束這次 rebase

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

    squash合并提交

    squash

    該命令使您可以將兩個或多個提交合并為一個提交。提交被壓縮到其上方的提交中。Git使您有機會編寫描述這兩個更改的新提交消息。

    假定,我想合并某幾個提交,怎么辦

    如下

    我們合并 add 5 和 add 3

    git rebase - i HEAD~2 pick 6934312 add 3 pick 6fa47e4 add 5# Rebase 7f9d45d..6fa47e4 onto 7f9d45d (2 command(s))

    修改為

    pick 6934312 add 3 s 6fa47e4 add 5

    接著 Esc,:wq 保存退出

    開始執行變更

    然后 在彈出來的編輯框里 寫提交信息

    # 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

    我們可以修改提交消息,默認是把兩個消息都合并

    接著 Esc,:wq 保存退出

    git log查看,合并成功

    fixup合并提交,只保留較早的提交信息

    這類似于squash,但是要合并的提交已丟棄其消息。提交僅合并到其上方的提交中,并且較早提交的消息用于描述這兩個更改。

    最后兩次提交

    git rebase -i HEAD~2 pick 7f9d45d add 2 ~ new comment pick 311adc9 add 3# Rebase 77bd0eb..311adc9 onto 77bd0eb (2 command(s)) # ---------------------- # 變更為 # -----------------------pick 7f9d45d add 2 ~ new comment f 311adc9 add 3# 保存

    exec 執行任意shell命令

    git rebase -i HEAD~3# 彈出編輯框 #---------------------------- pick 81fe4d0 添加test2.txt和test3.txt pick 77bd0eb add 1 pick e7c68b8 add 2 ~ new comment# Rebase 258a059..e7c68b8 onto 258a059 (3 command(s)) #....# ---------------- # 加一行 命令 # --------------- x echo "Hello is echo do ......." pick 81fe4d0 添加test2.txt和test3.txt pick 77bd0eb add 1 pick e7c68b8 add 2 ~ new comment## 執行了我們剛剛鍵入的命令 Executing: echo "Hello is echo do ......." Hello is echo do ....... Successfully rebased and updated refs/heads/master.

    其他

    # 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

    刪除提交

    用法與 p,r,e,s,f,x 一致

    命令寫錯了怎么辦

    看提示

    You can fix this with 'git rebase --edit-todo'. # 用 git rebase --edit--todo 來重新編輯命令

    總結

    以上是生活随笔為你收集整理的【git 整理提交】git rebase -i 命令详解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。