简体   繁体   中英

How to stop tracking the content of a directory in git?

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?

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.

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/*

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM