1. 程式人生 > >利用rebase來壓縮多次提交

利用rebase來壓縮多次提交

 

我們可以用Git merge –squash來將分支中多次提交合併到master後,只保留一次提交歷史。但是有些提交到github遠端倉庫中的commit資訊如何合併呢?

歷史記錄

首先我們檢視一下master分支的提交歷史:

$ git log
commit 415a0be986a48113829b3c60ee2387c6dbdc81d8
Author: xuxu <[email protected]126.com>
Date:   Mon Jan 26 20:11:29 2015 +0800

    10->1

commit ed09a6cbe0797275ceead3f8c8d829d01f0e604b
Author: xuxu 
<[email protected]126.com> Date: Mon Jan 26 19:57:17 2015 +0800 2015.01.26 commit 1821f6a1c1ed074fe886228cf33b3b3cb71819c4 Author: xuxu <[email protected]126.com> Date: Mon Jan 26 19:53:28 2015 +0800 2015.01.26 commit cc779982fc61e82ec494d6a5654417fa7194d748 Author: xuxu <[email protected]
126.com> Date: Mon Jan 26 19:40:24 2015 +0800 2015.1.26

我們看到上面有四次commit,如何將這個四次合併到一次呢?

壓縮

使用下面的命令,最後一個數字4代表壓縮最後四次提交。

git rebase -i HEAD~4

該命令執行後,會彈出vim的編輯視窗,4次提交的資訊會按照提交時間順序排列,最上面的是最早一次提交,最下面的是最近一次提交。

我們需要修改第2-4行的第一個單詞pick為squash,意思就是將下面的三次提交壓縮到第一行中去,效果就是我們在pick所在的提交就已經做了4次動作,但是看起來就是一次而已。
注意:並不會刪除下面的三次提交內容,只會將四次提交壓縮為一次。

 然後我們儲存退出,git會一個一個壓縮提交歷史。

如果有衝突,需要修改,修改的時候要注意,保留最新的歷史,不然我們的修改就丟棄了。修改以後要記得敲下面的命令:

git add .
git rebase --continue

如果你想放棄這次壓縮的話,執行以下命令:

git rebase --abort

如果所有衝突都已經解決了,會出現如下的編輯視窗:

這個時候我們需要修改一下合併後的commit的描述資訊,我們將其描述為helloworld吧:

儲存退出後會看到我們完整的資訊:

$ git rebase -i HEAD~4
[detached HEAD 9097684] helloworld
 Author: xuxu <[email protected]126.com>
 Committer: unknown <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 9 files changed, 25 insertions(+), 11 deletions(-)
Successfully rebased and updated refs/heads/master.

現在我們來檢視一下歷史:

$ git log
commit 90976848524251b0a62376a9e45ea5c8aae25d87
Author: xuxu <[email protected]126.com>
Date:   Mon Jan 26 19:40:24 2015 +0800

    helloworld

commit f57da9460196b950036fe4c2c8f7e4c9131ee04e
Author: xuxu <[email protected]126.com>
Date:   Mon Jan 26 19:38:27 2015 +0800

    2015.01.26

commit 64104e057f48d6dd0e432af8b55cbccd0a09ee63
Author: xuxu <[email protected]126.com>
Date:   Mon Jan 26 19:31:42 2015 +0800

    2015.01.26

commit 8499d4fa23c4395660d21e0b7dca873a7a3ef2b8
Author: xuxu <[email protected]126.com>
Date:   Mon Jan 26 18:58:40 2015 +0800

    2015.01.26

提交歷史很清楚的顯示出了我們的壓縮成功了。

同步到遠端倉庫

github上的提交歷史還是之前的,如何更新我們壓縮後的歷史記錄呢?

我們採用git push 試試:

To https://github.com/DoctorQ/AndroidTestScrpits.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/DoctorQ/AndroidTestScrpit
s.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

被拒絕了,因為我們合併的歷史記錄已經在遠端倉庫之前了,你無法覆蓋它。
那怎麼辦?如果你確定你的合併過程都是正確無誤的,那麼就可以強制push:

$ git push -f
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

In Git 2.0, Git will default to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 1, done.
Writing objects: 100% (1/1), 232 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/DoctorQ/AndroidTestScrpits.git
 + 415a0be...9097684 master -> master (forced update)

OK,不出意外的話,開啟gitlab應該可以看到結果了。

相關推薦

git中利用rebase壓縮提交

之前我們用git merge –squash來將分支中多次提交合併到master後,只保留一次提交歷史。但是有些提交到github遠端倉庫中的commit資訊如何合併呢? 歷史記錄 首先我們檢視一下master分支的提交歷史: $ git log

利用rebase壓縮提交

  我們可以用Git merge –squash來將分支中多次提交合併到master後,只保留一次提交歷史。但是有些提交到github遠端倉庫中的commit資訊如何合併呢? 歷史記錄 首先我們檢視一下master分支的提交歷史: $ git log commit 415a

jq防止ajax提交的方法

console csdn min complete num href config 中斷 ring 1、第一種,對於onclick事件觸發的的ajax 可以采用如下方法: 即在beforeSend中使點擊按鈕不可用,ajax結果返回後置為可用 $.ajax( { type

ASP.NET webform提交表單問題

的人 事件 client form gin ron 遇到 提交按鈕 orm 最近幾天遇到一個頭疼的問題,項目采用的是webform開發,每個界面都有個提交按鈕,點擊多次提交按鈕導致提交按鈕的OnClick事件執行了多次, 每次OnClick裏面都有一些邏輯處理,執行了

防止表單提交的方法

作者:莎士比亞的人生 來源:CSDN 原文:https://blog.csdn.net/qq_34368762/article/details/79130198  表單重複提交是在多使用者Web應用中最常見、帶來很多麻煩的一個問題。有很多的應用場景都會遇到重複提交問題,比如:

Android 按鈕提交資訊

同事在是要過程中總是多次點選按鈕,知道成功以後自動返回,在這個過程中由於上傳的資料較大,在2秒左右,可以提交七八次,針對這個問題進行優化,在網上看了好多解決方法, 第一種:設定點選間距時間大於1000毫秒(1秒), 用後完全沒用。 第二種:直接把按鈕設定為一次性提交: B

如何在提交後給某個commit新增tag

比如你準備以某條commit內容為基準,釋出版本。但是你卻忘記對其新增tag,這時有兩種方法。第一種是版本回退,然後以需要的commit內容為基礎,釋出版本。這種做法不太推薦,它會丟棄你之後的開發工作,

阻止表單提交按鈕提交

碰巧看到《超實用jQuery程式碼段》關於單個頁面重複提交按鈕,粘張圖吧 說明:   給提交按鈕繫結單擊事件,該事件函式內通過attr()函式 先禁用該提交按鈕的單擊功能,因為此時使用者已經提交過一次了,禁用為防止反覆操作提交功能。   然後使用ajax()函式

sql優化:with as 作 union all的子查詢 避免掃描表

語法: 1.結構: with 別名 as ( 公共sql片段 ) 2.CTE(公共表示式)後面必須直接緊跟使用CTE的SQL語句,否則失效; 如: 1) with cr as (select CountryRegionCode from person.CountryR

mybatis一個業務提交事務commit造成主鍵不順序遞增(觸發器的原因)

package com.dangdang.service; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Set

Swift 防止快速連續點選"提交"按鈕,造成提交

//防止快速連續點選"提交"按鈕,造成多次提交 @IBAction func submit(sender: UIButton) {           //防止連續點選造成多次提交的解決  self.

快取檔案,瞭解git add對同一個檔案分提交的騷操作

場景: 我們在專案開發過程中, 有時候會遇到經理開始說了兩個需求, 我們在專案的基礎上直接將兩個需求做完了, 正當我們沉浸於提交一天的成果感覺人生已經達到了巔峰的氣氛中時, 經理說先上一個一個需求, 另一個需求暫時先不上. 暫且不提心情的落差, 放下了2米4的大砍刀之後,仔細分析了索

jquery ajax 提交問題

由於我的ajax請求被繫結到按鈕的click事件執行。 act_btn.click(function(){ var data = $("#action_content").serialize(); var url = "dor

防止form表單提交

點選提交按鈕兩次。點選重新整理按鈕。使用瀏覽器後退按鈕重複之前的操作,導致重複提交表單。使用瀏覽器歷史記錄重複提交表單。瀏覽器重複的HTTP請求。使用者提交表單時可能因為網速的原因,或者網頁被惡意重新整理,致使同一條記錄重複插入到資料庫中,這是一個比較棘手的問題。我們可以從客

利用介面實現

I介面 i = new MyClass();             i.funk();             Console.ReadKey(); interface I介面     {         void funk();     } class MyClass:

微信支付提示:同一筆交易不能提交

微信支付API上說明: OUT_TRADE_NO_USED 商戶訂單號重複 同一筆交易不能多次提交 請核實商戶訂單號是否重複提交 測試的時候先用微信支付得到預支付id,取消當前支付,再次付款的時候就會提示:“訂單號重複” 網上搜了一下,大概共有三種解決辦法: 1

JS--阻止ajax因資料重複提交的方法

1.需求:應用ajax技術非同步提交資料 2.問題:多次點選元素由於伺服器返還資料延遲問題,造成多次資料請求。 3.解決方法: 1.用變數識別符號 (定時器模擬延遲返還資料)

uni-app圖片壓縮轉base64位 利用遞迴實現張圖片壓縮

//選擇圖片 chooseImage(){ let that =this uni.chooseImage({ sizeType: ['original','compressed'], //可以指定是原圖還是壓縮圖,預設二者都有 count: 9,//預設9 suc

利用java的timer定時器限制使用者的錯誤登陸

這裡主要用到的是timer這個類的幾個功能 具體操作在timertask內執行  用new timer來決定是迴圈執行 還是延遲執行  比如在timertask的run()方法中 根據頁面傳過來的account查詢到資料庫中的資訊 (在資料庫中有一個欄位是判斷錯誤登入次數

利用 git rebase -i 指令合併 commit

由於公司要求一次 push 只能有一次 commit,然而自己由於操作失誤,導致一次修改的內容,分成了兩次 commit,再進行 push 的時候就失敗了 在網上找了一次找到了如下的方法 合併前兩次 commit 的資訊 # git rebase -i