0

First, I saw a solution for this using git subtree split

However, the solution above only move 1 branch at a time (usually master branch). Now I want to move all of the branches (about 100 branches) from that subdirectory, is there a way to do it using git command?

Tung Do
  • 73
  • 2
  • 2
  • 12
  • You could proceed "the other way", I mean `clone` your whole repo then delete all subtrees but the one you want to keep. Also, did you take a look at Paul's answer in the question you linked? – Romain Valeri Sep 25 '18 at 08:34
  • Pauls' answer is like i said, only move 1 branch at a time. I want to move all of the branches belong to that subdirectory. The clone method is not good too, since when you clone, you can only clone 1 branch. I have 100 branches in the remote repository. – Tung Do Sep 26 '18 at 06:47

1 Answers1

1

If I understand correctly want you want to achieve, I think you should try the --subdirectory-filter of the filter-branch command :

    git filter-branch -f --subdirectory-filter "src/" --prune-empty -- --all

Replace src/ with the name of your subdirectory. The option --all will perform the action on all branches.

Hope this helps.

EDIT: The command will only change the root directory of your local branches. If you want to change the root directory for all remote branches, I suggest you use this trick :

  • first you clone the remote repository with the --mirror option, which leads to a bare local repositorey with all branches,
  • and then you make this local repository a regular one.

Thenin this newly cloned repository that contains all branches, you can apply the filter-branch command.

With the option --subdirectory-filter, this command will set the specified subdirectory as root of the local git repo.

The --prune-empty option will discard all commits that are empty (because they refers exclusively to files outside the subdirectory).

It is important to understand that all the git history will be rewritten as if the subdirectory were always the root of the git repo.

I hope I help you understand this command.

Romain Warnan
  • 1,022
  • 1
  • 7
  • 13
  • Hi Sir, i dont really understand the behavior of the comment above. And could you kindly add of the steps needed to perform the task? note that i did it from the clone repository step. So in my local repository, i only have the master branch. – Tung Do Sep 26 '18 at 06:50
  • @TungDo I have edited the answer, I hope this answer your questions. – Romain Warnan Sep 26 '18 at 08:51