Disable git add . command

2019-01-29 04:53发布

问题:

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

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

回答1:

SVN re-education

I guess it is a bad habit from svn, which has a default to add only tracked files [...]

You must unlearn what you have learned :)

You should run git status often. If files you want to ignore get listed as untracked files, you should then edit your .gitignore file, so that those files actually become ignored. Because git add doesn't affect ignored (and untracked) files, you will then be able to use git add . to stage all files of interest (and only those) in one fell swoop.

How to completely disable git add .

Git itself doesn't allow to do that, but if you really want to completely forbid the use of git add . (and git stage ., an exact equivalent), you can write a small wrapper around git (in your ~/.<shell>rc file) for that:

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;
}


回答2:

The important point about git add . is that it looks at the working tree and adds all those paths to the staged changes if they are either changed or are new and not ignored, it does not stage any 'rm' actions.

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

If that is the case, then

git reset HEAD <file>

will do the job.

Your modifications will be kept and the file will once again show up in the modified, but not yet staged set of git status.

See the git reset man page for details.



回答3:

You should add all files and directories you do not want under version control to the .gitignore file.

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



回答4:

undo git add unstage remove git rm --cached filename.txt git delete from index cancel from commit git reset filename.txt

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

To undo git add . use git reset (no dot).



标签: git git-add