4

I have a Drupal project, and we want to use Git for version control, but we need to have some folders that are ignored by Git. The problem is that .gitignore seems to be a core file. After the last "drush up" command, the gitignore file was replaced.

So, other than asking each developer to manually add entries to his or her global .gitignore file, is there a good way to effectively .gitignore a path in Drupal?

  • 2
    The problem is that the gitignore is part of the drupal project. It is related in that a drupal project cannot have its' own gitignore because Drupal updates can overwrite it. – TMorgan Mar 23 '15 at 14:26
  • 3
    The file is distributed with Drupal, and is in the repo. How to properly handle it in the context of a Drupal site is 100% on topic here. – mpdonadio Mar 23 '15 at 14:56
  • Look into Git Sub Modules .gitmodules. – earthmeLon Mar 23 '15 at 20:05
6

While .gitignore, .htaccess and robots.txt will come as part of the package when you clone Drupal, they should not be considered part of core and you should not feel that they cannot be changed or replaced with your own files.

This is what I do: Prior to updating (drush up), I make a backup of those three files. After updating, I use the CLI program diff to compare the newly updated version with my backup (typically, my .gitignore have more entries at the end than those that come with Drupal). If nothing else has changed, I just overwrite the one that came with Drupal with the backup. If diff shows that the "drupal part" of .gitignore has changed, I copy those changes into my backup version before overwriting. (Same procedure with .htaccess and robots.txt.)

4

I use a global gitignore file https://www.drupal.org/node/1054616#comment-4139810

Place the following in ~/.gitignore:

*~
*.orig
*.rej
*.patch
*.diff
LICENSE.txt
.directory

Then run this:

git config --global core.excludesfile ~/.gitignore

To add a file that is in the ignore list (like a .patch file) do this

git add --force awesome-sauce.patch

Side note: To clean up all files that are not committed you can run this

git clean -x -f -d

-x : This allows removing all untracked files.
-f : git clean will refuse to delete files or directories unless given -f, -n or -i.
-d : Remove untracked directories in addition to untracked files.

2

It turns out that we solved the issue by creating a new repository with the Drupal site being a subdirectory. Because this was a new repository, that wasn't complicated to do.

  • 1
    You should never mix your cloned version of the Drupal repo with your own repo - not even as a subdirectory. Manage repos for different projects under version control seperately. Then copy the latest version of HEAD from your repos to the production server when you need to update. – Free Radical Mar 23 '15 at 15:01
  • Actually, this is a site that was created using the drush site-install command. The gitignore file is part of the fileset that drush downloaded, but the site was not created via a git clone. With that having been said, what I have is a project directory containing my .git directory and my .gitignore, with an html directory containing all the files drush downloaded (including the drupal project .gitignore, but not a .git file). We plan to use "drush up" to handle all drupal core updates. So, @FreeRadical, do you see this as something that will cause problems? – TMorgan Mar 23 '15 at 15:47
  • The .gitignore that drush downloaded in not used by Drupal. Its only purpose is to protect clueless people push from shooting themselves in the foot if they later do git init and push. You can use drush up to update (beware of that method if there are contribs that need mods, (e.g. CKEditor), as drush up will zap all those mods. A safer method is drush up drupal - and handle contribs on a case-by case basis. I don't foresee any problems of having a drupal production site as a subdirectory of your own .git repo, by my gut feeling is that mixing dev and production is a bad idea. – Free Radical Mar 23 '15 at 16:00
  • 1
    .gitignore files are recognized recursively by git. Making the Drupal site a subdirectory of the project git repository is an ideal solution. – greg_1_anderson Mar 23 '15 at 16:17
2

Although the .gitignore file is included in Drupal core releases, the general rule of thumb to never modify core does not apply to it. It is intended to be modified on an as-needed basis, and comments in the .gitignore file itself call this out.

As stated in the question, since the .gitignore file is included in the Drupal release package, your customized version of it can be unintentionally overwritten when an update is performed. One solution to this is to place your modified .gitignore file itself under version control. This can be done simply by commenting-out the .gitignore entry in the .gitignore file, and performing a commit to your repository.

This solution has several advantages:

  1. Your customized version of .gitignore won't be lost if accidentally overwritten.
  2. You can intentionally overwrite .gitignore with the version included in the Drupal release package, and then use git's diff and merge functionality to combine any changes made in the official release in a manner in which you're probably already familiar with.
  3. As your project evolves, having a way of tracking changes to any file in your project (including development-related files like .gitignore) can, of course, be very advantageous.
0

You can add .gitignore itself to your local copy of .gitignore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.