美文网首页
git 忽略&移除文件

git 忽略&移除文件

作者: 潘帅次元 | 来源:发表于2018-05-04 16:04 被阅读106次

    前言

    在git仓库中,有时候需要添加某个文件并且追踪;有时候需要移除已经追踪的文件;或者不再追踪已经追踪的文件,但是并不希望该文件从工作区中删除。这一系列操作都知道怎么玩,但是要用的时候往往要到网上各种翻才可以找到自己需要的,这里就直接简单列了一下,以及详细步骤之后查询一目了然。嘿嘿

    1. .gitignore 文件添加忽略文件或者文件夹
    2. git commit -am "提交.gitignore修改"
    3. git rm --cached -f "删除暂存区的文件文件夹"
    

    .gitignore 格式

    文件 .gitignore 的格式规范如下:
    • 所有空行或者以 # 开头的行都会被 Git 忽略。
    • 可以使用标准的 glob 模式匹配。
    • 匹配模式可以以(/)开头防止递归。
    • 匹配模式可以以(/)结尾指定目录。
    • 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

    所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。星号(*)匹配零个或多个任意字符;[abc] 匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);问号(?)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如[0-9] 表示匹配所有 0 到 9 的数字)。使用两个星号(*) 表示匹配任意中间目录,比如a/**/z 可以匹配 a/z,
    a/b/z 或 a/b/c/z等。

    操作步骤

    1.文件已经提交

    #将目录下的文件名`bash.exe.stackdump`添加到 .gitignore 文件中
    D:\git\intellijIdeaPorject>git commit -am "ignore bash.exe.stackdump"
    [master c2a07b4] ignore bash.exe.stackdump
     1 file changed, 1 insertion(+)
    
    D:\git\intellijIdeaPorject>git status
    On branch master
    Your branch is ahead of 'origin/master' by 7 commits.
      (use "git push" to publish your local commits)
    
    nothing to commit, working tree clean
    
    #修改 `bash.exe.stackdump` 文件之后,git status,还是被追踪了。
    D:\git\intellijIdeaPorject>git status
    On branch master
    Your branch is ahead of 'origin/master' by 7 commits.
      (use "git push" to publish your local commits)
    
    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)
    
            modified:   bash.exe.stackdump
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    #解决方案: 需要删除暂存区的文件并且提交
    D:\git\intellijIdeaPorject>git rm --cached bash.exe.stackdump
    rm 'bash.exe.stackdump'
    
    D:\git\intellijIdeaPorject>git status
    On branch master
    Your branch is ahead of 'origin/master' by 8 commits.
      (use "git push" to publish your local commits)
    
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            deleted:    bash.exe.stackdump
    
    
    D:\git\intellijIdeaPorject>git commit -am "deleted:    bash.exe.stackdump"
    [master 9edf2d8] deleted:    bash.exe.stackdump
     1 file changed, 23 deletions(-)
     delete mode 100644 bash.exe.stackdump
    
    D:\git\intellijIdeaPorject>git status
    On branch master
    Your branch is ahead of 'origin/master' by 9 commits.
      (use "git push" to publish your local commits)
    
    nothing to commit, working tree clean
    
    #完成
    

    2.文件在暂存区

    # 将目录下的文件名`bash.exe.stackdump`添加到 .gitignore 文件中
    D:\git\intellijIdeaPorject>git add .gitignore
    
    D:\git\intellijIdeaPorject>git commit -m "ignore bash.exe.stackdump"
    [master c2a07b4] ignore bash.exe.stackdump
     1 file changed, 1 insertion(+)
    
    D:\git\intellijIdeaPorject>git status
    On branch master
    Your branch is ahead of 'origin/master' by 7 commits.
      (use "git push" to publish your local commits)
    
    nothing to commit, working tree clean
    
    
    #修改 `bash.exe.stackdump` 文件之后,git status,照样被追踪了。
    
    D:\git\intellijIdeaPorject>git status
    On branch master
    Your branch is ahead of 'origin/master' by 7 commits.
      (use "git push" to publish your local commits)
    
    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)
    
            modified:   bash.exe.stackdump
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    #解决方案: 需要删除暂存区的文件并且提交 需要添加-f参数强行删除
    D:\git\intellijIdeaPorject>git rm --cached -f bash.exe.stackdump
    rm 'bash.exe.stackdump'
    
    D:\git\intellijIdeaPorject>git status
    On branch master
    Your branch is ahead of 'origin/master' by 8 commits.
      (use "git push" to publish your local commits)
    
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            deleted:    bash.exe.stackdump
    
    
    D:\git\intellijIdeaPorject>git commit -am "deleted:    bash.exe.stackdump"
    [master 9edf2d8] deleted:    bash.exe.stackdump
     1 file changed, 23 deletions(-)
     delete mode 100644 bash.exe.stackdump
    
    D:\git\intellijIdeaPorject>git status
    On branch master
    Your branch is ahead of 'origin/master' by 9 commits.
      (use "git push" to publish your local commits)
    
    nothing to commit, working tree clean
    
    #完成
    

    3.文件在工作区

    #解决方案:将目录下的文件名`bash.exe.stackdump`添加到 .gitignore 文件中
    D:\git\intellijIdeaPorject>git add .gitignore
    
    D:\git\intellijIdeaPorject>git commit -m "ignore bash.exe.stackdump"
    [master c2a07b4] ignore bash.exe.stackdump
     1 file changed, 1 insertion(+)
    
    D:\git\intellijIdeaPorject>git status
    On branch master
    Your branch is ahead of 'origin/master' by 7 commits.
      (use "git push" to publish your local commits)
    
    nothing to commit, working tree clean
    #完成
    

    删除文件补充

    git rm 删除文件--全部删除

    要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除(确切地说,是从暂存区域移除),然后提交。可以用 git rm 命令完成移除操作,并连带从工作目录中删除指定的文件,这样以后就不会出现在未跟踪文件清单中了。

    git rm --cached 删除文件--保留工作区

    我们想把文件从 Git 仓库中删除(亦即从暂存区域移除),但仍然希望保留在当前工作目录中。换句话说,你想让文件保留在磁盘,但是并不想让 Git 继续跟踪。当你忘记添加 .gitignore 文件,不小心把一个很大的日志文件或一堆 .a 这样的编译生成文件添加到暂存区时,这一做法尤其有用。为达到这一目的,git rm 需要使用 --cached 选项:

    相关文章

      网友评论

          本文标题:git 忽略&移除文件

          本文链接:https://www.haomeiwen.com/subject/clwdrftx.html