簡體   English   中英

如何在不在.gitignore中添加文件的情​​況下在本地忽略文件

[英]How to ignore files locally without adding it in .gitignore

我有一個github存儲庫,我在本地檢查。

  • 我在environment.local-dev.ts文件中更改了我的環境變量。 (它通常指向localhost,但由於我沒有安裝本地api存儲庫,我更新了文件以指向我的同事本地api)
  • 現在,更改的配置特定於我的需要。 所以,我不想將這些更改提交到Github存儲庫

我搜索了兩個選項:

  • .gitignore添加文件。 但問題是,我仍然會看到.gitignore ,我可能會意外地犯下這個問題。
  • 將其添加到.git/info/exclude 我這樣做了,但是當我執行git status時,我仍然看到更改的文件

我正在研究的Github存儲庫已經由其他人創建。 所以無法改變初始配置。 其他人也在同一個存儲庫上工作,因此無法提交可能會對其他人產生負面影響的更改。

所以,問題是找到一種not commit tracked by git文件的方法。 有沒有辦法做到這一點 ?

編輯 :正如@Philippe指出的那樣, skip-worktreeassume-unchanged skip-worktree合適。 在這個答案中,一切都是一樣的,但你只需切換選項。

.gitignore.git/info/exclude僅用於排除未跟蹤的文件。

要忽略對跟蹤文件的本地修改,可以使用git update-index --assume-unchanged FILES...
但是存在忘記需要在文件中提交的修改的風險。
要使修改再次可見: git update-index --no-assume-unchanged FILES...

$ git status
On branch master
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:   file

no changes added to commit (use "git add" and/or "git commit -a")

$ git update-index --assume-unchanged file

$ git status
On branch master
nothing to commit, working tree clean

$ git ls-files -v
h file

$ git update-index --no-assume-unchanged file

$ git ls-files -v
H file

$ git status
On branch master
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:   file

no changes added to commit (use "git add" and/or "git commit -a")

您可以定義一些別名:

[alias]
    forget = update-index --assume-unchanged
    unforget = update-index --no-assume-unchanged
    forgotten = ! git ls-files -v | grep ^[a-z]

更好的方法是使本地忽略配置覆蓋文件。

嘗試這個:

git update-index --skip-worktree environment.local-dev.ts

有關進一步閱讀,請參閱git update-index文檔和其他相關問題,例如Git - “假設未更改”和“跳過工作樹”之間的差異

與其他答案相反,不是assume-unchanged功能(保留用於檢查成本高昂的大文件),您應該使用但skip-worktree

請參閱https://stackoverflow.com/a/13631525/717372

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM