5

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 .gitignore
  • is accidentally pushed into the repository
  • fix the .gitignore file
  • i need to remove it from the repository, but when I perform git pull on the server the file should not be removed.
  • in this example it's very simple with just one file to manually backup on the server, git pull (the file is removed), restore the backup.
  • but in my case there are a lot of different folders/files.

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

Community
  • 1
  • 1
user1236048
  • 5,542
  • 7
  • 50
  • 87
  • Pulling doesn't change what is on the server. Do you mean pushing? – bengoesboom Oct 03 '13 at 14:16
  • @bengoesboom - i'll update my question with more details in a couple of minutes. maybe it's not too clear – user1236048 Oct 03 '13 at 14:16
  • So you have repos `you | common | server` push/pulling across the vbars, and you want to erase all trace of the oopsie from the commit histories without deleting the flotsam from the server worktree, yes? – jthill Oct 03 '13 at 15:01
  • @jthill - 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 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` -- does this make sense? – user1236048 Oct 03 '13 at 15:06
  • @alex.dominte Add that to the question description. It makes sense now, but didn't make earlier. – Anshul Goyal Oct 03 '13 at 15:11
  • So the error was committed in `local-machine` and pushed or pulled into `repo-server` and `deploy-server`? (edit: I presume other repos will clone from `repo-server`, just checking here) – jthill Oct 03 '13 at 15:13
  • @jthill - yes the `wrong` files were committed and pushed to the server already, and pulled to the `deploy-server` and also a lot time ago I assume, I just stumble upon them now – user1236048 Oct 03 '13 at 15:14

1 Answers1

2

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.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • +1 - for the detailed answer and suggested solutions, but I'm sure you understand that those solutions are way too complex for an apparently simple problem. thanks – user1236048 Oct 03 '13 at 17:07