2009-08-29 109 views

回答

149

您可以結帳到測試分支然後提交。移動到另一個分支時,您不會失去無限制的更改。

假如你是在主分支:

git checkout test 
git add . 
git add deletedFile1 
git add deletedFile2 
... 
git commit -m "My Custom Message" 

我真的不知道關於刪除的文件,但我想,當你使用git add .

+11

有時,結帳將失敗,因爲您的更改與該分支發生衝突。您可以嘗試簽出-m進行合併。 – 2010-11-04 14:00:05

+1

刪除的文件可以在您執行'git -u add。'時進行管理。 – 2012-08-17 19:44:37

+2

我試過了,但出現錯誤: 錯誤:您對本地文件的更改將被結帳覆蓋。請在您更改分支之前進行修改或存儲它們。 – ishwr 2013-12-19 11:16:14

5
git checkout TEST 
git add file1 file2 
git commit 
+1

+1僅用於簡單 – 2012-08-17 19:45:27

194

你也可以創建一個自己不包括新的分支,並切換到它這樣做:

git checkout -b new_branch 
git add . 

我用這一切的時候,因爲我總是忘記之前,我開始編輯代碼,開始一個新的分支。

+13

+1最簡單的答案。 – 2012-08-17 19:45:06

+2

與@jouni針對其他答案指出的相同問題 - 如果其他更改與原始更改衝突,則可能會遇到難以將分支合併回主。 IMO這個線程更好地回答了這個問題:http:// stackoverflow。com/questions/556923/git-how-to-merge-my-local-working-changes-into-another-branch – jpwynn 2013-04-20 18:58:30

+0

簡短,甜美而令人放心......「我一直都在用這個......」 – 2013-06-17 16:45:08

35

爲什麼不使用git存儲。我認爲它更像直接複製粘貼。

$ git branch 
    develop 
* master 
    feature1 
    TEST 
$ 

您在當前分支中有一些文件需要移動。

$ git status 
# On branch master 
# Changes to be committed: 
# (use "git reset HEAD <file>..." to unstage) 
# 
#  modified: awesome.py 
# 
# Changed but not updated: 
# (use "git add <file>..." to update what will be committed) 
# 
#  modified: linez.py 
# 
$ 
$ git stash 
Saved working directory and index state \ 
    "WIP on master: 934beef added the index file" 
HEAD is now at 934beef added the index file 
(To restore them type "git stash apply") 
$ 
$ git status 
# On branch master 
nothing to commit (working directory clean) 
$ 
$ 
$ git stash list 
[email protected]{0}: WIP on master: 934beef ...great changes 
$ 

移到其他分支。

$ git checkout TEST 

並申請

$ git stash apply 
# On branch master 
# Changed but not updated: 
# (use "git add <file>..." to update what will be committed) 
# 
#  modified: awesome.py 
#  modified: linez.py 
# 

我也很喜歡git stash因爲我用git flow,當你想完成一個特性分支,而在工作目錄中仍然有變化,這抱怨。

就像@Mike Bethany一樣,這一直髮生在我身上,因爲我在解決一個新問題的同時忘記了我還在另一個分支上。所以你可以存儲你的工作,git flow feature finish...,和git stash apply到新的git flow feature start ...分支。

+2

'git stash'是我處理未提交更改的首選方式。當你認爲它是剪切和粘貼的時候,這當然是一種直觀的方法。 – 2013-01-22 13:41:17

+0

這對我來說似乎是一個很好的方法。它工作沒有問題。 – 2014-05-01 09:04:58

+0

不知道你可以做到這一點,它很好地工作。感覺有點比其他方法更直觀。 – glaucon 2014-08-14 04:55:13