Git忽略正在追踪的文件而不删除它们 [英] Git ignore files being tracked WITHOUT DELETING THEM

查看:383
本文介绍了Git忽略正在追踪的文件而不删除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解基本的git概念时遇到了一些麻烦:/ b / b

在我的git控制的站点上尝试一些事情之前,我正在测试我的本地Windows机器。



我有:

  gittesting / repo1:
file.txt
ignoreme:
ignore.txt

  gittesting / repo2 
file.txt
忽略:
ignore.txt

Repo2是repo1的副本,并且ignoreme已被跟踪。 ignore.txt文件在repo2中被更改,但我想停止跟踪它,并让git完全忽略它。问题是,如果我创建一个.gitignore文件并添加ignoreme,那就太迟了,因为它已经被跟踪了,所以我必须做git rm --cached ignore,但它被标记为已删除,如果我将提交repo1,该目录将被删除,而不是被孤立..



总结:


  1. ignore.txt在两个回购站点之间有不同的内容。

  2. 我希望ignore.txt内容保持原样并被git完全忽略
  3. >

我在网上查了一下,在IRC中被问到,看了一下非常相关的问题,但是找不到办法做到这一点。我知道这个例子看起来微不足道,但这正是我需要在我的网站上做的,而目录是Forum / cache。






编辑:



这有点破解,我更喜欢更好的答案,但我最终做了:

  cd repo2 
echoignoreme> .gitignore
echoignoreme / *> .gitignore
git rm --cache -r ignoreme
git commit -m现在应该忽略
cd ../repo1
mv ignoreme ignoreme2
git pull。 ./repo2
mv ignoreme2 ignoreme


解决方案

我正确理解你的问题,你需要 repo2 来了解 ignoreme / 目录,但是你不想要Git关心目录的任何修改。而 git rm --cached 不会帮助你,因为你告诉Git从现在开始停止跟踪这些内容。



Git的解决方案是通过子模块来跟踪内容,但在某个时间点固定。这是从 Git Book 解释(强调增加):


虽然 rack 是你工作目录下的子目录,但Git将它视为子模块,当您不在该目录中时,不会跟踪其内容

/ blockquote>

您可以尝试以下操作:
$ b


  1. 复制 ignoreme / 目录移动到一个新的位置,并将其作为一个git存储库


  2. 将它作为子模块添加到 repo2

      git submodule add file:/// path / to / ignoreme ignoreme 
    git commit -mAdd ignoreme / as a submodule


ignoreme 子模块现在已被固定到特定的提交。 repo2 仍在跟踪子模块内的任何内容,但是不会跟踪 ignoreme / 中文件的任何更改 repo2 ,除非您在子模块中提交它们。假设 ignoreme / ignore.txt 被修改了:

  $ git状态
#在分支主机上
#没有为commit提交更改:
#(使用git add< file> ...更新将提交的内容)
# (使用git checkout - < file> ...放弃工作目录中的更改)
#(提交或放弃子模块中未跟踪或修改的内容)

#修改:ignoreme(已修改的内容)

没有更改添加到提交中(使用git add和/或git commit -a)

即使运行 git add。 ignoreme / ignore.txt中的更改不会被添加到索引中,除非它们像这样被提交给子模块:

  $ cd ignoreme 
$ git commit -am更改ignore.txt的时间
$ cd ..
$ git add。
$ git status
#在分支主机上
#要提交的更改:
#(使用git reset HEAD< file> ...停用)

#modified:ignoreme

然而如果你想忘记子模块中的本地修改:

  $ cd ignoreme 
$ git reset --hard
$ cd ..


I'm having some trouble understanding basic git concepts :/

I'm testing on my local Windows machine before trying some things on my git-controlled site.

I have:

gittesting/repo1:
    file.txt
    ignoreme:
        ignore.txt

and

gittesting/repo2
    file.txt
    ignoreme:
        ignore.txt

Repo2 is a copy of repo1, and ignoreme is already being tracked. The ignore.txt file becomes changed in repo2, but I want to stop tracking it and for git to completely ignore it. The problem is that if I create a .gitignore file and add ignoreme, it's too late because it's already being tracked, so I would have to do git rm --cached ignore, but then it's marked as deleted and if I pulled the commit to repo1, the directory would be deleted instead of being left alone..

To sum it up:

  1. The ignore.txt have different content between the two repos.
  2. I want the ignore.txt contents to remain as they are and be completely ignored by git

I've looked online, asked in the IRC, and looked at the very related questions, but can't find a way to do this. I know the example seems trivial, but it's exactly what I need to do on my site, where the directory is Forum/cache instead.


edit:

This is a bit of a hack and I'd prefer a better answer, but I ended up doing:

cd repo2
echo "ignoreme" > .gitignore
echo "ignoreme/*" > .gitignore
git rm --cache -r ignoreme
git commit -m "Should ignore now"
cd ../repo1
mv ignoreme ignoreme2
git pull ../repo2
mv ignoreme2 ignoreme

解决方案

If I understand your question correctly, you want repo2 to know about the ignoreme/ directory, but you don't want Git to care about any modifications to the directory. And git rm --cached won't help you because you're telling Git to stop tracking this content from now on.

Git's solution for keeping track of content, but fixed at a certain point, is through submodules. This except from the Git Book explains (emphasis added):

Although rack is a subdirectory in your working directory, Git sees it as a submodule and doesn’t track its contents when you’re not in that directory. Instead, Git records it as a particular commit from that repository.

You could try the following:

  1. Copy the ignoreme/ directory to a new location, and make it a git repository

  2. Add it back as a submodule in repo2:

    git submodule add file:///path/to/ignoreme ignoreme
    git commit -m"Add ignoreme/ as a submodule"
    

The ignoreme submodule is now fixed to a particular commit. repo2 is still tracking whatever content is inside the submodule, but any changes to files in ignoreme/ will not be tracked in repo2 unless you commit them in the submodule. Let's say ignoreme/ignore.txt was modified somehow:

$ 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)
#   (commit or discard the untracked or modified content in submodules)
#
#       modified:   ignoreme (modified content)
#
no changes added to commit (use "git add" and/or "git commit -a")

Even if you run git add ., the changes in ignoreme/ignore.txt will not be added to the index unless they are committed to the submodule like so:

$ cd ignoreme
$ git commit -am"Time to change ignore.txt"
$ cd ..
$ git add .
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   ignoreme
#

However if you want to forget the local modifications in the submodule:

$ cd ignoreme
$ git reset --hard
$ cd ..

这篇关于Git忽略正在追踪的文件而不删除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆