Cleaning up Mercurial & Git repos

Recently, I cloned a repo that I created on my laptop and pushed to Bitbucket back down to my desktop. At that point, I discovered that I had included all the vendor files for the project within the repository. That’s a no-no. It’s a waste of space, mainly because the dependent libraries can be downloaded by running composer once the new clone is created. So, I decided to remove the vendor files from the repository without dropping them from the file directory.

I thought I had solved the problem by updating Mercurial’s .hgignore file, which is used by Mercurial to mark which files and folders to not track. Unfortunately, the vendor files had been included in the initial repository creation. Going forward, .hgignore would ignore new files and folders in the vendor directory, but I still needed to forget the files already listed. It turns out that ‘hg forget file_name’ would do the trick.

I wanted to be sure it worked, so I created a ‘tests’ folder and touched a file inside that folder. Sure enough, the file appeared in Mercurial’s commit list. I ran a commit, then tried

> hg forget a.php

This removed the file from the committed list, but the file still appeared in the staging area, which did not make sense. When I updated .hgignore to not track the ‘tests’ directory, the new file disappeared from the staging area. That’s what I wanted.

I now had all the files in the vendor directory for forget. I moved to the vendor directory and entered the following command to forget the .json files:

> hg forget -I **.json .

That removed the .json files from the commit list. I did the same for .js, .map, .txt, .conf, .tpl, .yml, .css and .html files. Oddly, only one .php file was forgotten in this manner in the vendor directory. I wonder what might happen if I try ‘ > hg forget -I vendor/** .’ ? (I may try that if I have to clone this repo again.)

Git

It takes less command line work to forget files in git. This command did it all:

> git rm -r –cached vendor

where rm (folder remove) -r (recursively) –cached (from index only) vendor (the folder name). That’s much easier.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s