21

Suppose the following scenario:

I have a git project that has a dir named project. Inside this dir I made some commits. Then, with cp -r I duplicated this dir to a dir called project-with-feature-b, i.e. I manually created a branch.

Now I want to merge these two folders like if they were two branches, is there any way to do that using git?

To be more specific to my problem. I have a svn repository which I am using with git-svn tool to clone it. And I cant use the -T/-b/-t options but I want to make a merge.

9
  • Can you initialize a git repository in project and project-with-feature-b and then do a git pull project-with-feature-b in project?
    – user849425
    Commented Apr 10, 2012 at 15:42
  • The problem with this workaround is that it doesn't use project-with-feature-b's story to improve the merge. Commented Apr 10, 2012 at 15:47
  • Did you try git pull --rebase project-with-feature-b? It should save the combined history of your two projects.
    – user849425
    Commented Apr 10, 2012 at 15:58
  • @meagar I think he probably copied the project directory after he already initialized the git repository. In that case the .git folder already exists so he can in fact commit in the "branch" project folder.
    – user849425
    Commented Apr 10, 2012 at 16:05
  • I have a svn repository which I am using with git-svn tool. I couldnt use the -T/-b/-t options. Commented Apr 10, 2012 at 16:10

3 Answers 3

11

This questions is very old, I joined recently.

I am using ubuntu, and use meld to solve merge issues.

So If I have 2 folders of the same app, which might have minor changes.

I will do this in the terminal

meld ./app_branch_master ./app_branch_dev

This will open up both the folder, Meld will take time to compare both the folders. It will show you what's there on left hand side and what's on the right hand side. You can also move little code from there.

That's how I do it.

1
  • the GUI is also great! Thanks a lot
    – ThePhi
    Commented Sep 29, 2018 at 16:05
8

Yes, you can technically accomplish this, but only because it is not too late to fix your mistake. You need to go back in time and do this the correct way:

  1. Move your project-with-feature-b folder outside your git repo:

    $ mv project-with-feature-b ..
    
  2. Go back through git log and find the commit ID of the last commit before you created the project-with-feature-b directory. If you haven't made any commits "inside" the project-with-feature-b directory, you can skip this step.

  3. Create a new branch called feature-b based on that commit ID. If you skipped step 2, omit <commit-id-from-step-2>

    $ git branch feature-b <commit-id-from-step-2>
    $ git checkout feature-b
    
  4. In the feature-b branch, replace the project folder with your backup of project-with-feauture-b

    $ rm -rf project
    $ mv ../project-with-feature-b project
    $ git add project
    $ git commit -m "Add feature-b"
    
  5. Go back to your master branch and merge your feature-b branch into master

    $ git merge --no-ff feature-b
    

In the future, you should use branches as above before you start doing work. Learn to use the tools correctly rather than how you think they should work.

1
  • 4
    The problem with this is that the "feature-b" history is lost. Commented Apr 10, 2012 at 16:52
3

Take a look at Detach subdirectory into separate Git repository, once you split the history of both directories you'll be able to branch, merge or do whatever you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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