Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable git add . command

Tags:

git

git-add

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 Answers

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;
}
like image 182
jub0bs Avatar answered Jun 24 '26 04:06

jub0bs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!