Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

git add - Disable git add . command

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...