Reputation: 157
So, I have two directory trees, one on my desktop computer and one on my laptop. They mostly contain text files. They are supposed to be equivalent, and I try to keep them vaguely in sync, manually. It’s an inefficient method, obviously, and lacks any kind of version control.
So, I want them to be a Git repository. The problem is, the two directories aren’t identical; each one has slightly drifted from the last time I synchronized things, and my synchronizations have always been sloppy and uneven. Also, neither one is a Git repositry. So this is what I’d like to do:
This isn’t a question about merging two Git repositories—neither directory is version-tracked at present—nor is it about moving one repository inside another. This is about merging two directories into one new Git repository, using the available Git tools. Thanks!
Upvotes: 3
Views: 329
Reputation: 2981
This basic idea works...There are probably better approaches.
create new repository (start in new, empty directory):
git init
touch .gitignore #give git something to commit
git add .
git commit -m "blank directory"
create branch for directory 1:
git checkout -b dir1
touch a b c #<3 example files..replace this command by copying directory 1 in here
git add .
git commit -m "directory 1"
create branch for directory 2:
git checkout master # very important...start directory 2 from master
git checkout -b dir2
touch c d e #<3 example files..one of which overlaps..again, replace with appropriate directory
git add .
git commit -m "directory 2"
merge branches:
git checkout master
git merge dir1
git merge dir2
New master directory:
>ls -a
a b c d e .gitignore
Note that I did try a more sophisticated merge (out of curiousity) after first post. Seemed to work quite well with subdirectories, files with different contents, etc. One important caveat may be that i have the following line in my .gitconfig file:
[diff]
algorithm = minimal
[merge]
tool = vimdiff
[mergetool "diffconflicts"]
cmd = vim -c DiffConflicts \"$MERGED\" \"$BASE\" \"$LOCAL\" \"$REMOTE\"
trustExitCode = true
I used the diffconflicts tool (a vim plugin) for this one git mergetool -t diffconflicts
. It performs an at least mostly two-way merge based on diff markers. It does know about the base but I'm not sure to what degree it cares. Various other tools that are git compatible can perform two-way merges as well.
Upvotes: 2