簡體   English   中英

將更改從一個提交的分支轉移到另一個分支

[英]Transfer changes from one committed branch to another branch

我在feature / project分支做了修改並提交,然后我意識到我在分支做錯了,我必須在feature / nfts-list-and-detail分支做我使用了下面的方法,但是這是如果我們沒有承諾

$ git stash

$ git checkout feature / nfts-list-and-detail

$ git stash pop

如何將我的更改轉移到feature / nfts-list-and-detail

正如其他用戶 torek 所解釋的那樣, “Git 就是關於提交的”。

所以這對於你來說意味着什么? 提交不在分支上或分支中。 一個分支指向一個提交,如果你跟隨父提交,你會得到這個分支的歷史。 但分支不過是動態標簽。 它們可以被創建、重命名、刪除。

只要您沒有將任何內容推送到遠程存儲庫,就無需擔心:cherry-pick 提交到正確的分支,然后回滾您的其他分支。 Cherry-picking 會創建一個新的提交,其中包含與原始提交相同(或相似)的更改。

這里有兩種方法(注意分支名不能有空格):

git checkout feature/nfts-list-and-detail # switch to correct branch
git cherry-pick feature/project # create copy of latest commit of wrong branch
git branch -f feature/project feature/project # undo latest commit on wrong branch
git checkout feature/project # switch to wrong branch
git reset --mixed HEAD^ # undo latest commit, keep changes in working tree
git stash
git checkout feature/nfts-list-and-detail
git stash pop
git add files you want to commit
git commit

還有其他不同程度的復雜性和搬起石頭砸自己腳的可能性的方法。

如果你已經推到了錯誤的分支,事情就會變得有點棘手。 但是,如果您不關心顯示提交存在的歷史記錄,那么簡單的git revert就可以完成這項工作。

暫無
暫無

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

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