git 跟踪、忽略、删除、取消跟踪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9785792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:38:11  来源:igfitidea点击:

git track, ignore, delete, untrack

git

提问by Martin Petts

I seem to have ballsed up my git repo by tracking a file, later ignoring it and then deleting it. The thing is, I want the file to be present in a remote repository (the live site), but just don't want to track.

我似乎通过跟踪文件来充实我的 git repo,后来忽略它然后删除它。问题是,我希望该文件存在于远程存储库(实时站点)中,但只是不想跟踪。

Now my master branch is trying to remove all the files from the repository, which I know will delete them on the remote branch when I push changes... I just want to untrack them, but cannot do that as they're already deleted on master and git rm -r --cached says 'did not match any files'.

现在我的主分支正在尝试从存储库中删除所有文件,我知道当我推送更改时会在远程分支上删除它们......我只想取消跟踪它们,但不能这样做,因为它们已经被删除了master 和 git rm -r --cached 说“没有匹配任何文件”。

How can I untrack these deleted files without removing them from the remote repository?

如何取消跟踪这些已删除的文件而不将它们从远程存储库中删除?

回答by Karthik Bose

You just don't want to track a file, but you want to keep the file in the remote repository (non-bare).

您只是不想跟踪文件,但希望将文件保存在远程存储库(非裸)中。

Use this git command. After you do this, git stops checking the file for possible modifications.

使用这个 git 命令。执行此操作后,git 将停止检查文件以进行可能的修改。

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

At any time, you can track again by setting --no-assume-unchagedflag

在任何时候,您都可以通过设置--no-assume-unchaged标志再次跟踪

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

These command do not affect the remote repository.

这些命令不会影响远程存储库。

For more information: http://kar-git.blogspot.in/2012/11/how-to-set-git-to-ignore-few-files-like.html

更多信息:http: //kar-git.blogspot.in/2012/11/how-to-set-git-to-ignore-few-files-like.html

回答by heinrich5991

git rmis used to delete files from the repository.

git rm用于从存储库中删除文件。

To stop tracking them, you can, after deleting, add them to the .gitignorefile in the root of the repository.

要停止跟踪它们,您可以在删除后将它们添加到.gitignore存储库根目录中的文件中。

回答by ralphtheninja

Even if you have untracked a file with git rm, you can still checkout a previous version of that file, e.g.

即使您使用 git rm 取消跟踪文件,您仍然可以检出该文件的先前版本,例如

In your local repo: git rm foo git commit -m 'removed foo' git push origin master

在您的本地仓库中: git rm foo git commit -m 'removed foo' git push origin master

In the remote repository:

在远程存储库中:

git pull origin master // foo will be removed
git checkout abcd1234 -- foo // checkout foo from commit abcd1234 (foo is staged)
git reset HEAD // unstage foo
git status

# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       foo

Then add to .gitignore to untrack

然后添加到 .gitignore 以取消跟踪