2013-08-29 76 views
4

我確實在尋找解決方案,但我相信我的情況與我閱讀的有所不同。git:如何在提交時忽略對文件的更改?

我在一個特定的分支上工作,並犯了一個錯誤,編輯文件的方式與分支主題無關,現在希望這些更改不會進入提交。我不想失去這些改變,因爲我會盡量在以後的其他分支上以這種或那種方式提交它們。

我試圖git rm --cached但後來我看到狀態deletedgit status - 將文件被刪除從倉庫中?我不希望這樣 - 我希望它保持不變,不管它是否在我的工作分支上的前一個提交中。

+1

只是不要階段的一些文件。這是一個完全正常和平凡的使用Git。對於每一次提交,你都會這樣做。你在使用'git add .'還是'git commit -a'?停止使用它,並開始每一次使用'git add -p' * *。 – meagar

回答

8

如果沒有舞臺的變化,他們不會的承諾的一部分,這樣的事情:

git status 
# On branch development 
# 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: ResearchProposal.tex 

這裏,ResearchProposal.tex有變化,但他們不會被git commit承諾。

如果您在單個文件中有兩組更改,您要提交的一些文件和您不需要的文件的一部分文件的內容爲this

如果你已經在你不希望提交一個文件運行git add,則需要unstage它:

git reset HEAD <file> 

這樣的:

$ git add ResearchProposal.tex 
$ git status 
# On branch development 
# Changes to be committed: 
# (use "git reset HEAD <file>..." to unstage) 
# 
# modified: ResearchProposal.tex 

$ git reset HEAD ResearchProposal.tex 
Unstaged changes after reset: 
M ResearchProposal.tex 

$ git status 
# On branch development 
# 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: ResearchProposal.tex 
+0

該文件在要提交的更改中列出。我不知道它是如何到達那裏的,可能是因爲我早些時候偶然地添加了它,或者是因爲另一個原因。所以我想,現在我需要以某種方式使它出現在「未進行提交的更改中」,這正是我所追求的,對吧? – amn

+1

'git reset '將文件取消,或者'git reset .'取消所有內容,'git add -p'選擇性地取消放置。 – meagar

+0

@amn看到Meagars評論或我編輯的答案。 :) – simont

1

一個可能的解決方案:

git stash 
git checkout <another-branch> 
git stash apply 
git add <one-file> 
git commit 
git stash 
git checkout <original-branch> 
git stash apply 
+0

有沒有辦法讓'git stash'只存儲一個文件?我不需要存放整個工作目錄,只需要一個文件。 – amn

+0

@amn:是的,http://stackoverflow.com/q/3040833/1065190 –

+0

看來,@ simont的答案是我所需要的。感謝您的選擇:-) – amn

5

運行以下命令您的終端:

git update-index --assume-unchanged 

要讓Git重新跟蹤文件,只需運行:

git update-index --no-assume-unchanged path/to/file.txt