Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Remove files/folders from repository, but not from git clones when running git pull

Tags:

git

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

like image 877
user1236048 Avatar asked Nov 02 '22 13:11

user1236048


1 Answers

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.

like image 193
Anshul Goyal Avatar answered Nov 15 '22 03:11

Anshul Goyal