简体   繁体   中英

gitignore files have been deleted locally

I'm trying to figure out what the best approach is for my situation. I had removed everything from my repository and recommitted in order for my gitignore files to work. It did, however, I switched to another branch that is still tracking everything, then checked out the original branch, and now those files are not there.

From what I understand this is the correct git behaviour, but I was wondering how I could stop tracking these core files while keeping them locally in order for my site to function?

Should I merge branch branch A (no core files) with branch B (core files), then clear the repository and recommit?

Actually your question is many fold. First, when you want to drop files to be tracked further on a branch, but keep the files themselves you will use

git rm --cached FILE
// possibly edit .gitignore here and `git add .gitignore`
git commit -m "removed the file FILE"

On the other hand, it seems that you want this for a checkout procedure for the contents of web site or something similar.

As you want to merge A and B ,it seems you want this rule (removing FILE from the index) for all branches. This can be done with

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatched HEAD -- FILE'

. Be careful to have the FILE s in your work tree or have a backup outside of your repository, as the HEAD you fire this command on, will loose all ideas of what these files ever had been, so you cannot check them out again from the history! You will either need them in your work tree or have a backup before you filter your history.

I dislike the idea, for some reasons, to have the repository that near to a working directory that might contain a mix of tracked files, additional files or even files from other repositories, that track a distinct set of files.

For such a use case it is often useful to use a second --work-tree for your repository

git --work-tree=WHERE_YOU_PUBLISH_YOUR_FILES checkout HEAD -- .

to checkout the tracked files from HEAD to your htdocs or whatever. Inside of WHERE_YOU_PUBLISH_YOUR_FILES you can put any other files, without touching the repository.

You can check differences of the tracked files, of a repository, against the work tree with

git --work-tree=WHERE_YOU_PUBLISH_YOUR_FILES diff

. You can even check in the files from WHERE_YOU_PUBLISH_YOUR_FILES into the repository

git --work-tree=WHERE_YOU_PUBLISH_YOUR_FILES add -u

To simplify this operation I often use global aliases.

git config --global alias.public '!git --work-tree $(git config --get public.path)'
git config public.path /var/www/localhost/htdocs

Now you can use

git public status -s -uno

or similar commands.

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