2012-06-18 139 views
26

我正在開發一個大型的python項目,如果.pyc和*〜文件真的讓我感到噁心。我想刪除它們。我已經看到git clean的-X標誌會刪除未跟蹤的文件。正如你可以想象的,我沒有跟蹤.pyc*~文件。這將成爲訣竅。問題是我有一個local_settings.py文件,我想在git clean之後保留這個文件。Git:用git clean排除一個文件

所以,這就是我所得到的。

的.gitignore:

*.pyc 
*~ 
local_settings.py 

當我執行以下命令:

git clean -X -n -e local_settings.py 

我得到的結果名單:

將消除local_settings.py
將刪除要求.txt〜
會刪除(其他的[R一堆)〜文件
將消除(其他一堆)PYC文件

我不想刪除local_settings.py文件。我嘗試了很多方法來做到這一點,但我無法弄清楚如何實現它。

git clean -X -n -e local_settings.py 
git clean -X -n -e "local_settings.py" 
git clean -X -n --exclude=local_settings.py 
git clean -X -n --exclude="local_settings.py" 

而且似乎沒有任何工作。

編輯:

爲後人,做正確的方式是(感謝@Rifat):

git clean -x -n -e local_settings.py # Shows what would remove (-n flag) 
git clean -x -f -e local_settings.py # Removes it (note the -f flag) 
+2

擁抱'〜.pyc's,配置編輯器放在其他目錄下的臨時/備份文件(或不根本就沒有),忘記了'乾淨'。不要餵你的OCD – KurzedMetal

+0

你是否已經提交了* .pyc和*〜? –

+0

'git update-index --skip-worktree local_settings.py'有幫助嗎? –

回答

13

請問您可以用小號x代替大寫字母。像 - git clean -x

git clean -x -n -e local_settings.py # Shows what would remove (-n flag) 
git clean -x -f -e local_settings.py # Removes it (note the -f flag) 

the git documentation

-x 
     Don't use the standard ignore rules read from .gitignore (per 
     directory) and $GIT_DIR/info/exclude, but do still use the ignore 
     rules given with -e options. This allows removing all untracked 
     files, including build products. This can be used (possibly in 
     conjunction with git reset) to create a pristine working directory 
     to test a clean build. 

    -X 
     Remove only files ignored by git. This may be useful to rebuild 
     everything from scratch, but keep manually created files. 
+0

這就是它!非常感謝你! – santiagobasulto

0

如果你正在運行的Python 2.6+只是設置環境變量,PYTHONDONTWRITEBYTECODE ,到true。你可以只添加以下內容類似.profile.bashrc禁用它完全是爲了您的個人資料:

export PYTHONDONTWRITEBYTECODE=true 

或者,如果你只是想這樣做,對你工作的特定項目,你需要每次在shell中運行以上命令(或者在virtualenv init腳本中使用virtualenv和virtualenvwrapper時),或者在調用python時簡單傳遞參數-B,例如

python -B manage.py runserver 
0

如果您COMMITED的PYC的等已經,請執行下列操作:

添加* pyc文件,*〜和local_settings.py到.gitignore。 然後做你的Git倉庫:

find . -name '*.pyc' | xargs rm 
find . -name '*~' | xargs rm 

然後執行:

git commit -am "get rif of them" 

現在他們不應該打擾你了

8
git clean -X -n --exclude="!local_settings.py" 

作品。當我搜索並獲得this page時,我發現了這一點。

+0

不錯!謝謝。 :)我不得不放棄'!'在我的外殼,但它的工作。 – Vimes

1

我在.git/info/exclude(例如我的IDE項目文件)中放入了屬於此類別的本地文件。他們可以做一個乾淨的像這樣:

git ls-files --others --exclude-from=.git/info/exclude -z | \ 
    xargs -0 --no-run-if-empty rm --verbose 

其中:

  • - 其他:顯示未跟蹤文件
  • --exclude-來自:提供一個標準的Git忽略樣式文件從排除列表
  • -z/-0:使用\ 0,而不是\ n拆分名
  • --no-運行,如果空:如果列表爲空,不要運行RM

您可以創建一個別名,例如:

git config --global alias.myclean '!git ls-files --others --exclude-from=.git/info/exclude -z | xargs -0 --no-run-if-empty rm --verbose --interactive' 

的--interactive意味着你必須做的git myclean -f強制刪除。

參考:http://git-scm.com/docs/git-ls-files(加上默認的.git /信息的第一行/排除)