2

I have a directory named img. I want to that directory be exits on the repo, but always it should be empty. I mean just the content of that directory should be untracked.

Here is a simplified of my project:

myproject/ .git/ application/ public/ img/ img1.jpg img2.jpg vendor/ composer.json composer.lock 

I want to make it like this on repository:

myproject/ .git/ application/ public/ img/ vendor/ composer.json composer.lock 

How can I do that?


According to some researches, I have to use .gitignore file. But how? Should I make a .gitignore file on the root of project and write this in it?

// .gitignore file public/img/* 

Or should I make a .gitignore inside img directory? If yes should I write what thing in it?


Ok, now I got a new problem. Let's explain what I want to do exactly. Here is the scenario:

I develop my website locally and push it on the github repository. Then I go in the server and do a git pull. Also there is an img directory that contains all user's avatars. All I'm trying to do is to keep the real-users avatar on img directory. If I put the whole img directory inside .gitignore, then avatars won't be created (because of lack of img directory). If I exclude the content of img directory, then all current user's avatars will be gone when I do a new git pull. Any idea how can I handle such a situation?

2
  • If you want the directory to exist but its contents to not be tracked, the convention is to put a .gitkeep file in img/, along with a .gitignore that excludes everything except the keep file. But that ignore file could be in a parent directory, see e.g. github.com/textbook/cyf-library/blob/master/server/… and github.com/textbook/cyf-library/tree/master/server/static where I've done a similar thing. Commented Jul 11, 2018 at 12:24
  • Git is not a deployment tool. It can be used as one, but it isn't meant to be one. A proper deployment tool would extract the commit to be deployed, add any desired empty directories, and then deploy that. Don't try to do this inside Git itself. Commented Jul 11, 2018 at 14:44

3 Answers 3

7

Git doesn't track the directories, only the files.

In order to add an empty directory in the repo you have to put a file in it and write the correct rules in the project's .gitignore.

But, because a Git repository allows placing a .gitignore file in every directory you can put one in the directory you want to exclude and put these lines in it:

# Ignore everything in this directory * # ... but do not ignore this file !.gitignore 

Add the new file to the repository and commit it.

That's all.


If you want to keep all the ignoring rules in a single place (in the .gitignore file in the root directory of your project) then you can put an empty file in the directory to ignore and puts the rules above prefixed with the path into the .gitignore file.

Usually, such an empty file is named .gitkeep (the name doesn't matter, this one is used to express its purpose). In this case, add to the .gitignore file of the project:

/public/img/* !/public/img/.gitkeep 

If you have already added the files from public/img to the repository (and committed them) you have to remove them first in order to not be tracked any more. Run

git rm --cached public/img/* 

and commit this change together with the changes in .gitignore. The --cached argument tells git rm to not remove the files from the working tree but only stop tracking them.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you .. can you please take a look at my edited version?
I never do git commit on the server. Do I still need git rm --cached ?
You don't run this on the server but on your development machine. If you didn't add the files from public/img in the repo before then you can skip this part.
2

You should see a .gitignore file in the working directory by issuing a ls -la. If not create a .gitignore file there(on the same level as the .git file).

In that file you can add the path to the img folder as such: public/img/*

1 Comment

Thank you .. can you please take a look at my edited version?
0

Git doesn't track directories, only files. What you can do is track any file in that directory, like a .gitignore, or even some file called .placeholder. You will also want to unversion the files in there but not remove the actual files, you can do this with git rm --cached file

# Untrack, but keep the existing files in img git rm --cached myProject/public/img/* # Create the placeholder file to keep the directory tracked # You do not need this if any other versioned file exists # in this directory or potentially any subfolders of this # directory. echo 'placeholder' > myProject/public/img/.placeholder # Track the placeholder file in the img directory git add myProject/public/img/.placeholder # Commit your work git commit -m 'YOUR COMMIT MESSAGE' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.