10

I have a django project managed with git. I am git pushing it to my host. Now, I want to be able to push just one of the directories (inc. all sub dirs) to another git repo. How is it possible, if it is possible at all?

Edit: So I want that directory to be a git repo itself.

igauravsehrawat
  • 3,696
  • 3
  • 33
  • 46
yasar
  • 13,158
  • 28
  • 95
  • 160

3 Answers3

21

You could look at git's support for submodules. You would add the second repository as a submodule to your main project, e.g. with commands like:

git submodule add git://wherever/blah.git library-code
git commit -m "Added a new submodule called 'library-code'"

When you change into the library-code subdirectory, it's as if the parent repository doesn't exist - you can change origin to use a transport that you can push over and then push as if it were completely independent.

To specify that you want the submodule to be at a particular version, you should change into the submodule and use git checkout to switch to the right version. Then you change back up to the main repository and stage and commit that new submodule version with:

git add library-code
git commit -m 'Change the submodule version'

The tree of the main repository just stores the version that the submodule should be at, so when you push the main repository, it's not pushing any of the files in the submodule.

In order to split off this subdirectory, while preserving history, you'll need to clone your original repository and user git filter-branch to rewrite the history, as described in this answer:

Then you can push that to a newly created repository GitHub repository, return to your original project, remove the subdirectory, and replace it with the submodule as described above.

If you're not very familiar with git concepts then this may be difficult for you - I would recommend reading up on git submodules beforehand.

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 2
    I already have code in my sub-directory, so I want to convert them to git repo, and push them to a fresh remote repo (e.g github). Can you give an example of doing that? I am sorry that I am so newbie at these thing :) – yasar Aug 22 '11 at 16:33
  • 1
    Do you want to preserve the history of everything in the subdirectory? – Mark Longair Aug 22 '11 at 16:35
  • And what about submodule when I push bigger repo to other remote repo? Does that include submodule's files? – yasar Aug 22 '11 at 16:36
  • preserving history doesn't matter in submodule, so long as history in bigger repo doesn't messed up. – yasar Aug 22 '11 at 16:40
  • 1
    I hope I've added those questions in my edits to the answer. If you don't care about the history of the subdirectory then you don't need to worry about `git filter-branch` step - you could just copy the contents of the subdirectory into a newly created git repository, and push that to GitHub. – Mark Longair Aug 22 '11 at 16:46
1

When you make changes to a particular directory, stage those changes and commit and push, you are effectively "pushing" just that directory.

If the other Git repo has only the directory you want to push, and that is why you want to have it as a repo in itself, you can look at Git Submodules.

manojlds
  • 290,304
  • 63
  • 469
  • 417
1

Have you looked into git submodules? It allows git repositories to cleanly nest, but remain fully independent.

This means you can have a subdirectory with a different remote url for push/pull and different commit tree+log without cluttering up the main project.

https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial

lunixbochs
  • 21,757
  • 2
  • 39
  • 47