为什么我 git reset HEAD 就是无法把一个被 add 的文件给赶出去? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Newyorkcity

为什么我 git reset HEAD 就是无法把一个被 add 的文件给赶出去?

  •  
  •   Newyorkcity May 5, 2021 2551 views
    This topic created in 1821 days ago, the information mentioned may be changed or developed.
    D:\JAVA\projects\syxwall-parent>git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: .gitignore modified: log/backend/normal/normal.log modified: log/backend/sql/sql.log modified: log/mirai/normal/normal.log deleted: syxwall-backend/syxwall-backend-web/src/main/java/com/syxgo/syxwall/backend/web/controller/FallbackController.java modified: syxwall-mirai/syxwall-mirai-api/src/main/java/com/syxgo/syxwall/mirai/api/MiraiRobotApis.java no changes added to commit (use "git add" and/or "git commit -a") 
    D:\JAVA\projects\syxwall-parent>git reset HEAD log/backend/normal/normal.log Unstaged changes after reset: M .gitignore M log/backend/normal/normal.log M log/backend/sql/sql.log M log/mirai/normal/normal.log D syxwall-backend/syxwall-backend-web/src/main/java/com/syxgo/syxwall/backend/web/controller/FallbackController.java M syxwall-mirai/syxwall-mirai-api/src/main/java/com/syxgo/syxwall/mirai/api/MiraiRobotApis.java 

    normal.log 出现在 Unstaged changes after reset: 就 TM 离谱

    为什么啊?

    18 replies    2021-05-06 14:43:07 +08:00
    learningman
        1
    learningman  
       May 5, 2021 via Android
    git reset HEAD --hard
    JasonLaw
        2
    JasonLaw  
       May 5, 2021 via iPhone
    你可以先说一下你想实现什么,什么是“ 把一个被 add 的文件给赶出去”?
    JasonLaw
        3
    JasonLaw  
       May 5, 2021 via iPhone
    learningman
        4
    learningman  
       May 5, 2021 via Android
    @JasonLaw 我知道啊,我平常都用 mixed,但是楼主这个操作就是要用 hard
    JasonLaw
        5
    JasonLaw  
       May 5, 2021 via iPhone
    @learningman #4 我的意思是“你应该说明出这个命令存在的危险性”,没有别的意思。
    learningman
        6
    learningman  
       May 5, 2021 via Android
    @JasonLaw 没危险的,你仍然可以 git reflog 来恢复,git 会保存一切操作
    Newyorkcity
        7
    Newyorkcity  
    OP
       May 5, 2021
    @JasonLaw 就是我不需要这些文件被 git 管理 至于文件上的改动我希望能保留
    orcusfox
        8
    orcusfox  
       May 5, 2021 via iPhone
    你的日志里已经贴了,要用 git checkout log/mirai/normal/normal.log
    JasonLaw
        10
    JasonLaw  
       May 5, 2021 via iPhone
    @learningman #6 "If you accidentally remove uncommited changes which were never tracked by git (speak: committed or at least added to the index), you have no way of getting them back using git.",那怎么解释这段话呢?
    Newyorkcity
        11
    Newyorkcity  
    OP
       May 5, 2021
    @napsterwu 那这样修改的内容不就保存不了了?
    yuancoder
        12
    yuancoder  
       May 5, 2021
    Changes not staged for commit:
    (use "git add/rm <file>..." to update what will be committed)
    (use "git checkout -- <file>..." to discard changes in working directory)


    已经提示的很明白了
    jokerai
        13
    jokerai  
       May 5, 2021   2
    “不需要 git 管理 / 把文件踢出 git 管辖区 / 让 git 无视这个文件的存在吧” 的设置是应该放在 .gitignore 里的
    如果误操作了,则先更新 .gitignore 文件并 commit,再对此文件把它移出去、commit 一次、再把它移回来就 OK
    参考
    https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore/1274081#1274081

    例子
    cd testDir1
    git init
    touch 1.txt 2.txt 3.txt 4.txt 5.txt
    git add 1.txt 2.txt
    git commit -m 'update'

    touch .gitignore
    nano .gitignore 写入 2.txt
    git commit -m 'update gitignore'

    把 2.txt 移到外面目录( git 管辖区文件夹的外面)
    git add 2.txt
    git commit -m 'update'

    把 2.txt 移到目录( git 管辖区文件夹)
    git status

    此后 2.txt 不会再被 git 管辖
    jokerai
        14
    jokerai  
       May 5, 2021
    上面少了一步, 应该是

    touch .gitignore
    nano .gitignore 写入 2.txt
    git add .gitignore
    git commit -m 'update gitignore'
    yuhaoyuhao
        15
    yuhaoyuhao  
       May 5, 2021
    no changes added to commit (use "git add" and/or "git commit -a")
    msg7086
        16
    msg7086  
       May 6, 2021
    你先要搞清楚状况。

    1. log 文件已经在仓库里了,不管你做什么操作,他们都在仓库里。之前已经有人把这几个文件提交进去了,换句话说这些文件已经成了历史书的一部分,你只有改变历史才能「踢出」这个文件。

    2. Unstaged changes 的部分是不会进入提交的(除非你提交时手动要求加入 unstaged )。所以这些文件在 Unstaged 里是再正常不过了。
    fyy21
        17
    fyy21  
       May 6, 2021
    git reset HEAD~
    Delbert
        18
    Delbert  
       May 6, 2021
    git update-index --assume-unchanged <file>

    这个?
    About     Help     Advertise     Blog     API     FAQ     Solana     5429 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 55ms UTC 03:19 PVG 11:19 LAX 20:19 JFK 23:19
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86