簡體   English   中英

git:如何首先插入[non empty]提交,然后將所有其他提交轉移?

[英]git: how to insert a [non empty] commit as the first, shifting all the others?

我想在整個歷史之前在我的master分支上插入一個提交。 我已經更改了該軟件堆棧,並將更改后的版本提交為初始提交。 當我嘗試查看最初更改時,我意識到我的錯誤方法已經很晚了,並注意到我沒有首先提交原始堆棧。 所以我想先榨干它...

我發現了這個非常相似的問題,並認為僅添加某些文件的原始版本並提交就很簡單。

但是,重新部署開始抱怨每個文件都帶有:

CONFLICT (add/add): Merge conflict in XXXX

當我將文件合並到並嘗試重新設置基礎時,繼續,它仍然失敗:

Applying: first version
No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

第一個版本是我的master分支上的第一個提交。

我要做什么甚至有可能嗎?

如果您不打算刪除所做的所有更改,則可以使用以下命令返回項目的第一個提交版本

git revert $id

$ id是您可以使用git log命令找到的提交的ID

然后您可以使用所需的版本進行修改

但是,請謹慎使用,然后再三思

問題是它要應用的第一個提交是添加文件而不是更改文件,因為它通常會存儲一個提交,因此您的初始提交會添加一堆文件,而您嘗試執行的提交也是如此依靠。

我不是100%肯定這是最好的方法,但是應該可以

使孤立分支進行一次提交,以使您的代碼庫變得與您的初始提交相同,將您的第二個提交重新部署到該新提交上。

因為您的第二個提交實際上包含更改,所以不應沖突。

如果那不能滿足我的要求,那么我認為我無法正確理解您的問題。

明智的命令應該是:

# first you need a new empty branch; let's call it `newroot`
git checkout --orphan newroot
git rm -rf .
#add all files you want
git commit -am 'initial commit'
git checkout 1st_commit
git merge -s ours newroot
git rebase --onto newroot --root 2nd commit
git branch -d newroot

您可以預料到“原始堆棧”和“已更改版本”之間的沖突。 也就是說,當您(在某個分支上)第一次提交“原始堆棧”,然后合並到堆棧的“更改版本”中時,所有內容都會發生沖突。 但是,您知道的是“變更版本”是正確的版本, 因此您不必一一解決每個沖突 相反,您將使用:

git checkout --ours -- <files that conflict>       # see note below
git add -- <files that conflict>
git commit -m 'Resolved with --ours'

注意:有時-我們的是正確的; 有時-它們是正確的,這取決於您是合並還是重新定基。 選擇一個,使用它,檢查是否獲得了正確的文件,如果沒有,則使用另一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM