Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“如何制作Git”;“忘了吧”;关于一个被跟踪但现在在.gitignore中的文件?_Git_Gitignore_Git Rm - Fatal编程技术网

“如何制作Git”;“忘了吧”;关于一个被跟踪但现在在.gitignore中的文件?

“如何制作Git”;“忘了吧”;关于一个被跟踪但现在在.gitignore中的文件?,git,gitignore,git-rm,Git,Gitignore,Git Rm,有一个文件被git跟踪,但现在该文件位于.gitignore列表中 但是,该文件在编辑后会一直显示在git状态中。如何强制git完全忘记它?将它移出,提交,然后再移入。这在过去对我很有效。可能有一种“gittier”方法可以实现这一点。将文件移动或复制到一个安全的位置,这样您就不会丢失它。然后git rm文件并提交。如果您恢复到以前的某个提交,或者恢复到尚未删除该文件的另一个分支,则该文件仍将显示。但是,在以后的所有提交中,您将不会再次看到该文件。如果文件在git ignore中,那么您可以将其

有一个文件被
git
跟踪,但现在该文件位于
.gitignore
列表中


但是,该文件在编辑后会一直显示在
git状态中。如何强制git完全忘记它?

将它移出,提交,然后再移入。这在过去对我很有效。可能有一种“gittier”方法可以实现这一点。

将文件移动或复制到一个安全的位置,这样您就不会丢失它。然后git rm文件并提交。如果您恢复到以前的某个提交,或者恢复到尚未删除该文件的另一个分支,则该文件仍将显示。但是,在以后的所有提交中,您将不会再次看到该文件。如果文件在git ignore中,那么您可以将其移回文件夹,git将看不到它。

我通过使用完成了这一点。我使用的确切命令取自手册页:

警告:这将从您的整个历史记录中删除该文件

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

此命令将重新创建整个提交历史记录,在每次提交之前执行
git rm
,因此将删除指定的文件。在运行命令之前不要忘记备份它,因为它将丢失。

.gitignore
将阻止未跟踪的文件被添加到git跟踪的文件集中(没有添加-f
),但是git将继续跟踪任何已经被跟踪的文件

要停止跟踪文件,需要将其从索引中删除。这可以通过此命令实现

git rm --cached <file>
git-rm——缓存
如果要删除整个文件夹,则需要递归删除其中的所有文件

git rm -r --cached <folder>
git rm-r——缓存
将在下次提交时从头部修订中删除该文件


警告:虽然这不会从本地删除物理文件,但如果由于其他人可能需要而无法
git rm
跟踪文件,它将从下一个
git pull

上的其他开发人员计算机上删除该文件(警告,即使您
git rm--cached
,当其他人得到此更改时,他们的文件将在其文件系统中被删除)。这通常是由于配置文件覆盖、身份验证凭据等原因造成的。请查看人们解决此问题的方法

总结如下:

  • 让应用程序查找被忽略的文件config-overide.ini,并在提交的文件config.ini上使用该文件(或者,查找~/.config/myapp.ini或$myconfig文件)
  • 提交文件config-sample.ini并忽略文件config.ini,必要时使用脚本或类似文件复制该文件
  • 尝试使用gittributes clean/smudge magic为您应用和删除更改,例如,将配置文件涂抹为备用分支的签出,并将配置文件清理为HEAD的签出。这是一件棘手的事情,我不建议新手使用
  • 将配置文件保留在一个专用于它的部署分支上,该分支从未合并到master。当您想要部署/编译/测试时,您可以合并到该分支并获取该文件。这基本上是涂抹/清除方法,除了使用人工合并策略和额外的git模块
  • 反建议:不要使用“假定不变”,它只会以眼泪结束(因为git撒谎会导致糟糕的事情发生,比如你的改变永远丢失)

下面的一系列命令将从Git索引(不是从工作目录或本地repo)中删除所有项,然后更新Git索引,同时考虑Git ignores.PS.Index=Cache

第一名:

git rm -r --cached .
git add .
git commit -am "Remove ignored files"
然后:

git rm -r --cached .
git add .
git commit -am "Remove ignored files"

或一个班轮:

git rm -r --cached . && git add . && git commit -am "Remove ignored files"
这项工作是否适合我:

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

git update-index --no-skip-worktree <file>
git更新索引--不跳过工作树

Matt Fear的回答是最有效的IMHO。下面是一个PowerShell脚本,适用于那些在windows中只从git repo中删除与其排除列表匹配的文件的用户

# Get files matching exclusionsfrom .gitignore
# Excluding comments and empty lines
$ignoreFiles =  gc .gitignore | ?{$_ -notmatch  "#"} |  ?{$_ -match  "\S"} | % {
                    $ignore = "*" + $_ + "*"
                    (gci -r -i $ignore).FullName
                }
$ignoreFiles = $ignoreFiles| ?{$_ -match  "\S"}

# Remove each of these file from Git 
$ignoreFiles | % { git rm $_}

git add .

这将获取被忽略文件的列表并将其从索引中删除,然后提交更改。

我总是使用此命令删除那些未跟踪的文件。 单行、Unix样式、干净输出:

git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached
它列出所有被忽略的文件,用带引号的行替换每个输出行,以处理内部带有空格的路径,并将所有内容传递到
git rm-r--cached
以从索引中删除路径/文件/目录。

什么对我不起作用 (在Linux下),我想使用这里的帖子,建议使用
ls文件--忽略--排除标准的| xargs git rm-r--缓存
方法。但是,(一些)要删除的文件在其名称中嵌入了换行符/LF/
\n
。这两种解决方案都没有:

git ls-files --ignored --exclude-standard | xargs -d"\n" git rm --cached
git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached
应对这种情况(获取有关未找到文件的错误)

所以我提议 这使用
-z
参数来ls文件,使用
-0
参数来xargs来安全/正确地处理文件名中的“讨厌”字符

在手册页面的git ls文件(1)
中,说明:

如果未使用-z选项,则在 路径名分别表示为\t、\n和\\

因此,我认为如果文件名中有任何这些字符,就需要我的解决方案

  • 更新你的
    .gitignore
    文件–例如,添加一个你不想跟踪到
    .gitignore
    的文件夹

  • git rm-r--cached.
    –删除所有跟踪的文件,包括需要的和不需要的文件。只要您在本地保存,您的代码就会安全

  • git add.
    –除了
    中的文件之外,所有文件都将被添加回.gitignore



  • 感谢@AkiraYamamoto为我们指明了正确的方向。

    我想,也许git不能
    git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached
    git commit -am "Remove ignored files"
    
    git update-index --skip-worktree <file>
    
    git update-index --no-skip-worktree <file>
    git stash
    git pull 
    
    git update-index --skip-worktree <file>
    
    $ java -jar bfg.jar --strip-blobs-bigger-than 100M
    
    $ java -jar bfg.jar --delete-files *.mp4
    
    git rm -r --cached .
    
    git add .
    
    git commit -m ".gitignore fix"
    
    find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
    
    echo ".DS_Store" >> ~/.gitignore_global
    echo "._.DS_Store" >> ~/.gitignore_global
    echo "**/.DS_Store" >> ~/.gitignore_global
    echo "**/._.DS_Store" >> ~/.gitignore_global
    git config --global core.excludesfile ~/.gitignore_global
    
    # Create empty repo
    mkdir gitignore-test
    cd gitignore-test
    git init
    
    # Create a file and commit it
    echo "hello" > file
    git add file
    git commit -m initial
    
    # Add the file to gitignore and commit
    echo "file" > .gitignore
    git add .gitignore
    git commit -m gitignore
    
    # Remove the file and commit
    git rm file
    git commit -m "removed file"
    
    # Reintroduce the file and check status.
    # .gitignore is now respected - status reports "nothing to commit".
    echo "hello" > file
    git status
    
    git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached 
    git ls-files -z --ignored --exclude-standard | xargs -0 git stage 
    git stage .gitignore 
    git commit -m "new gitignore and remove ignored files from index"
    
    git rm -r --cached path-to-those-files
    
    git add .
    git commit -m "removed unnecessary files from git"
    git push origin
    
    git rm {PATH_OF_THE_FILE}/slnx.sqlite -f
    git commit -m "remove slnx.sqlite"
    
    git fetch --all
    git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch full/path/to/file' --prune-empty --tag-name-filter cat -- --all
    git push origin --force --all
    git push origin --force --tags
    git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
    git reflog expire --expire=now --all
    git gc --prune=now
    
    git ls-files -z | xargs -0 git update-index --assume-unchanged
    
    git rm -r --cached **/*.lock
    
    git rm --cached test.txt
    git add .
    git commit -m "test.txt removed"
    git push
    
    git rm -r --cached .idea
    git add .
    git commit -m ".idea removed"
    git push
    
    git update-index --skip-worktree .envrc
    git rm --cached .envrc
    
     git filter-branch --index-filter 'git rm --cached --ignore-unmatch .envrc' HEAD