上下文:我正在為master添加一個簡單的功能。 幾分鐘後,我意識到這不是那麼簡單,應該更好地進入一個新的分支。
這總是發生在我身上,我不知道如何切換到另一個分支并采取所有這些未經修改的更改與我離開主分支清潔。 我認為
git stash && git stash branch new_branch
會完成它,但這就是我得到的:
~/test $ git status
# On branch master
nothing to commit (working directory clean)
~/test $ echo "hello!" > testing
~/test $ git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: testing
#
no changes added to commit (use "git add" and/or "git commit -a")
~/test $ git stash
Saved working directory and index state WIP on master: 4402b8c testing
HEAD is now at 4402b8c testing
~/test $ git status
# On branch master
nothing to commit (working directory clean)
~/test $ git stash branch new_branch
Switched to a new branch 'new_branch'
# On branch new_branch
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: testing
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/[email protected]{0} (db1b9a3391a82d86c9fdd26dab095ba9b820e35b)
~/test $ git s
# On branch new_branch
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: testing
#
no changes added to commit (use "git add" and/or "git commit -a")
~/test $ git checkout master
M testing
Switched to branch 'master'
~/test $ git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: testing
#
no changes added to commit (use "git add" and/or "git commit -a")
你知道有沒有辦法實作這個目标?
#1樓
嘗試:
git stash
git checkout -b new-branch
git stash apply
#2樓
不需要藏匿。
git checkout -b new_branch_name
不會觸及您當地的更改。 它隻是從目前HEAD建立分支并在那裡設定HEAD。 是以我想這就是你想要的。
---編輯解釋結賬大師的結果---
您是否感到困惑,因為
checkout master
不會丢棄您的更改?
由于更改隻是本地的,是以git不希望您太容易丢失它們。 更改分支後,git不會覆寫您的本地更改。
checkout master
的結果是:
M testing
,這意味着您的工作檔案不幹淨。 git确實改變了HEAD,但沒有覆寫你的本地檔案。 這就是為什麼你的上一個狀态仍然顯示你的本地更改,雖然你是
master
。
如果您确實要放棄本地更改,則必須使用
-f
強制結帳。
git checkout master -f
由于您的更改從未送出過,是以您将失去它們。
嘗試傳回到您的分支,送出更改,然後再次檢出主伺服器。
git checkout new_branch
git commit -a -m"edited"
git checkout master
git status
您應該在第一次結賬後收到
M
消息,但在
checkout master
後不再顯示,并且
git status
應顯示沒有修改過的檔案。
---編輯以清除工作目錄(本地檔案)的混亂---
在回答您的第一條評論時,本地變化隻是......好吧,本地。 Git不會自動儲存它們,您必須告訴它以便以後儲存它們。 如果您進行更改并且未顯式送出或存儲它們,則git不會對它們進行版本控制。 如果更改HEAD(
checkout master
),則自未儲存後不會覆寫本地更改。
#3樓
你可以做兩件事:
git checkout -b sillyname
git commit -am "silly message"
git checkout -
要麼
git stash -u
git branch sillyname [email protected]{0}
(
git checkout -
< - 破折号是您上一個分支的快捷方式)
(
git stash -u
< -
-u
表示它也會進行非分段更改)
#4樓
如果您正在使用GitHub Windows用戶端(就像我一樣)并且您正在進行未送出的更改,而您希望将其移至新分支,則可以通過GitHub用戶端簡單地“建立新分支”。 它将切換到新建立的分支并保留您的更改。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5yTxhVek9CXt92YuIXdn1Waus2YhR3cuk2Lc9CX6MHc0RHaiojIsJye.png)
#5樓
在最新的Windows GitHub用戶端中,如果您有未送出的更改,并選擇建立新分支,則會提示您如何處理此确切方案:
如果您隻是簡單地切換分支也是如此。