Git: How to push a subdirectory to a separate branch of the same repository
The current project I am working on is limited to one git repository.
My current working directory looks something like this:
dist/ *distribution files src/ *src files
We have an Azure server set up to automatically deploy anything pushed to the ‘deploy’ branch of our repo. So the objective is to push src files to master and the content of the dist folder to the ‘deploy’ branch.
What I’ve been doing up until now is branching off master, deleting the src folder, and moving the contents of the dist folder to the root before pushing to deploy.
I know ideally we would have separate repos, but that’s not an option currently. Is there an easier way to do this?
One Solution collect form web for “Git: How to push a subdirectory to a separate branch of the same repository”
The git-subtree command works great for this. From your
master branch do:
git subtree split --branch deploy --prefix dist/
Just run that command every time you want to update the
deploy branch. It will merge every commit in the
dist subdir (of the current branch–presumably
master) to the root of the
deploy branch and maintain a full commit history. Note that only changes in the
dist subdir will be merged. If a commit includes changes both to the
dist subdir and other files in the repo, only the changes in the
dist subdir get included.
The tricky part is installation. As I understand it, the command is included with git 1.7.11 and higher. However, it’s in the “contrib” subtree, so it’s not installed by default. Search for “git-subtree” in your system’s package manager and install the package if it exists. If not, or if you are using an older version of git, the easiest way I’ve found is to install git-subtree from the author’s repo.
git clone https://github.com/apenwarr/git-subtree.git cd git-subtree/ make install
Once you get it installed, you can do various cool things with it as explained in the docs.