GIT RM -R --CACHED 去掉已经托管在GIT上的文件

1.gitignore文件

  在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法。这个文件每一行保存了一个匹配的规则例如:

# 此为注释 – 将被 Git 忽略

            *.a       # 忽略所有 .a 结尾的文件
            !lib.a    # 但 lib.a 除外
            /TODO     # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
            build/    # 忽略 build/ 目录下的所有文件

            doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

    这样设置了以后 所有的 .pyc 文件都不会添加到版本库中去。

 

2.gitignoreglobal全局忽略文件

 

另外 git 提供了一个全局的 .gitignore,你可以在你的用户目录下创建 ~/.gitignoreglobal 文件,以同样的规则来划定哪些文件是不需要版本控制的。

需要执行 git config --global core.excludesfile ~/.gitignoreglobal来使得它生效。

其他的一些过滤条件

    * ?:代表任意的一个字符
    * *:代表任意数目的字符
    * {!ab}:必须不是此类型
    * {ab,bb,cx}:代表ab,bb,cx中任一类型即可
    * [abc]:代表a,b,c中任一字符即可

    * [ ^abc]:代表必须不是a,b,c中任一字符

    由于git不会加入空目录,所以下面做法会导致tmp不会存在 tmp/*             //忽略tmp文件夹所有文件

    改下方法,在tmp下也加一个.gitignore,内容为
                        *
                        !.gitignore
    还有一种情况,就是已经commit了,再加入gitignore是无效的,所以需要删除下缓存
                        git rm -r --cached ignore_file

 

注意: .gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。

    正确的做法是在每个clone下来的仓库中手动设置不要检查特定文件的更改情况。
    git update-index --assume-unchanged PATH    在PATH处输入要忽略的文件。

    另外 git 还提供了另一种 exclude 的方式来做同样的事情,不同的是 .gitignore 这个文件本身会提交到版本库中去。用来保存的是公共的需要排除的文件。而 .git/info/exclude 这里设置的则是你自己本地需要排除的文件。 他不会影响到其他人。也不会提交到版本库中去。

    .gitignore 还有个有意思的小功能, 一个空的 .gitignore 文件 可以当作是一个 placeholder 。当你需要为项目创建一个空的 log 目录时, 这就变的很有用。 你可以创建一个 log 目录 在里面放置一个空的 .gitignore 文件。这样当你 clone 这个 repo 的时候 git 会自动的创建好一个空的 log 目录了。


3.git rm -r --cached显神威

但是有时候,gitignore考虑不全面,发现有不该提交的文件已经提交后,仅仅在.gitignore中加入忽略是不行的。这个时候需要执行:

git rm -r --cached filename 

去掉已经托管的文件,然后提交即可。

posted @   silentmuh  阅读(824)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
Live2D
欢迎阅读『GIT RM -R --CACHED 去掉已经托管在GIT上的文件』
  1. 1 Walk Thru Fire Vicetone
  2. 2 爱你 王心凌
  3. 3 Inspire Capo Productions - Serenity
  4. 4 Welcome Home Radical Face
  5. 5 粉红色的回忆 李玲玉
Walk Thru Fire - Vicetone
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Van Der Voort, Joren Johannes

作曲 : Victor Pool/Justin Gammella/Ruben Christopher den Boer/Meron Mengist/Joren van der Voort

Talk to me

Spill the secrets you've been keeping

Life cuts deep

Let me help pick up the pieces

You're not alone, I'm by your side

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

No matter what, I'll make it right

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

I'm not an angel, I'm not a saint

I've been a closed book full of mistakes

But when you're broken, when you're in pain

Oooh, ooh

I'll walk through fire with you

I'll walk through fire

I'll walk through fire with you

I'll walk through fire

You know I

Don't pretend to be a savior

But let me in, yeah

I promise nobody can break us

You're not alone, I'm by your side

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

No matter what, I'll make it right

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

I'm not an angel, I'm not a saint

I've been a closed book full of mistakes

But when you're broken, when you're in pain

Oooh, ooh

I'll walk through fire with you

I'll walk through fire with you

I'll walk through fire

I'll walk through fire with you

I'll walk through fire with you

You're not alone, I'm by your side

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

I'm not an angel, I'm not a saint

I've been a closed book full of mistakes

But when you're broken, when you're in pain

Oooh, ooh

I'll walk through fire with you

I'll walk through fire with you

I'll walk through fire

I'll walk through fire with you

I'll walk through fire with you

点击右上角即可分享
微信分享提示