Disable git add. command

Many times I mistakenly add unwanted files to the staging area using the command git add .

.

I wonder if there is a way to disable this command completely, so I only use git add file

?

+1


source to share


4 answers


Re-educating SVN

I think this is a bad habit from svn, which by default only adds tracked files [...]

You have to wean what you've learned :)

You must run frequently git status

. If the files you want to ignore are listed as untracked files, you must then edit the file .gitignore

so that these files are effectively ignored. Since it git add

does not affect ignored (and unchecked) files, you can use it git add .

to create all files of interest (and only those) in one fell swoop.



How to completely disable git add .

Git itself doesn't allow you to do this, but if you really want to completely disallow usage git add .

(and git stage .

, the exact equivalent), you can write a little wrapper around git

(in your file ~/.<shell>rc

):

git() {
    if [ "$1" = "add" -o "$1" = "stage" ]; then
        if [ "$2" = "." ]; then
            printf "'git %s .' is currently disabled by your Git wrapper.\n" "$1";
        else
            command git "$@";
        fi
    else
        command git "$@";
    fi;
}

      

+1


source


The important point is that it looks at the working tree and adds all these paths to the staged changes, if they are either changed or new, and are not ignored, but not put any "rm" actions. git add .

If I understand the question correctly, you just want to "undo" the git add that was done for this file.

If so, then

git reset HEAD <file>

      



will do the job.

Your changes will be saved and the file will reappear in the modified but not yet delivered set git status

.

See the man page for details git reset

.

0


source


You must add all files and directories that you do not want to be version controlled to the .gitignore file.

If you just want to not add everything, well then don't git add.

0


source


undo git add inactive delete git rm --cached filename.txt git delete from index undo commit git reset filename.txt

Will remove the file named filename.txt from the current index, "about to be commit" area, without changing anything.

Cancel git add. use git reset (no dot).

0


source







All Articles