1

I have a git repo for a project. Since there were some issues in the codebase and it was hard to identify and fix, to meet the deadline I create a new project on my local machine and started to re develop the entire application. After developing, I ran the git init command and then ran the git remote add origin <path to the original repo>.

When I try to push the changes, git did not allow me to push the changes and the message I got was you need to at least perform a single commit. I somehow had to secure my changes and created another temporary repo on the serve and finally pushed my changes to the new repo.

I need to move my new repo changes to my original repo and preferably on a new branch. How can I get this done? Please let me know the steps to get this done.

zilcuanu
  • 3,451
  • 8
  • 52
  • 105

2 Answers2

3

This is not a problem for git. Check out the answer here. For you, this would be:

cd <<original_repo>>
git remote add new_repo <<url_to_new_repo>>
git fetch new_repo
git push origin refs/remotes/new_repo/master:refs/heads/new_repo_master

This will create a new branch in origin called new_repo_master that contains the commits from the master branch in your new repository.

Community
  • 1
  • 1
houtanb
  • 3,852
  • 20
  • 21
0

There are available two possible solutions,

Submodules

Either copy repository A into separate directory in larger project B, or (perhaps better) clone repository A into subdirectory in project B. Then use git submodule to make this repository a submodule of a repository B.

This is a good solution for loosely-coupled repositories, where development in repository A continues, and major portion of development is separate stand-alone development in A. See also SubmoduleSupport and GitSubmoduleTutorial pages on Git Wiki.

Subtree merge

You can merge repository A into a subdirectory of a project B using the subtree merge strategy. This is described in Subtree Merging and You by Markus Prinz.

git remote add -f Bproject /path/to/B
git merge -s ours --no-commit Bproject/master
git read-tree --prefix=dir-B/ -u Bproject/master
git commit -m "Merge B project as our subdirectory"
git pull -s subtree Bproject master

Or you can use git subtree tool (repository on github) by apenwarr (Avery Pennarun), announced for example in his blog post A new alternative to git submodules: git subtree.


I think in your case (A is to be part of larger project B) the correct solution would be to use subtree merge

Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65