0

I'm trying to push a branch sourcebranch from a source GIT repository to a sub-folder of a target repository. Both repositories are remote.

From the source repo I've added a remote to the target repo using the following command:

git remote add origin https://path-to-target-repo.git

And then pushed the branch:

git push origin sourcebranch

But this pushes it to the root location of the target repository when in fact I want to push it to a sub-folder of that repository.

Any guidance would be appreciated.

aw1975
  • 1,669
  • 3
  • 20
  • 35
  • Possible duplicate of [Push to a remote origin on a subfolder of git repository?](https://stackoverflow.com/questions/21911542/push-to-a-remote-origin-on-a-subfolder-of-git-repository) – phd Jun 05 '18 at 18:12
  • Has your problem been solved yet? – Marina Liu Jun 08 '18 at 08:09

2 Answers2

2

A git commit is an immutable snapshot of a version of the entire project. Each file's path within that snapshot is the same for every repo that holds the commit. You could try to get around this by using some crazy scheme that rewrites the history as the push is being completed, but if you could make it work at all the result would still be a remote that's constantly out of sync with the local (i.e. you'd be unable to push or pull cleanly).

There are two options, depending on what you're trying to accomplish.

If the remote contains multiple projects that you want shared together, but you want to modify each project individually, then probably the closest thing you can do is make a separate remote that looks just like your local, and then push to it, and set it up as a submodule or subtree of the "broader" repository.

If you're trying to use git as a deployment tool and the files just aren't appearing in the right working path, then you should either

(a) switch the remote to be a BARE repo, and use hooks to perform a chedkout operation in which you specify the git work tree directory, OR

(b) move the root of the repo work tree so that it is where you want it to be on the server, OR

(c) [my recommendation] use a proper deployment tool instead of making push do something it wasn't really designed for

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
1

If you want your files in a subdirectory, you need to create a commit locally that moves the files into the appropriate subdirectory and then push those changes to the remote repository.

mkdir mydirectory
git mv file1 file2 file3 mydirectory
git commit -m 'relocated files'
git push
larsks
  • 277,717
  • 41
  • 399
  • 399