Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
978 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.
" "$1";
        else
            command git "$@";
        fi
    else
        command git "$@";
    fi;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...