zoukankan      html  css  js  c++  java
  • 第一次使用git,撤销git add操作 (undo git add before commit)

    I mistakenly added files using the command

    git add file
    

    I have not yet run git commit.

    Is there a way to undo this or remove these files from the commit?




    You want:

    git rm --cached <added_file_to_undo>
    

    Reasoning:

    Also a newbie I first tried

    git reset .
    

    (to undo my entire initial add) only to get this (not so) helpful message:

    fatal: Failed to resolve 'HEAD' as a valid ref.
    

    turns out that this is because the HEAD ref (branch?) doesn't exist until after the first commit. That is, you'll run into the same newbie problem as me if your workflow, like mine, was something like:

    1. cd to my great new project directory to try out git, the new hotness
    2. git init
    3. git add .
    4. git status

      ... lots of crap scrolls by ...

      => Damn, I didn't want to add all of that.

    5. google "undo git add"

      => find Stackoverflow - yay

    6. git reset .

      => fatal: Failed to resolve 'HEAD' as a valid ref.

      it further turns out that there's a bug logged against the unhelpfulness of this in the mailing list.

      And that the correct solution was right there in the git status output (which, yes, I glossed over as 'crap)

    ...
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    ...
    

    And the solution indeed is to use git rm --cached FILE

    Note the warnings elsewhere here - git rm deletes your local working copy of the file, but not if you use --cached. Here's the result of git help rm`:

    --cached Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left.

    I proceed to use

    git rm --cached .
    

    to remove everything and start again. Didn't work though, because while add . is recursive, turns out rmneeds -r to recurse. sigh.

    git rm -r --cached .
    

    Okay, now I'm back to where I started. Next time I'm going to use -n to do a dry run and see what will be added:

    git add -n .
    

    UPDATE Oh yeah forgot to mention - I zipped up everything to a safe place before trusting git help rmabout the --cached not destroying anything (and what if I misspelled it)


  • 相关阅读:
    MYSQL数据库基础
    MSYQL操数据DML
    MYSQL外键约束
    MYSQL多表查询
    MYSQL结果排序、分页查询、聚合函数
    未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项”的解决方法
    由于扩展配置问题而无法提供您请求的页面。
    JS中的prototype(转载)
    mvc表单如何绑定bool类型的属性或变量
    使用c#正则验证关键字并找出匹配项
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744789.html
Copyright © 2011-2022 走看看