Ignoring directories in Git repos on Windows

2019-01-01 14:19发布

How can I ignore directories or folders in Git using msysgit on Windows?

17条回答
弹指情弦暗扣
2楼-- · 2019-01-01 14:40

to instruct GIT to ignore certain files or folders, you have to create .gitignore file.

but in windows explorer you have to provide a name for the file, you just cannot create file with just extension, the trick is that create a empty text file and go to command prompt and change the name of the file to .gitignore

ren "New Text Document.txt" .gitignore

now open the file with your favorite text editor and add the file/folder names you wish you ignore. you can also use wildcards like this *.txt

hope it answers you question

查看更多
只靠听说
3楼-- · 2019-01-01 14:40

On Unix:

touch .gitignore

On Windows:

echo > .gitignore

These commands executed in a terminal will create a .gitignore file in the current location.

Then just add info to this .gitignore file (using Notepad++ for example) which files or folders should be ignored. Save your changes. That's it :)

More information: https://www.atlassian.com/git/tutorials/saving-changes/gitignore

查看更多
无与为乐者.
4楼-- · 2019-01-01 14:41

By default windows explorer will display .gitignore when in-fact the file name is .gitignore.txt

Git will not use .gitignore.txt

And you can't rename the file to .gitignore because explorer thinks its a file of type gitignore with no name.

Non command line solution:

You can rename a file to ".gitignore." and it will create ".gitignore"
查看更多
荒废的爱情
5楼-- · 2019-01-01 14:42

I assume the problem is that your working tree is like:

a-cache/foo
a-cache/index.html
b-cache/bar
b-cache/foo
b-cache/index.html
.gitignore

... with the .gitignore you describe. This will give you git status output like:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
#   a-cache/
#   b-cache/

... if the index.html files have not yet been added to the repository. (git sees that there are unignored files in the cache directories, but only reports the directories.) To fix this, make sure that you have added and committed the index.html files:

git add *cache/index.html
git commit -m "Adding index.html files to the cache directories"

... and your git status will then look like:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
nothing added to commit but untracked files present (use "git add" to track)

(Obviously you do want to commit .gitignore as well, I was just being lazy with this test case.)

查看更多
荒废的爱情
6楼-- · 2019-01-01 14:43

Create a file named .gitignore in your project's directory. Ignore directories by entering the directory name into the file (with a slash appended):

dir_to_ignore/

More info here.

查看更多
只若初见
7楼-- · 2019-01-01 14:44

It seems that for ignoring files and directories there are two main ways:

  1. .gitignore

    • Placing .gitignore file into the root of your repo besides .git folder (in Windows make sure you see the true file extension and then make .gitignore. (with the point at the end to make empty file extension) )
    • Making global configuration ~/.gitignore_global and running git config --global core.excludesfile ~/.gitignore_global to add this to your git config

    note: files tracked before can be untracked by running git rm --cached filename

  2. Repo exclude - For local files that do not need to be shared, you just add the file pattern or directory to the file .git/info/exclude. Theses rules are not commited, so are not seen by other users more info here

[updated] To make exceptions in list of ignored files, see this question.

查看更多
登录 后发表回答