4

I want to move all the content in a git repository one level up, and need this to apply for every branch I have. Literally, I have:

+--repo
+----+.git
+----+folder
+--------+ A
+--------+ B

And I want simply:

+--repo
+----+.git
+----+A
+----+B

This post How can I move all git content one-level up in the folder hierarchy? has an nice answer, suggesting to do:

git mv folder/* ./ -k

Now how do I do with all the branches? I can think of:

  • do the git mv for master, then git rebase for all, on master

  • do the git mv for all branches?

Is there an advantage of one over the other? I saw also some answers based on git filter-branch --index-filter (answer here) but don;t understand how to apply it to my case.

Thanks!

Matifou
  • 5,399
  • 1
  • 32
  • 41
  • You could move the folders, and then add those changes? – evolutionxbox Oct 31 '18 at 13:53
  • but would you do that for every branch, or for one then rebase? Thanks – Matifou Oct 31 '18 at 18:49
  • Depends on the commit tree structure. It might make most sense to do it on each branch rather than relying on `filter-branch` or `rebase`. – evolutionxbox Oct 31 '18 at 18:53
  • I always had the .git and folder at the same level, and all branches were based on that structure, so it seems a "one for all" operation would make sense? What do you mean by *depends on the tree structure* exactly? Mind elaborating on that in a post? Thanks a lot!! – Matifou Nov 01 '18 at 17:35
  • Do you understand what a commit tree is? – evolutionxbox Nov 01 '18 at 19:59
  • not deeply I guess! So if your answer answer could not assume good knowledge of the commit tree, would be great, thanks :-) – Matifou Nov 06 '18 at 19:49
  • This will take a while, I’m not great at writing answers. – evolutionxbox Nov 06 '18 at 19:50
  • `git filter-branch --subdirectory-filter folder --tag-name-filter cat -- --all` will rewrite your entire history, – jthill Nov 30 '20 at 04:18
  • It looks like you're not looking to rewrite the history, so `git mv` on `master`, and then rebase (or merge) of other branches is the most straightforward way. – root Dec 04 '20 at 09:42

1 Answers1

7

I would use the new git filter-repo, which does replace the old git filter-branch or BFG.

It comes with many usage examples, including path-based filtering, in order for you to move folder content to the top (assuming the top directory only includes folder)

cd /path/to/second/clone
git filter-repo --path-rename folder/:

By default, git filter-repo applies to all branches.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • thanks! So yes, top directory has only `.git` and `folder`, does that still fit into your assumption of *top directory only includes folder*? Slightly confused about the path you indicate, would you not do it on the main clone? Or is it just to test first? thanks again!! – Matifou Nov 30 '20 at 17:28
  • @Matifou You can test it on a separate local clone. The path rename is: "rename folder to /" (/ being the root folder of the repo). – VonC Nov 30 '20 at 17:29