Disable git add . command

我只是一个虾纸丫 提交于 2019-11-28 09:27:37

问题


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).



来源:https://stackoverflow.com/questions/25884007/disable-git-add-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!