git ignore and untrack folder on remote server

2.7k views Asked by At

I know similar questions have been asked multiple times, but I cannot get it to work for my case.

Where I am: I have a local repo that I push to a live server, that included a /content folder and its subfolders. During production that was okay, since I could make changes to /content and just push them. But since this is a website using a CMS all changes to /content will now be produced remotely.

Where I want to be: I now want to 'detach' /content and its subfolders from the code base, so that I can delete the hefty /content folder locally without it being deleted on the live server. That way I still would be able to make changes in the code and push them to the server, while people are able to use the CMS to create files and changes in the /content folder.

So what I did so far:

  1. added the folder to .gitignore:

/content/

  1. removed the folder from the index:

git rm -r --cached content/*

  1. tried to set assume-unchanged so that on push, the files in /content would not be deleted from the live server:

git update-index --skip-worktree

When I do this, I of course get a:

fatal: Unable to mark file

Since the files are in .gitignore, and they need to stay there, such that the folder will be ignored in the future. So what do I do?

3

There are 3 answers

3
Mark Adelsberger On BEST ANSWER

Updated to reflect correction from OP, that the updates had been done on the local clone rather than the server, and that the server is a bare repo with a hook that updates the working folder via checkout -f


The index flags (skip-worktree or assume-unchanged) are not the solution to the problem, but your understanding of why (that it has anything to do with .gitignore) is not correct either. Rather, those flags are set on the file's entry in the index; you can't set the flag on those files because you removed the files from the index.

You clarified that the three steps you mention (create .gitignore, run rm --cached, attempt to set index flags) were done on the local clone. So the next question would be how to sync this up with the server, without deleting the server's content directory.

The index, like the worktree, is local. So the server isn't yet aware that you've done anything. The next time you do a commit and push it, you'll need to take steps to properly update the server (or else the content directory will be lost). I recommend doing this now, so that you don't have additional changes to juggle at the same time (and so that you don't forget to do the extra steps).

You'll need to jump back and forth between the local and the server, so I'd go ahead and open a shell on each.

On the server, you need to prevent the worktree from being automatically updated by the upcoming push. Because your server-side repo is bare and the update occurs by way of a hook, disable the hook. (Either move the post-receive script out of the hooks directory, or edit it and comment out the lines that checkout the changes.)

Now locally you need to stage the .gitignore file and commit the changes (which will include the removal (from the index) of the content directory), and push:

git add .gitignore
git commit
git push

Now on the server you need to sync up the work tree, and re-enable the hook. Since the only change (other than suppressing the content folder) was the .gitignore file

git checkout --work-tree /path/to/webroot -- .gitignore

will sync things back up. Then undo whatever you did to disable the hook.

4
Fabian S. On

You shouldn't use git as a deployment tool.

What you could do: have a remote repo without the content folder. Add content folder to .gitignore. Have your live server pull from that remote repo. This will not change the content of your content folder.

Alternative: have your content somwhere outside your git-folder.

Edit: To solve you problem: do the following on the remote:

git rm -r --cached content/*
git commit -m "content deleted"

then backup you local content folder and git pull.

7
RoshanKumar Mutha On

Just to clear concept of gitignore first,

  • .gitignore file says i will not detect these files and folder as a part of version controll system.
  • If file is already part of version control system and if you try to ignore then it wont be ignored.
  • You can not ignore file which are part of remote repostiory. Only when you delete it from remote repo and add the folder in gitignore then it will be ignored. Note : YOu wont have content/ on remote repo in this case.

Best Solution :

Let it be on remote repository and you do not commit the files of this folder.

Hope it adds value to your question