簡體   English   中英

如何保留但不跟蹤GitHub上的文件?

[英]How to keep, but not track a file on GitHub?

我有一個共享的初始化腳本,其中包含特定於用戶的內容,我想與我的團隊作為模板共享,但沒有讓每個人都用自己的說法(自己的電子郵件地址)進行更新。

  echo "myname" > testfile
  git add testfile
  git commit -m "Add a user-specific file"
  git push push origin master

  echo "testfile" >> .gitignore
  git add .gitignore
  git commit -m "Ignore user-specific file"

  git rm --cached testfile
  # Showing that testfile is staged as deleted!

我遇到了一個非常相似的問題,在該問題中,我想在樹中提供一些“易於構建”的Shell腳本,我希望人們可能會更改,但我不希望他們在工作時提交更改。

我在項目中解決此問題的方法是使用git update-index --assume-unchanged保護構建腳本(如果適用)。

我在存儲庫的根目錄中創建了兩個bash腳本git_assume_build_scripts_unchanged.shgit_undo_assume_build_scripts_unchanged.sh ,它們看起來像這樣:

git_assume_build_scripts_unchanged.sh

#!/bin/bash

set -e

# Cause git to assume that all build scripts are unchanged, so that you can use "git add ." syntax to commit
# without committing local changes to the build scripts

git update-index --assume-unchanged *build*.sh
git update-index --assume-unchanged Toolchain-*.cmake
git update-index --assume-unchanged set_*_env_vars.sh

git_undo_assume_build_scripts_unchanged.sh

#!/bin/bash

set -e

# Cause git to assume that all build scripts are unchanged, so that you can use "git add ." syntax to commit
# without committing local changes to the build scripts

git update-index --no-assume-unchanged *build*.sh
git update-index --no-assume-unchanged Toolchain-*.cmake
git update-index --no-assume-unchanged set_*_env_vars.sh

這個想法是,如果有人需要修改其本地計算機的構建腳本,或修改cmake工具鏈文件或類似文件,則他們將運行第一個腳本。 git update-index --assume-unchanged導致git標記文件,因此,即使在索引中對其進行了更改,即使已對其進行了跟蹤,這些更改也不會被git檢測到,並且在使用git add .時不會被暫存git add . 或類似。

如果有人需要撤消更改,則可以使用undo腳本。

您可能正在尋找的是:

git update-index --assume-unchanged <file>

然后,當您想要相反的操作時,可以執行以下操作:

git update-index --no-assume-unchanged <file>

假定您未更改文件,這會將文件保留在最新版本的存儲庫中。

希望能有所幫助,

干杯

暫無
暫無

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

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