How to remove files from git staging area?

后端 未结 14 1128
失恋的感觉
失恋的感觉 2020-11-28 16:53

I made changes to some of my files in my local repo, and then I did git add -A which I think added too many files to the staging area. How can I delete all the

相关标签:
14条回答
  • 2020-11-28 17:42

    If you've already committed a bunch of unwanted files, you can unstage them and tell git to mark them as deleted (without actually deleting them) with

    git rm --cached -r .
    

    --cached tells it to remove the paths from staging and the index without removing the files themselves and -r operates on directories recursively. You can then git add any files that you want to keep tracking.

    0 讨论(0)
  • 2020-11-28 17:45

    Use "git reset HEAD <file>..." to unstage fils

    ex : to unstage all files

    git reset HEAD .
    

    to unstage one file

    git reset HEAD nameFile.txt
    
    0 讨论(0)
  • 2020-11-28 17:46

    You can unstage files from the index using

    git reset HEAD -- path/to/file
    

    Just like git add, you can unstage files recursively by directory and so forth, so to unstage everything at once, run this from the root directory of your repository:

    git reset HEAD -- .
    

    Also, for future reference, the output of git status will tell you the commands you need to run to move files from one state to another.

    0 讨论(0)
  • 2020-11-28 17:48

    As noted in other answers, you should use git reset. This will undo the action of the git add -A.

    Note: git reset is equivalent to git reset --mixed which does this

    Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action. [ git reset ]

    0 讨论(0)
  • 2020-11-28 17:49

    Now at v2.24.0 suggests

    git restore --staged .
    

    to unstage files.

    0 讨论(0)
  • 2020-11-28 17:49

    If unwanted files were added to the staging area but not yet committed, then a simple reset will do the job:

    $ git reset HEAD file
    # Or everything
    $ git reset HEAD .
    

    To only remove unstaged changes in the current working directory, use:

    git checkout -- .
    
    0 讨论(0)
提交回复
热议问题