Ignore all files and files in subdirectories except one file - gitignore

Imagine the following structure:
/a/1.txt
/a/2.txt
/a/.keep
/a/b/1.txt
/a/b/2.txt
/a/b/3.txt
/a/b/.keep
/a/b/c/1.txt
/a/b/c/2.txt
/a/b/c/3.txt
/a/b/c/4.txt
/a/b/c/.keep
/d/test.txt
/d/work.txt
I want to ignore all files in a directory except .keep files to obtain the following results:
/a/.keep
/a/b/.keep
/a/b/c/.keep
/d/test.txt
/d/work.txt
My .gitignore file that doesn't work:
/a/*
!.keep

Unfortunatelly, you cannot reinclude files at directories ignored by previous rules, according to the gitignore Documentation:
It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.
So this
/a/*
!/a/**/.keep
will only reinclude /a/.keep but not the others.
You'll have to exclude each file pattern under /a explictly.
/a/**/*.txt
/a/**/.ignore
/a/**/.alsoignore
UPDATE: Or a better solution is to create the following .gitgnore at your /a subdirectory:
*.*
!.keep
(the only drawback is that this solution will also keep files with no extension)

In your case, you should use:
/a/*
!**/.keep
From the gitignore documentation:
A leading "**" followed by a slash means match in all directories. For
example, "**/foo" matches file or directory "foo" anywhere, the same
as pattern "foo". "**/foo/bar" matches file or directory "bar"
anywhere that is directly under directory "foo".

Related

.gitignore all except one specific file (via a full/fuller file path)

The Goal:
How to use .gitignore to exclude all folders & files except PowerShell $Profile?
The answer should help expand the current exception file list to more files in other subfolders. If possible, reduce wildcards to minimum, and use as specific as possible (full/fuller file path). Why? For instance, Book1.xlsx may exist in multiple subfolders but I want to be able to choose only the specific desired subfolders.
Thanks in advance!
Current status:
On Windows 10 (not Linux Distros):
git init initiated on top level directory C:\. [Please don't suggest to start from other subfolders. Just remain with C:\, as I will include more files to the exception list]
C:\.gitignore containing the below:
# Ignore All
/*
# Exception List [eg. PowerShell's $Profile (please use FULL/FULLER FILE PATH, if possible)]
!.gitignore
!Microsoft.PowerShell_profile.ps1
!Users/John/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1
!Users\John\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
!C:/Users/John/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1
!C:\Users\John\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
With the above codes, git status successfully returned only .gitignore as 1 untracked file. Microsoft.PowerShell_profile.ps1 remained missing from untrack listing.
I've tried alternative ways (wildcard, partial subfolder name with pattern, etc) but all failed to return the fuller file path PowerShell $Profile.

Cygwin or Gnuwin - Find .txt files named* copy & paste to specific directory

Okay so I want to know how I would go about doing this, using grep to locate .txt files named "cocacola1", "cocacola2", "cocacola3" & then copying them to another directory. So searching for files named "cocacola" &/even if it contains other characters within the file name to then copy them to another directory/location.
You can just use unix find. Assuming the files you're searching for are in 'source' and you want to copy to 'destination':
find source -name '*cocacola*' -exec cp {} destination \;
I put the wildcard '*' before and after cocacola since you said other characters might exist in the file name.

How to copy a file from source to a destination and recreate the folder structure

I have some files which needs to be copied through deployment process often to a destination. This is my folder folder structure:
SOURCE:
c:\
folder1
sub1
subsub1
file1
file2
I need something where i can tell my "script" something like this
mycopy c:\folder1\sub1\subsub1\file1 h:\
That means that i dont want to
check if folder structure exist
provide on bot sides the complete structure for each file on destination side
I want to
provide the full path and filename on source side
create folder structure if not exist
overwrite file if exist
How can i achieve this?
See the robocopy help page on MS Technet
https://technet.microsoft.com/en-us/library/cc733145%28v=ws.11%29.aspx
In your case you would do something like
robocopy c:\folder1\sub1\subsub1 h:\ file1 <options ...>
Notice that the first two parameters are the source and dest paths only, with no filename at the end
Read the linked help page and test on your desktop ...
Ok it works like this:
You can use Robocopy for this task. Thanks you Rick716 for this direction. I am not marking it as answer cause it is only a direction not the solution.
Lets Assume that we have the following source folder structure:
N:\source\a1\b1\c1\d1\e1\f1
and we want to recreate the hole structure under n:\source within h:\destination. Then we have to use Robocopy in this way:
robocopy N:\source h:\destination /e
The option /e will create the folder structure even create empty folders. Additionally you can append the files which should be copied. For example
file.txt
*.jpg
*.bkp
etc. But these files will be even copied when they exist somewhere within the folder strcuture! For example you have the file n:\source\a1\file.txt and the file n:\source\a1\b1\c1\file.txt then both will be copied by using
robocopy N:\source h:\destination file.txt /e

How to use xcopy to select specific folders(Not Files) within another folder

I am trying to xcopy specific folders from some directory to another folder, problem is i don't want to copy all the folders but i want specific folders. E.g. DirectorySource has folders (folderA,folderB,folderC) but i want to copy folderA and folderB only to DirectoryDestination.
Create a text file, say C:\excludes.txt with the following contents:
DirectorySource\folderC\
then you can copy:
xcopy DirectorySource DirectoryDestination /s /i /exclude:C:\excludes.txt
The file excludes.txt contains a list of strings, one per line. If a file or directory matches that string, then it will not be copied. If you want to exclude folder, it is safer to use the above string instead of simply folderC, which might skip a file called folderC_listing.txt

How can I ignore everything under a folder in Mercurial

I am looking for an expression for the .hgignore file, to ignore all files beneath a specified folder.
eg: I would like to ignore all files and folders beneath bin
Actually any advice on how the expressions are formed would be great
Alternately:
syntax: glob
bin/**
I did some experiments and I found that the regex syntax on Windows applies to the path starting with the current repository, with backslashes transformed to slashes.
So if your repository is in E:\Dev for example, hg status will apply the patterns against foo/bar/file1.c and such. Anchors apply to this path.
So:
Glob applies to path elements and is rooted to element parts
foo matches any folder (or file) named foo (not to "foobar" nor "barfoo")
*foo* matches any folder or file with "foo" in the name
foo/bar* matches all files in "foo" folder starting with "bar"
Regex is case sensitive, not anchored
Of course, backslash regex special characters like . (dot)
/ matches \ path separator on Windows. \ doesn't match this separator...
foo matches all files and folders with "foo" inside
foo/ matches only folders ending with "foo"
/foo/ matches the folder "foo" somewhere in the path
/foo/bar/ matches the folder "bar" in the folder "foo" somewhere in the path
^foo matches file or folder starting by foo at the root of the repository
foo$ matches file ending with foo
I hope this will help, I found the HGIGNORE(5) page a bit succinct.
Both of those will also filter out a directory called cabin, which might not be what you want. If you're filtering top-level, you can use:
^/bin/
For bin directories below your root, you can omit the ^. There is no need to specify syntax, regexp is the default.
syntax: glob
bin/**
This answer is shown above, however I'd also like to add that * and ** are handled differently. ** is recursive, * is not.
See Hg Patterns
Nevermind, I got it
syntax: regexp
bin\\*
expressions follow standard perl regular expression syntax.
to ignore .class files
syntax: regexp
?\.class