简体   繁体   English

暂时从 git 中取消跟踪文件

[英]Untrack files from git temporarily

I have setup a local git on my machine.我已经在我的机器上设置了一个本地 git。 When I initialized git, I added pre-compiled libs and binaries.当我初始化 git 时,我添加了预编译的库和二进制文件。 However, now during my development I don't want to check in those files intermittently.但是,现在在我的开发过程中,我不想间歇性地检查这些文件。 I dont want to remove these files from repo.我不想从 repo 中删除这些文件。 Is there any way to not keep a track of these files till I complete my development.在我完成开发之前,有什么方法可以不跟踪这些文件。 (I think I can not use .gitignore as it works only for those files which are not in git. I want to temporarily disable tracking of files.) (我想我不能使用 .gitignore,因为它只适用于那些不在 git 中的文件。我想暂时禁用文件跟踪。)

git update-index should do what you want git update-index应该做你想做的

This will tell git you want to start ignoring the changes to the file这将告诉 git 您要开始忽略对文件的更改
git update-index --assume-unchanged path/to/file

When you want to start keeping track again当您想再次开始跟踪时
git update-index --no-assume-unchanged path/to/file

Github Documentation: update-index Github 文档:更新索引

you could keep your files untracked after你可以在之后保持你的文件不被跟踪

git rm -r --cached <file>

add your files with添加您的文件

git add -u

them push or do whatever you want.他们推动或做任何你想做的事。

git rm --cached

However, you shouldn't be committing compiled binaries and external dependancies in the first place.但是,您不应该首先提交已编译的二进制文件和外部依赖项。 Use a tool like Bundler to pull those in instead.使用像 Bundler 这样的工具来代替它们。

使用以下命令取消跟踪文件

git rm --cached <file path>

Not an answer to the original question.不是原始问题的答案。 But this might help someone.但这可能对某人有所帮助。

To see the changes you have done ( know which files are marked as --assume-unchanged )查看您所做的更改(知道哪些文件被标记为 --assume-unchanged

git ls-files -v

The resulted list of files will have a prefix with one character (ex : H or h) If it is a lowercase (ie h) then the file was marked --assume-unchanged结果文件列表将有一个带一个字符的前缀(例如:H 或 h)如果它是小写字母(即 h),则该文件被标记为 --assume-unchanged

The git-book mentions this in section 2.4: "Undoing Things". git-book 在第 2.4 节:“撤消事情”中提到了这一点。 Basically, what you have to do is reset the state of the index for some files back to the HEAD state, that is to the state of the latest checkout (or commit).基本上,您需要做的是将某些文件的索引状态重置回 HEAD 状态,即最新检出(或提交)的状态。 This undoes staging the file to the current index.这将撤消将文件暂存到当前索引。 The command for this task is git reset .[1]此任务的命令是git reset .[1]

So, the command you have to execute is:因此,您必须执行的命令是:

git reset HEAD /path/to/file

The newer versions of git (I believe since 1.6 or so) gives this command (and many more) as a tip when executing git status .较新版本的 git(我相信从 1.6 左右开始)在执行git status时将此命令(以及更多)作为提示。 These versions are very user-friendly.这些版本非常人性化。 A personal tip: if you are only staging a few files, use git add -i .个人提示:如果您只是暂存几个文件,请使用git add -i This will launch the interactive staging tool, which makes thing particularly easy.这将启动交互式暂存工具,这使事情变得特别容易。 Also, I highly recommend reading the book, as it is very explicit on the use of git in practical situations.另外,我强烈推荐阅读这本书,因为它非常明确地介绍了 git 在实际情况中的使用。

[1] http://www.git-scm.com/book [1] http://www.git-scm.com/book

I am assuming that you are asking how to remove ALL the files in the build folder or the bin folder, Rather than selecting each files separately.我假设你是问如何删除所有文件在生成文件夹或bin文件夹,而不是分别选择每个文件。

You can use this command:你可以使用这个命令:

git rm -r -f /build\\*

Make sure that you are in the parent directory of the build directory.确保您位于构建目录的父目录中。
This command will, recursively "delete" all the files which are in the bin/ or build/ folders.此命令将递归地“删除” bin/ 或 build/ 文件夹中的所有文件。 By the word delete I mean that git will pretend that those files are "deleted" and those files will not be tracked.通过删除这个词,我的意思是 git 会假装这些文件被“删除”并且这些文件不会被跟踪。 The git really marks those files to be in delete mode. git 确实将这些文件标记为处于删除模式。

Do make sure that you have your .gitignore ready for upcoming commits.请确保您的 .gitignore 为即将到来的提交做好了准备。
Documentation : git rm文档:git rm

To remove all Untrack files.删除所有 Untrack 文件。 Try this terminal command试试这个终端命令

 git clean -fdx

An alternative to assume-unchanged is skip-worktree . assume-unchanged的替代方法是skip-worktree The latter has a different meaning, something like "Git should not track this file. Developers can, and are encouraged, to make local changes."后者有不同的含义,比如“Git 不应该跟踪这个文件。开发人员可以并且被鼓励进行本地更改。”

In your situation where you do not wish to track changes to (typically large) build files, assume-unchanged is a good choice.在您不希望跟踪对(通常很大)构建文件的更改的情况下, assume-unchanged是一个不错的选择。

In the situation where the file should have default contents and the developer is free to modify the file locally, but should not check their local changes back to the remote repo, skip-worktree is a better choice.在文件应该具有默认内容并且开发人员可以在本地自由修改文件,但不应将其本地更改检查回远程存储库的情况下, skip-worktree是更好的选择。

Another elegant option is to have a default file in the repo.另一个优雅的选择是在 repo 中有一个默认文件。 Say the filename is BuildConfig.Default.cfg .假设文件名为BuildConfig.Default.cfg The developer is expected to rename this locally to BuildConfig.cfg and they can make whatever local changes they need.开发人员应该在本地将其重命名为BuildConfig.cfg ,他们可以进行任何他们需要的本地更改。 Now add BuildConfig.cfg to .gitignore so the file is untracked.现在将BuildConfig.cfg添加到.gitignore以便文件不被跟踪。

See this question which has some nice background information in the accepted answer.请参阅此问题该问题在已接受的答案中有一些不错的背景信息。

git reset [file] git reset [文件]

Get file back from staging area to working directory 将文件从暂存区返回到工作目录

I often forget to add my node modules to the .gitignore file, this solves that problem. 我经常忘记将节点模块添加到.gitignore文件中,这解决了该问题。

This worked for me 这对我有用

git reset HEAD 'filename' git reset HEAD'文件名'

要删除未跟踪的文件和文件夹,请设置git clean -fdx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM