I've added and committed a wrong folder. After fixing the .gitignore file, I need to remove those files from the repository, but I don't want those files to be deleted when I run git pull
on the server.
I've tried the --cached
flag as suggested here Remove a file from a Git repository without deleting it from the local filesystem
but it just doesn't work. I remove the file by rm --cached file
. I commit the fact that is deleted. But when I git pull
it removes that file from the server.
Updated Example: SF2 parameters file (should be ignored, as it differs from machine to machine)
app/config/parameters.yml
- a file that should be ignored in .gitignoregit pull
on the server the file should not be removed.By server I mean a clone of the repository, not the repository itself. It's just another machine with the repository cloned
Updated Short Explanation:
Let's consider repo-server | local-machine | deploy-server
.. I want to remove via local-machine
some files from the repository (if another local-machine-2
git clone
the repository it doesn't get these files), BUT when I update the deploy-server
via git pull
I don't want those files to be removed from the deploy-server
IMO, you have three options here.
1) Use update-index
with --assume-unchanged
flag
git update-index --assume-unchanged app/config/parameters.yml
git update-index --no-assume-unchanged app/config/parameters.yml #Undo effect of previous command
You won't be removing the file from your git repo, but will be ignoring any local changes to it using this one.
Problem with this is, if any upstream commits update these files, git will abort the pull and thow up an error saying, Your local changes to the following files would be overwritten by merge
. You might remember right now that you ran this command, but some time later, you won't, and might have a hard time figuring what is wrong.
2) Use git submodules
For using submodules, you will have to completely remove the folders (say app/config
) from the parent repo
git rm -r --cached app/config/
and create a new repo inside app/config/
Potential problems - 1) multiple submodules might be required depending on your app structure 2) If all that is changing are config files, this will be a definite overkill in terms of headaches you will face.
3) Use nested git repositories
In this one, you create a git repo inside the first one
cd ~/Desktop/project
git rm -r --cached app/config/
echo app/config >> .gitignore
cd app/config
git init && git add . && git commit -m "config paramters"
The difference between 2 & 3 is that with git submodules, you can get the exact relationship among the repos, but with nested repos, you will miss out on that information.
Also you will need to create an extra git repo for each such folder in submodules, but you can use a single extra repo to synchronise all such changes using a shell script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With