Wednesday, April 2, 2014

Git: Pulling specific directories or files

Recently I had an issue when working on Demigod's project. I do not know the reason, but I did not have all the stylesheet files which should be included in the /app/assets/stylesheets/ folder. The first workaround I tried was to pull all the changes from our remote repository, just in case other member of the team would accidentally have  moved those files. The remote repository had all the files (I checked it through Bitbucket website), but no changes occurred on my local repository when I run a git pull command on the Rails console. I did not understand what was happening, but I needed those files on my local repository.

I did a quick research on the Internet, I found a solution to solve my issue. There exists a way to pull only specific directories or files. As I commented on my last post, git pull fetches and merges the remote branch, but we can use the "fetch" command, that also fetches the remote branch, but, additionally, extracts the directory or file you need from that branch by a checkout operation. Therefore, you actually need two operations, first a "fetch" to download the objects from the repository, and then, a "checkout" to update the files/directories in the working tree and match the version in the specified path:

> git fetch <remote> <branch>
> git checkout <remote>/<branch> -- relative_path_to_file_or_directory

In my case, we had to run the following code in order to update the "stylesheets" directory:

> git fetch origin master
> git checkout <remote>/master -- .app/assets/stylesheets

After running the previous commands, my local "stylesheets" directory was updated with all the files included in the same folder of the remote repository. If you need just a specific file, you can also select that file inside the folder, for example:

> git checkout <remote>/master -- .app/assets/stylesheets/home.css.scss

 Problem solved!

No comments:

Post a Comment