How to undo ‘git add’ before commit ?

How to undo ‘git add’ before commit  ?

Asked on December 14, 2018 in Git.
Add Comment


  • 3 Answer(s)

    Try this  solution:

    You can undo git add before commit with

    git reset <file>
    

    Whichever will remove it from the current index the “about to be committed” list without changing anything else.

    We can use

    git reset
    

    After any file name to unstage all due changes. This can come in handy when there are too many files to be listed one by one in a reasonable amount of time.

    In early versions of Git, the above commands are equivalent to git reset HEAD <file>and git reset HEAD respectively, and will fail if HEAD is undefined incase  you haven’t yet made any commits in  repo or ambiguous to create a branch called  HEAD. This was changed in Git 1.8.2,now latest  versions of Git  can use the commands above even prior to making your first commit:

    “git reset” without options or parameters used to error out when you do not have any commits in your history.

     

     

    Answered on December 14, 2018.
    Add Comment

    Try this you want:

    git rm --cached <added_file_to_undo>
    

    Reasoning:

    That was my first try 

    git reset .
    

     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 doesn’t exist until after the first commit. That is, you will run into the same beginner’s problem as me if your workflow, like mine, was something like:

    • cd to my great new project directory to try out Git, the new hotness
    • git init
    • git add .
    • git status
    • google “undo git add”
    •   git reset .

          Alone with 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)
    ...
    

    this solution indeed is to use git rm –cached FILE.

    Note the warnings 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:

     I advance to use

    git rm --cached .
    

    To remove everything and start again  while add . is recursive, turns out rm needs -r to recurse. Sigh.

    git rm -r --cached .
    

     To do a dry run and see what will be added:

    git add -n .
    

    I zipped up everything to a safe place before trusting git help rm about the –cached not destroying anything and what if I misspelled it.

    Answered on December 14, 2018.
    Add Comment

    Try this solution:

    To resolve: git add moves changes from the current working directory to the staging area

    This process is called staging. So the most natural command to stage the changes (changed files) is the obvious one:

    git stage
    git add is just an easier to type alias for git stage
    

    Pity there is no git unstage nor git unadd commands. The relevant one is harder to guess or remember, but is pretty obvious:

    git reset HEAD --
    

    We can easily create an alias for this:

    git config --global alias.unadd 'reset HEAD --'
    git config --global alias.unstage 'reset HEAD --'
    

    And final, we have new commands:

    git add file1
    git stage file2
    git unadd file2
    git unstage file1
    

    specially I use even shorter aliases:

    git a #for staging
    git u #for unstaging
    
    Answered on December 14, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.