2

Let's say that I have a directory called REPO with an initiated git repo in it, in that directory, there's 2 subdirectories called sub1, sub2 and a "1.cpp" file.
sub1 has "2.cpp" file and other files.
sub2 has "3.cpp" file and other files, so It'll be exactly like this diagram

enter image description here

Now I want only these files in my repository without any directories

1.cpp
2.cpp
3.cpp

I know how to ignore all files except .cpp files using .gitignore but I don't want sub1 and sub2 to be included in my repo, I only want the cpp files in them, I have searched a lot but couldn't find an answer, I hope I could explain it properly.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • So you want to keep the directory structure in file system while ignore directory structure in repo? – llllllllll Jan 20 '18 at 08:18
  • Yeah that's it exactly – Mohamed Magid Jan 20 '18 at 08:22
  • I don't know how it is possible. But like in Xcode, you can create some fake directory in IDE, so when you are working it seems those files are in different directories. But in this way both the filesystem and git don't know these directories. – llllllllll Jan 20 '18 at 08:27

2 Answers2

2

You can move them:

cd /path/to/REPO
git mv sub1/2.cpp .
git mv sub2/3.cpp .

And then remove the subfolders if you don't need them.

But actually, the OP meant:

I simply want Git to include all my .cpp files from all subdirectories and add them to the repository.

know how to ignore all files except .cpp files using .gitignore but I don't want sub1 and sub2 to be included in my repo, I only want the cpp files in them

If you haven't added any file yet, your .gitignore would be

**
!**/
!*.cpp

Then a git add . would include only cpp files.
You ignore all files (**), then exclude folders (!**/): that allows you to then exclude .cpp files (!*.cpp) from the gitignore rule.
That is because it is not possible to re-include a file if a parent directory of that file is excluded.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It's a Visual Studio C++ project so I only want to include .cpp files because nothing else matters for me, so unfortunately I can't move files to repo directory. – Mohamed Magid Jan 20 '18 at 08:10
  • @MohamedMagid Sure, you can work with Git from the command-line with Visual Studio: https://learn.microsoft.com/en-us/vsts/git/command-prompt – VonC Jan 20 '18 at 08:11
  • @MohamedMagi and you could do that through a Windows Explorer as well: see my edited answer. – VonC Jan 20 '18 at 08:14
  • I'm not sure that I got your point, I can't move source files as visual studio won't be able to read them, I simply want Git to include all my .cpp files from all subdirectories and add them to the repository. – Mohamed Magid Jan 20 '18 at 08:21
  • @MohamedMagid Got it, I misinterpreted your question at first. I have edited my answer. – VonC Jan 20 '18 at 09:00
  • Thanks that's so close to my point, I just want the subdirectory itself get excluded but not its files, so if I push the repo to a remote repo I get all .cpp files in one place without going to subdirectories – Mohamed Magid Jan 20 '18 at 09:10
  • @MohamedMagid Wait, are those files supposed to *move* somehow, or can they stay where they are, in there subfolder, but without any other files? – VonC Jan 20 '18 at 09:10
  • @MohamedMagid In other words, anyone cloning your repo will get what you put in it, exactly as you put it (the files remains in their folder) – VonC Jan 20 '18 at 09:11
  • Yeah that was my point, I wanted anyone clones it to only get file.cpp not directory/file.cpp but thanks anyway you helped a lot – Mohamed Magid Jan 20 '18 at 09:15
  • @MohamedMagid Mmm... that doesn't sound like something Git could do natively. You mentioned XCode, but that is an IDE. Maybe an IDE can filter and present you those files that way (no subfolder). But Git is much more basic: it gives you back what you put in it. – VonC Jan 20 '18 at 09:16
0

After adding the folders in the repository, move the files using git to repo directory level

git add sub1
git mv sub1/2.cpp .
git mv sub2/3.cpp .

you do not need to remove the directory considering that it is empty. Commit it after adding.

Koshik Raj
  • 108
  • 6