2013-03-24 23 views
0

我通過閱讀和玩我的機器上的測試回購來學習Git。如何從Git分支中刪除文件

有一件事令我困惑。我有2個分支。 mastertesting

我結帳testing branch並刪除文件系統上的一些文件。我然後運行git add .

我希望它來記錄這些文件從然而,在下面的圖片中testing分支刪除,你可以看到他們是不提供當我運行git commit -m "Deleted some files from testing branch"

我希望這些文件被刪除並允許我提交該更改,那麼當我再次結賬master時,它應顯示仍存在的文件。

有人可以告訴我我做錯了什麼,以及如何做我想做的事?

enter image description here

回答

4

你刪除文件的更改不會在Git的指數顯示的原因是因爲你刪除自己的文件,而無需通知飯桶。

現在有兩種方法可以解決這個問題。

選項1:

使用git add -u <FILES>命令使該指數跟蹤的文件,反映了你的工作樹的變化。 Git會檢測工作樹中的文件刪除,並更新索引以對應此狀態。

# Example command to update the git index to 
# reflect the state of the files in the work-tree 
# for the two files named helpers.php and registry.class.php 
git add -u helpers.php registry.class.php 

選項2:

要刪除一個文件,而不是刪除使用delrm命令在你的shell手動,你可以直接問git的刪除並記錄在索引中的變化使用git rm命令。請注意,即使您已經自己刪除了這些文件(例如您提到的情況),也可以執行該命令。

# Example command to remove two files named 
# helpers.php and registry.class.php 
git rm helpers.php registry.class.php 

使用上述任一你的選擇應該看到您的狀態命令應自動指示文件已在臨時區域被刪除:

git status 
# On branch testing 
# Changes to be committed: 
# (use "git reset HEAD <file>..." to unstage) 
# 
# deleted: helper.php 
# deleted: registry.class.php 
# 

那麼你應該能夠提交更改使用commit命令。

git commit -m "Deleted some files from testing branch" 
3

您與-u標誌階段他們。

例如爲:git add -u .