zoukankan      html  css  js  c++  java
  • gitignore 不起作用的解决办法 不再跟踪 让.gitignore生效,跟踪希望被跟踪的文件

    实践

    # https://git-scm.com/docs/gitignore https://git-scm.com/docs/gitignore

    不跟踪log目录下的所有文件,但需要保留这个文件夹;

    # A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.
    /src/logs/**

    # An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded.
    !/src/logs/.gitkeep
    # A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.


    取消多所有文件的跟踪(清空已跟踪)
    git rm -rf --cached .

    让.gitignore生效,跟踪希望被跟踪的文件

    git add .

    docker exec -it --user root 46ae311034e4 git rm -rf --cached .;
    docker exec -it --user root 46ae311034e4 git add .;

    gitignore 不起作用的解决办法 - sloong - 博客园 https://www.cnblogs.com/sloong/p/5523244.html

    Administrator@PC-2016 MINGW64 /d/Data-Structure (master)
    $ git rm -r --cached .
    rm '.gitignore'
    rm '01 绪论/Status.h'
    rm '02 线性表/01 SequenceList/H.h'
    rm '02 线性表/02 Union/H.h'
    rm '02 线性表/03 MergeSqList/H.h'
    rm '02 线性表/04 SinglyLinkedList/H.h'
    rm 'Data-Structure.dev'
    rm 'Data-Structure.exe'
    rm 'Data-Structure.layout'
    rm 'Makefile.win'
    rm 'README.md'
    rm 'learnC/Pointers.c'
    rm 'learnC/Pointers.exe'
    rm 'learnC/switch.c'
    rm 'main.c'
    rm 'main.o'
    
    Administrator@PC-2016 MINGW64 /d/Data-Structure (master)
    $ git add .
    
    Administrator@PC-2016 MINGW64 /d/Data-Structure (master)
    $ git commit -m "update .gitignore:git rm -r --cached ."
    [master e2e61ef] update .gitignore:git rm -r --cached .
     1 file changed, 0 insertions(+), 0 deletions(-)
     delete mode 100644 main.o
    
    Administrator@PC-2016 MINGW64 /d/Data-Structure (master)
    $ git push origin master
    Username for 'https://github.com': toywei
    remote: Invalid username or password.
    fatal: Authentication failed for 'https://github.com/toywei/Data-Structure.git/'
    
    Administrator@PC-2016 MINGW64 /d/Data-Structure (master)
    $ git push origin master
    Username for 'https://github.com': toywei
    Enumerating objects: 3, done.
    Counting objects: 100% (3/3), done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (2/2), 232 bytes | 232.00 KiB/s, done.
    Total 2 (delta 1), reused 0 (delta 0)
    remote: Resolving deltas: 100% (1/1), completed with 1 local object.
    To https://github.com/toywei/Data-Structure.git
       8f666d3..e2e61ef  master -> master
    
    Administrator@PC-2016 MINGW64 /d/Data-Structure (master)
    $
    

      

     git忽略已经被提交的文件 - SegmentFault 思否 https://segmentfault.com/q/1010000000430426

    tl;dr: 正确的做法应该是:git rm --cached logs/xx.log,然后更新 .gitignore 忽略掉目标文件,最后 git commit -m "We really don't want Git to track this anymore!"

    具体的原因如下:

    被采纳的答案虽然能达到(暂时的)目的,但并非最正确的做法,这样做是误解了 git update-index 的含义,而且这样做带来的最直接(不良)后果是这样的:

    1. 所有的团队成员都必须对目标文件执行:git update-index --assume-unchanged <PATH>。这是因为即使你让 Git 假装看不见目标文件的改变,但文件本身还是在 Git 的历史记录里的,所以团队的每个人在 fetch 的时候都会拉到目标文件的变更。(但实际上目标文件是根本不想被 Git 记录的,而不是假装看不见它发生了改变)

    2. 一旦有人改变目标文件之后没有 git update-index --assume-unchanged <PATH> 就直接 push 了,那么接下来所有拉取了最新代码的成员必须重新执行 update-index,否则 Git 又会开始记录目标文件的变化。这一点实际上很常见的,比如说某成员换了机器或者硬盘,重新 clone 了一份代码库,由于目标文件还在 Git 的历史记录里,所以他/她很可能会忘记 update-index

    为什么会这样?答案就在 Git 的 man pages 里:

    首先,git update-index 的定义是:

    Register file contents in the working tree to the index(把工作区下的文件内容注册到索引区)

    这句话暗含的意思是:update-index 针对的是 Git 数据库里被记录的文件,而不是那些需要忽略的文件。

    接着看关于 --assume-unchanged 的几句相关的描述:

    When the "assume unchanged" bit is on, Git stops checking the working tree files for possible modifications, so you need to manually unset the bit to tell Git when you change the working tree file. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

    大致意思是:

    应用了该标识之后,Git 停止查看工作区文件可能发生的改变,所以你必须 手动 重置该标识以便 Git 知道你想要恢复对文件改变的追踪。当你工作在一个大型项目中,这在文件系统的 lstat 系统调用非常迟钝的时候会很有用。

    我们知道 Git 不仅仅是用来做代码版本管理的,很多其他领域的项目也会使用 Git。比如说我公司曾经一个客户的项目涉及到精密零件图纸文档的版本管理,他们也用 Git。有一种使用场景是对一些体积庞大的文件进行修改,但是每一次保存 Git 都要计算文件的变化并更新工作区,这在硬盘慢的时候延迟卡顿非常明显。

    git update-index --assume-unchanged 的真正用法是这样的:

    1. 你正在修改一个巨大的文件,你先对其 git update-index --assume-unchanged,这样 Git 暂时不会理睬你对文件做的修改;
    2. 当你的工作告一段落决定可以提交的时候,重置改标识:git update-index --no-assume-unchanged,于是 Git 只需要做一次更新,这是完全可以接受的了;
    3. 提交+推送。

    另外,根据文档的进一步描述:

    This option can be also used as a coarse file-level mechanism to ignore uncommitted changes in tracked files (akin to what .gitignore does for untracked files).

    这段描述告诉我们两个事实:

    1. 虽然可以用其来达成楼主想要的结果,但这是不讲究的做法(coarse);
    2. 同样的事情更应该用 .gitignore 文件来实现(针对未追踪的文件)。

    随之而来的问题是:为什么我增加了 .gitignore 里的规则却没有效果?

    这是因为我们误解了 .gitignore 文件的用途,该文件只能作用于 Untracked Files,也就是那些从来没有被 Git 记录过的文件(自添加以后,从未 add 及 commit 过的文件)。

    之所以你的规则不生效,是因为那些 .log 文件曾经被 Git 记录过,因此 .gitignore 对它们完全无效。这也正是开头那段简短答案所做的事情:

    1. 从 Git 的数据库中删除对于该文件的追踪;
    2. 把对应的规则写入 .gitignore,让忽略真正生效;
    3. 提交+推送。

    只有这样做,所有的团队成员才会保持一致而不会有后遗症,也只有这样做,其他的团队成员根本不需要做额外的工作来维持对一个文件的改变忽略。

    最后有一点需要注意的,git rm --cached 删除的是追踪状态,而不是物理文件;如果你真的是彻底不想要了,你也可以直接 rm+忽略+提交。

  • 相关阅读:
    洛谷P2569 (BZOJ1855)[SCOI2010]股票交易 【单调队列优化DP】
    洛谷 P2254 [NOI2005]瑰丽华尔兹(单调栈优化DP)
    CF372C Watching Fireworks is Fun(单调队列优化DP)
    2019牛客全国多校第八场A题 All-one Matrices(单调栈)
    HDU3896 Greatest TC(双联通分量+倍增)
    2019牛客多校第7场
    ZOJ 2112 Dynamic Rankings(树状数组+主席树)
    2019 杭电多校第六场 题解
    HDU2242 考研路茫茫——空调教室 (双联通分+树形DP)
    HDU5536 Chip Factory
  • 原文地址:https://www.cnblogs.com/rsapaper/p/10450292.html
Copyright © 2011-2022 走看看