How to setup .gitignore for Windows? - windows

I am setting up my first project in Git. How do I setup git-ignore file in Windows?
I am creating my first Rails project using Vagrant and trying to configure .gitignore in Windows

Easy.
Make a file .gitignore using your text editor
In there, write the file name you'd want to ignore
You can use wildcard like: *.pyc ignoring any file with extension .pyc
If you use TortoiseGit or other Git software, it will be easier. There will be add to ignore list menu when you right click to a file.

It seems that for ignoring files and directories there are two main ways:
.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.
Making global configuration %HOMEPATH%\.gitignore_global and running
git config --global core.excludesfile %HOMEPATH%\.gitignore_global
to add this to your git config.
Note: files tracked before can be untracked by running git rm --cached filename. This absolutely critical for repos that existed BEFORE you created the .gitignore
Repo exclude
For local files that doesn't need to be shared, you just add file pattern or directory to file .git\info\exclude. These rules are not committed, so are not seen by other collaborators in your project. These are machine specific configs.

Windows does not allow you to create a "dotfile" (i.e., a file whose name begins with a dot). There are three simple ways to create a .gitignore in Windows, though. In each case, you want the .gitignore in your project's root directory.
In your text editor, create a file and "Save" or "Save As". Name the file ".gitignore" and the text editor works around the OS's limitation. I use Sublime Text 3 and Vim. This method works for both (Vim would use the command :w, though).
A fallback would be to create an empty (for now) text file and save it as .gitignore.txt then go into the command shell and rename the file to .gitignore (using the command line). That will do it.
Git Bash (available in the git installer for Windows) has the "touch" command and it will create dotfiles. At the Git Bash prompt enter the command "touch .gitignore" and an empty file is created if no file existed with that name.
Add your exclusion rules inside the .gitignore file:
Starting point. This repo will give you sample exclusion patterns for (i) Windows, (ii) Rails and (iii) Vagrant. You could add those to a global gitignore file.
GitIgnore / Patterns. This section of the Git Manual explains the patterns to use in your new gitignore file. Basically, you exclude directories and files that don't need version control.

To add to previous answers that you should use a text editor to create the .gitignore file, I usually first run dir > .gitignore from a Windows command prompt or Powershell window.
This outputs the entire directory listing to a file named .gitignore .
Then it's very easy to use a text editor (e.g., Notepad ++ or Atom) to modify the file from the directory listing and not miss a file or mistype a file name.

For Windows 10 I used:
ls > .gitignore
to create a clean .gitignore file ready to just delete lines where using 'dir' I would have had to also edit out all the explanatory directory text. I needed this when vscode told me I had 5K changes after I added a virtual environment to an existing project.

Related

How do I exclude a single file from svn:ignore?

I've got a directory that needs to be ignored mostly. One of its files is special and I want to include it in the versioning system. The name of the folder to be ignored is hw-description, and the name of the file that I want to preserve is system.hdf.
I can add the hw-description to the svn:ignore list using TortoiseSVN -> Properties -> svn:ignore, and it works properly. Even though, I append !system.hdf to the next line, the file is still ignored.
What is the way of doing that?
Solution using the svn command-line tool
As Robert stated below, I should have not ignored the containing folder. Only the contents should have been ignored via setting the svn:ignore property of the folder as *. Therefore, I added only the system.hdf file to version control and ignored the rest of the folder. Here are the related commands for it:
# Add the folder to version control excluding the content
svn add hw-description/ --depth empty
# Add the specific file to version control
svn add hw-description/system.hdf
# Ignore the rest of the files via svn:ignore property
svn propset svn:ignore "*" hw-description/
Be aware that the parent folder must have been added to SVN before running the commands above.
You should not ignore the whole folder, I would manually add the one file to SVN and commit it and then in that folder add an ignore entry for *.
As ignore entries do not affect already added files the one file will remain as it is and other files are ignored.

Where should I put .gitattributes file so that PyCharm reads it (Windows 10)?

I have a simple .gitattributes file with (only) the following in it:
* text eol=lf
in my user home directory on Windows 10. It seems that PyCharm ignores it when I pull origin/master git repository, namely the newly checked out files always have CRLF endings.
Where should I put the file in order for it to be read by PyCharm? Alternatively, is there any other way to make PyCharm always check out files with LF line endings?
The .gitattributes file is parsed by git not PyCharm.
Where should I put the file ... ?
From the git docs [emphasis mine]:
When deciding what attributes are assigned to a path, Git consults $GIT_DIR/info/attributes file (which has the highest precedence), .gitattributes file in the same directory as the path in question, and its parent directories up to the toplevel of the work tree (the further the directory that contains .gitattributes is from the path in question, the lower its precedence). Finally global and system-wide files are considered (they have the lowest precedence).
Attributes which should be version-controlled and distributed to other repositories (i.e., attributes of interest to all users) should go into .gitattributes files.
If you think your attributes are ignored, you can double check your current configuration with git-check-attr, for example:
git check-attr eol *
git check-attr --all <some_path>
The .gitattributes file should be stored at the root of your git repository, close to the .gitignore or the .gitmodules files (if they exists), or at least close to the .git folder.

.gitignore is not working on Windows

I used to develop on linux environment, but now I need to write a git project on "windows" os. I need to ignore a directory from where the git is initiated.
like folder/.git/, now I need to ignore /folder/project/bin/*. So I have added .gitignore file on folder/.gitignore using both text pad and Visual studio.
.gitignore file contents are
/project/bin/*
Now, I expect git status should not show /project/bin folder.
Note, I have tried other ways too, but it seems that git is considering the .gitignore file as a general text file or so.
Just a guess, but I suspect that the leading forward-slash is throwing something off. I'm not sure about the trailing star either (as I reference my own .gitignore files).
Your .gitignore should be this:
project/bin/
I frustratingly found the same issue. I couldn't seem to ignore folders, but files were OK. My issue started with folders that contain spaces. All the combinations I could find after a searching (full directory path, trying to ignore case, escaping the spaces, etc) didn't resolve it.
I had to remove spaces in directory names, then I was able to ignore files.
I tried it on WSL (Windows sub-system for Linux - git was already installed), no problems.
Then reading more, I realised that if files are previously tracked, then the .gitignore won't work. I tested it by deleting all the files in the directory, then adding the directory to .gitignore path/to/my_files/*.csv (in my case I'm ignoring data in .csv files) and doing a commit then push (good job this is just dev).
After that worked, I was able to use git rm -r --cached ./path/to/my_files/ to un track all the files in another directory rather than cutting and pasting them back (Thanks to this answer)
So:
Un-track all the files
Add the directory in .gitignore (In windows it wasn't case sensitive)
add/commit/push as normal.

Mercurial in Windows doesn't see .hgignore - why?

Windows fails to pick up my .hgignore file. I'm running Mercurial from the command line, and "hg status" shows lots of files in the ignored directories.
The .hgignore file looks like this (there's no whitespace at the start of the file, or at the start of each line). I've put it in the root directory of the repository.
\.pyc$
\.pyo$
\.DS_Store
\.Python
\.installed.cfg
^bin$
^build$
^develop-eggs$
^eggs$
^include$
^lib$
^parts$
^pip-log.txt$
^web/localsettings.py$
I've tried saving the file in ANSI and UTF-8, and it doesn't seem to make a difference.
I know the file is working OK on Linux, is there anything different about the paths in Windows?
If the .hgignore file is in your user profile directory (%userprofile%/.hgignore), then edit your mercurial.ini (which should also be in your user profile folder) to have this:
[ui]
ignore = %USERPROFILE%/.hgignore
That will cause hg to recognize and use your .hgignore file. The .hgignore file in your user profile directory will affect hg's behavior for all repositories. If you want to have specific per-repository settings, you should be able to put .hgignore files in the root of each repository.
Also, be aware of this: Even if Mercurial is picking up your new .hginore file, it won't necessarily ignore all the specified files! Any file that had previously been added to the repository will NOT be ignored until you explicitly remove it from the repository.
First, the .hgignore file must be in the root of your repository
Then, you must specify the used syntax at the first line of your .hgignore file (in your case, regexp):
syntax: regexp
"hg status" shows lots of files in the ignored directories
Are you referring to .DS_Store and .Python exclusively? The rule for ^bin$ will only ignore entries with that exact string. It will not, for example, ignore "bin/a.out".
The only other thing I could think of is if there is some global setting that sets the syntax to glob (as opposed to the default of regex) hiding somewhere. I'm not aware of any such thing, and it certainly isn't on by default if installing an official binary version of Mercurial.

Ignoring directories in Git repositories on Windows

How can I ignore directories or folders in Git using msysgit on Windows?
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 information is here.
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 Windows Explorer thinks it's a file of type gitignore without a name.
Non command line solution:
You can rename a file to ".gitignore.", and it will create ".gitignore"
It seems that for ignoring files and directories there are two main ways:
.gitignore
Placing .gitignore file into the root of your repository besides the .git folder (in Windows, make sure you see the true file extension and then make .gitignore. (with the point at the end to make an empty file extension))
Making the global configuration ~/.gitignore_global and running git config --global core.excludesfile ~/.gitignore_global to add this to your Git configuration
Note: files tracked before can be untracked by running git rm --cached filename
Repository 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 committed, so they are not seen by other users. More information is here.
To make exceptions in the list of ignored files, see this question.
To ignore an entire directory place a .gitignore of “*” there.
For example,
Example System
/root/
.gitignore
/dirA/
someFile1.txt
someFile2.txt
/dirB/
.gitignore
someFile3.txt
someFile4.txt
Goal
ignore the contents of dirB/
Top Level (/root/.gitignore)
You could just “dirB/“ here
Ignored Directory (/root/dirB/.gitignore)
Or you could “*” here
Git watches for gitignore at every step of the file system. So here I choose dirB/.gitignore as “*” to ignore dirB/, including all files and subdirs within.
Done ☺️
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 an 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.
I had some issues creating a file in Windows Explorer with a . at the beginning.
A workaround was to go into the commandshell and create a new file using "edit".
If you want to maintain a folder and not the files inside it, just put a ".gitignore" file in the folder with "*" as the content. This file will make Git ignore all content from the repository. But .gitignore will be included in your repository.
$ git add path/to/folder/.gitignore
If you add an empty folder, you receive this message (.gitignore is a hidden file)
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
So, use "-f" to force add:
$ git add path/to/folder/.gitignore -f
You can create the ".gitignore" file with the contents:
*
!.gitignore
It works for me.
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 asterisks signify directory contents.
Just in case you need to exclude sub folders you can use the ** wildcard to exclude any level of sub directory.
**/build/output/Debug/
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.
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've had some problems getting Git to pick up 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
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 information to this .gitignore file (using Notepad++ for example) which files or folders should be ignored. Save your changes. That's it :)
More information: .gitignore
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 it 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 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 it's a file named data.txt:
echo data.txt >> .gitignore
If it's 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 repository with Linux guys, and they happily create files with the same (except for case) names in a given folder.
The effect is that I can clone the repository and immediately 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
Just create .gitignore file in your project folder Then add the name of the folder in it for ex:
frontend/node_modules
This might be extremely obvious for some, but I did understand this from the other answers.
Making a .gitignore file in a directory does nothing by itself. You have to open the .gitignore as a text file and write the files/directories you want it to ignore, each on its own line.
so cd to the Git repository directory
touch .gitignore
nano .gitignore
and then write the names of the files and or directories that you want to be ignored and their extensions if relevant.
Also, .gitignore is a hidden file on some OS (Macs for example) so you need ls -a to see it, not just ls.
Temporarily ignore a directory/file that was already in git:
I have a lot of projects in a multi-project gradle project and they can take a long time to delete them, and they're all pretty much the same but different. From time to time I want to remove those from the gradle build by deleting them altogether. git can get them back after all. However I don't want them showing up in git status either. So I use the following simple procedure;
delete files and folders I don't want.
verify build still works
tell git to ignore the deleted files for a bit (we can get them back)
git ls-files --deleted -z | git update-index --assume-unchanged -z
--stdin
go about life without the dirs until you want them back. Then run the same command as before but switch out assume-unchanged for no-assume-unchanged
git ls-files --deleted -z | git update-index --no-assume-unchanged -z
--stdin

Resources