V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Newyorkcity
V2EX  ›  问与答

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

  •  
  •   Newyorkcity · 2021-05-05 10:54:37 +08:00 · 1589 次点击
    这是一个创建于 1081 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2021-05-06 14:43:07 +08:00
    learningman
        1
    learningman  
       2021-05-05 11:05:13 +08:00 via Android
    git reset HEAD --hard
    JasonLaw
        2
    JasonLaw  
       2021-05-05 11:07:48 +08:00 via iPhone
    你可以先说一下你想实现什么,什么是“ 把一个被 add 的文件给赶出去”?
    JasonLaw
        3
    JasonLaw  
       2021-05-05 11:11:57 +08:00 via iPhone
    learningman
        4
    learningman  
       2021-05-05 11:22:36 +08:00 via Android
    @JasonLaw 我知道啊,我平常都用 mixed,但是楼主这个操作就是要用 hard
    JasonLaw
        5
    JasonLaw  
       2021-05-05 11:24:13 +08:00 via iPhone
    @learningman #4 我的意思是“你应该说明出这个命令存在的危险性”,没有别的意思。
    learningman
        6
    learningman  
       2021-05-05 11:31:49 +08:00 via Android
    @JasonLaw 没危险的,你仍然可以 git reflog 来恢复,git 会保存一切操作
    Newyorkcity
        7
    Newyorkcity  
    OP
       2021-05-05 11:32:19 +08:00
    @JasonLaw 就是我不需要这些文件被 git 管理 至于文件上的改动我希望能保留
    napsterwu
        8
    napsterwu  
       2021-05-05 11:34:48 +08:00 via iPhone
    你的日志里已经贴了,要用 git checkout — log/mirai/normal/normal.log
    JasonLaw
        10
    JasonLaw  
       2021-05-05 11:43:32 +08:00 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
       2021-05-05 11:57:39 +08:00
    @napsterwu 那这样修改的内容不就保存不了了?
    yuancoder
        12
    yuancoder  
       2021-05-05 12:07:05 +08:00
    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  
       2021-05-05 12:14:27 +08:00   ❤️ 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  
       2021-05-05 12:17:12 +08:00
    上面少了一步, 应该是

    touch .gitignore
    nano .gitignore 写入 2.txt
    git add .gitignore
    git commit -m 'update gitignore'
    yuhaoyuhao
        15
    yuhaoyuhao  
       2021-05-05 12:26:07 +08:00
    no changes added to commit (use "git add" and/or "git commit -a")
    msg7086
        16
    msg7086  
       2021-05-06 05:28:22 +08:00
    你先要搞清楚状况。

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

    2. Unstaged changes 的部分是不会进入提交的(除非你提交时手动要求加入 unstaged )。所以这些文件在 Unstaged 里是再正常不过了。
    fyy21
        17
    fyy21  
       2021-05-06 09:56:39 +08:00
    git reset HEAD~
    Delbert
        18
    Delbert  
       2021-05-06 14:43:07 +08:00
    git update-index --assume-unchanged <file>

    这个?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2974 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:10 · PVG 11:10 · LAX 20:10 · JFK 23:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.