WindowsのGitリポジトリでディレクトリを無視する
質問
Windowsでmsysgitを使用してGitのディレクトリまたはフォルダーを無視するにはどうすればよいですか?
解決
プロジェクトのディレクトリに.gitignore
という名前のファイルを作成します。ファイルにディレクトリ名を入力して、ディレクトリを無視します(スラッシュを追加):
dir_to_ignore/
詳細こちら。
他のヒント
デフォルトでは、ファイル名が.gitignore
.gitignore.txt
を表示します
Gitは<=>
を使用しませんまた、エクスプローラーは名前のないgitignoreタイプのファイルと考えているため、ファイルの名前を<=>に変更することはできません。
非コマンドラインソリューション:
You can rename a file to ".gitignore." and it will create ".gitignore"
ファイルとディレクトリを無視するには、主に2つの方法があるようです:
-
.gitignore
-
.gitignore
ファイルを.git
フォルダー以外のリポジトリのルートに配置します(Windowsでは、真のファイル拡張子を参照してから、.gitignore.
を作成します空のファイル拡張子)) - グローバル構成
~/.gitignore_global
を作成してgit config --global core.excludesfile ~/.gitignore_global
を実行し、これをgit構成に追加します
注:以前に追跡したファイルは、
git rm --cached filename
を実行することで追跡解除できます
-
-
Repo exclude -共有する必要のないローカルファイルの場合、ファイルパターンまたはディレクトリをファイル
.git/info/exclude
に追加するだけです。これらのルールはコミットされないため、他のユーザーには表示されません詳細はこちら
[更新済み] 無視されるファイルのリストで例外を作成するには、この質問をご覧ください。
Windowsエクスプローラーでファイルを作成するときに問題が発生しました。最初に。
回避策は、コマンドシェルに入り、<!> quot; edit <!> quot;
を使用して新しいファイルを作成することでした特定のファイルまたはフォルダーを無視するようにGITに指示するには、.gitignore
ファイルを作成する必要があります。
ただし、Windowsエクスプローラーではファイルの名前を指定する必要があります。拡張子だけでファイルを作成することはできません。空のテキストファイルを作成し、コマンドプロンプトに移動してファイル名を<= >
ren "New Text Document.txt" .gitignore
お気に入りのテキストエディタでファイルを開き、無視するファイル/フォルダ名を追加します。次のようなワイルドカードを使用することもできます*.txt
質問に答えられることを望んでいます
フォルダ内のファイルではなくフォルダを維持する場合は、<!> quot; .gitignore <!> quot; <!> quot; * <!> quot;が付いたフォルダ内のファイルコンテンツとして。このファイルは、リポジトリのすべてのコンテンツを無視します。ただし、.gitignore
はレポジトリに含まれます。
$ git add path/to/folder/.gitignore
空のフォルダーを追加すると、このメッセージが表示されます(.gitignoreは隠しファイルです)
The following paths are ignored by one of your .gitignore files:
path/to/folder/.gitignore
Use -f if you really want to add them.
fatal: no files added
したがって、<!> quot; -f <!> quot;を使用します。追加を強制するには:
$ git add path/to/folder/.gitignore -f
In Windows there's an extra catch with slashes. Excluding a single directory in .gitignore with
dir_to_exclude/
will possibly work, but excluding all directories with
/
causes problems when you have file names with spaces (like my file.txt
) in your directory: Git bash escapes these spaces with a backslash (like my\ file.txt
) and Git for Windows doesn't distinguish between /
and \
.
To exclude all directories better use:
**/
Two consecutive asteriscs signify directory contents.
Also in your \.git\info
projects directory there is an exclude file that is effectively the same thing as .gitignore
(I think). You can add files and directories to ignore in that.
To ignore an entire directory in git, the easiest way is to include a .gitignore file within the target directory which simply contains "*"
An illustrative example,
Example System
/root/
.gitignore
/dirA/
someFile1.txt
someFile2.txt
/dirB/
.gitignore
someFile3.txt
someFile4.txt
Goal
- ignore the contents of /dirB/
Top Level .gitignore (/root/.gitignore)
- This is where your standard gitignore info goes
Ignored Directory .gitignore (/root/dirB.gitignore)
- This file just reads as '*' and the directory is ignored completely, itself and all files!
and it's that simple :)
Just in case you need to exclude sub folders you can use the **
wildcard to exclude any level of sub directory.
**/build/output/Debug/
I've had some problems getting git to pickup the .gitignore file on Windows. The $GIT_DIR/info/exclude file always seems to work though. The downside of this approach, however, is that the files in the $GIT_DIR directory are not included in the check-in, and therefore not shared.
( p.s. $GIT_DIR is usually the hidden folder named .git )
You can create the ".gitignore" file with the contents:
*
!.gitignore
It works for me and simples.
When everything else fails try editing the file
/.git/info/exclude
and adding the directories you want to the end of the file, like this:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
assets/
compiled/
I added the folders "assets" and "compiled" to the list of files and directories to ignore.
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.)
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
On windows and Mac, if you want to ignore a folder named Flower_Data_Folder in the current directory, you can do:
echo Flower_Data_Folder >> .gitignore
If its a file named data.txt
echo data.txt >> .gitignore
if its a path like "Data/passwords.txt"
echo "Data/passwords.txt" >> .gitignore.
I had similar issues, I work on a windows tool chain with a shared repo with linux guys, they happlily create files with the same [except for case] names in a given folder.
The effect is that I can clone the repo and immediatly have dozens of 'modified' files that if I checked in would create havoc.
I have windows set to case sensitive and git to not ignore case but it still fails (in the win32 api calls apparently).
If I gitignore the files then I have to remember to not track the .gitignore file.
But I found a good answer here http://archive.robwilkerson.org/2010/03/02/git-tip-ignore-changes-to-tracked-files/index.html
Chris