Merge two folders on Mac using Terminal and tar
I have faced a little problem today when I needed to merge two folders' contents, but with a little trick.
Basically, I usually work on projects using Subversion, so I always keep track of what's changed, but sometimes I have to give the files to third party for various - usually design or content - enhancements. The problem is that I usually just don't have time/will/both to explain how Subversion (of version control systems, for that matter) works, and just handle exported files to the third party. This approach removes version control from files, but when I get the modified files back, I want them back into Subversion structure - this way I can see what exactly was changed, as well as have a ground to blame a specific person if some parts which should have not be touched, were actually touched and some part of the system broke in the result.
So, in order to that kind of trick, I need to merge "new" un-versioned directory structure into "old" versioned one, replacing all old files with new ones, but keeping my previous .svn directories.
You can't do folders merging using Mac OS X Finder, as well as I haven't found a good way to do that using FileMerge utility from Developer Tools. But there is actually an easy way to achieve the required result - and the tool for it is good old tar utility.
The way tar works during un-archiving is that is actually merges files and folders, and doesn't replace them. So, if directories on your hard drive and inside the .tar archive have the same name, the directories on your hard drive won't get erased and then replaced with stuff from .tar, but rather have their contents and contents of .tar directories merged. Just what I need.
Here's what you need to do to merge stuff.
We have two directories: project_trunk is a versioned directory, project_changed is the un-versioned directory we received from third-party. These directories are located on the same level so we can go between them with "cd ../project_trunk" or "cd ../project_changed" commands.
Open Terminal application and to the following:
cd /path/to/project_changed
tar cf allfiles.tar *
mv allfiles.tar ../project_trunk
cd ../project_trunk
tar xvf allfiles.tar
rm allfiles.tar
You're done! The un-versioned directory structure was successfully merged into the versioned one.
Now just do svn status on the project_trunk directory and compare changes and commit. Very fast and easy :)
January 10th, 2008 at 1:44 am
Very nice.
I've been manually moving files around (or svn rm and svn add'ing) manually.
This is obviously much better.
April 7th, 2008 at 1:54 pm
I'd also add that at the end I'd do an "svn add * –force" to make sure that any new files that have been added are included in SVN. Sometimes there are little files many folders deep that have been added that you have no idea about since the default svn add skips folders that it already knows about.
June 7th, 2008 at 5:36 pm
Have a look at ECMerge. It's a diff/merge tool running on Windows & MacOS, but also Linux and Solaris. It does side by side and 3-way merge for files and folders. You can use it to merge SVN branches or any other directory.