2013-12-19 77 views
2

我在分支「feature」。在我做了所有改變之後,我決定在這個分支上進行我的工作。Git commit在我的情況下

我首先檢查我已經通過執行命令所做的所有修改:

git status 

以上的git命令打印出如下結果:

On branch feature 
# Changes not staged for commit: 
# (use "git add <file>..." to update what will be committed) 
# (use "git checkout -- <file>..." to discard changes in working directory) 
# 
# modified: MyApp/src/Main.java 
# modified: MyApp/src/Model.java 

# Untracked files: 
# (use "git add <file>..." to include in what will be committed) 
# 
# MyApp/src/other/file1 
# MyApp/src/other/file2 

正如你看到的上面,有兩個未追蹤檔案file1file2。我不想讓他們在我的git倉庫中。但由於某些原因我忘了這兩個文件&直接提交我的變化是:

git add . 
git commit -m "Some changes" 

在這一點上,所有的變化,包括file1 & file2上犯下。現在,我怎麼能救自己排除file1 & file2但仍然保持其他修改的變化被提交?

(請不要建議我刪除file1 & file2從磁盤,我需要他們在我的本地磁盤上)

+0

請參閱撤消在https://www.kernel.org/pub/software/scm/git/docs/git-reset.html中的commti和redo請參閱 – dekdev

回答

5

後您承諾並意識到你不想加入這些文件,你可以像修復這樣的:

git reset HEAD^ --soft 
git reset MyApp/src/other/file1 MyApp/src/other/file2 
git commit -m "Some changes" 

git reset HEAD^ --soft將撤消最後一次提交:您的工作目錄將恢復到它在提交前的狀態。由於--soft標誌,暫存區域將保留原樣,因此您可以使用git reset取消兩個文件的暫存並重新提交。

如果你不希望這些文件添加到版本控制,那麼你可能要忽略它們,將它們添加到該文件.gitignore您的項目:

echo MyApp/src/other/file1 >> .gitignore 
echo MyApp/src/other/file2 >> .gitignore 

PS:謝謝你爲@hekdev--soft小費。

+1

git reset --soft? – dekdev

+0

'--soft'和'--mixed'(默認值)之間的唯一區別在於soft保持文件暫存。由於問題在於這些文件在不應該出現的時候進行,因此使用'--soft'將需要另一個'git reset'來升級這些文件。 –

+0

我不明白「git reset .../file2」是什麼意思?這個命令是否會恢復file2以未跟蹤? –

1

可以

git rm --chached path_to_file 

通知Git停止跟蹤file1file2。它們將顯示爲已刪除,但它們仍將存在於目錄中。然後,您將通過git add添加刪除。

您可以選擇將文件添加到您的.gitignore,以便git將來不會跟蹤它們。 然後你會修改你的最後一個與改變承諾:

git commit --amend 

希望它可以幫助你!

相關問題