[英]Move uncommitted changes from current branch to another branch that conflicts with those changes
假設我在分支master
並且開始進行一些更改。
我對已經在Emacs中打開的文件進行了更改(因此,在實際操作中,隨着結帳的發生,除非我不斷還原緩沖區,否則Emacs不會意識到)。
該文件確實存在於分支other_branch
,該分支other_branch
打算合並到master
中。 但是,直到我不小心從Emacs中保存了文件之后,該文件才在master
不存在。
更改未提交,但是我意識到我不應該在master
上進行更改,而是打算在開始更改之前簽出其他分支。
我不想丟失當前的工作,但也不想將其提交給master
。
我已經嘗試過使用git stash
然后使用git checkout other_branch
但這給我一個錯誤,說由於未提交的更改,不允許將分支切換到other_branch
:
ems@computer:~$ git checkout other_branch
error: The following untracked working tree files would be overwritten by checkout:
some_file
Please move or remove them before you can switch branches.
Aborting
我開始(偶然地)在master
進行修改的文件已經存在於other_branch
,因此僅檢出other_branch
會破壞我的工作目錄副本,並進行更改,對嗎?
我嘗試了stash
,它似乎確實隱藏了更改,但是當我嘗試結帳時,出現了以上錯誤消息。
摘要
結帳一些新分支, other_branch
跟蹤master
。
在other_branch
創建一個新文件test.txt
,然后將其打開以在Emacs中進行編輯。 提交到other_branch
。
回到終端,結帳主人。
回到Emacs,您會忘記自己現在已經掌握了,開始對test.txt
進行一些更改並保存文件。 就master
而言,這是一個全新的文件。
您如何將這些更改轉移到other_branch
?
如果在結帳期間未跟蹤的文件是一個問題,也許您可以嘗試:
git stash --include-untracked
從git stash
手冊頁 :
如果使用了
--include-untracked
選項,則所有未跟蹤的文件也會被保存,然后使用git clean
,使工作目錄處於非常干凈的狀態
由於這也不起作用(在隱藏彈出窗口中),您可能暗示:
HEAD~
臨時重命名文件?
$ mv test.txt test.txt.tmp
$ git checkout other_branch
$ mv test.txt.tmp test.txt
檢查變更並提交
對我有用的是:
從master
做git add
為未跟蹤的沖突文件git add
文件。 現在已經跟蹤了,執行git stash
並檢出other_branch
。 現在, git stash pop
將嘗試將other_branch
-in- other_branch
版本與other_branch
-in- other_branch
版本other_branch
。 這可能會導致合並沖突,因此您必須打開文件並解決所有沖突,並確保在other_branch
上git add
並commit
它們。 然后回到master
並驗證文件是否未暫存以提交並且在該分支中不存在。
這樣,就可以將它們視為已在存儲中進行了跟蹤,而無需實際從master提交和合並。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.