开发者

Is there a way to add a directory to Git but exclude extensions in .gitgnore?

Is there a way to add a directory to Git, but exclude extensions in .gitgnore?

e.g when I execute a command to add a directory such as

git add console/*

git responds with response

$ git add console/*
The following paths are ignored by one of your .gitignore files:
console/networkpr开发者_JS百科obing.ppu
console/opsdatamodule.lrs
console/opsdatamodule.o
console/opsdatamodule.ppu
console/rcacsSystemDM.compiled
console/rcacsSystemDM.o
console/rcacsSystemTestProcs.o
console/rcacsSystemTestProcs.ppu
Use -f if you really want to add them.
fatal: no files added

How can the command be made to automatically exclude the .gitignore extensions? Are there some additional options to be passed?


git add console/*

The problem is that the wildcard is expanded by shell globing and not passed directly to git, so git-add is really being passed every file in the console directory[1]. Because git-add is receiving each file in the directory as arguments, it thinks you might really be trying to add an ignored file which is why there is a warning and it hints to use -f if that is what you are really trying to do.

The solution is to not use shell globing but instead just pass the directory like so.

git add console

Similarly the proper way to add everything to a repo (including files starting with a '.') except ignored files[2] to a repo is to use git add . and not git add *.

[1] Except ones starting with a '.' assuming you are on a sh like shell and not attempting this from the Windows cmd.exe command prompt. If you are using the Windows command prompt you should switch to using either the Git-Bash shell from msysgit or a shell from cygwin depending on how you installed git.
[2] .gitignore is only used to prevent untracked files from becoming tracked, if the file is already being tracked by git then it will continue to be treated normally until it is removed from the repo. This can be done without effecting the actual file with git rm --cached filename.


.gitignore can take shell-style glob patterns. So, for example, if you want to ignore all .jpg files you can add

# Ignore all JPEGs
*.jpg

to .gitignore. If you only want to ignore JPEGs in your new directory, you can do

new_directory/*.jpg

Then just add the directory as normal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜