BEFORE YOU START: Make a backup of your git repo because the filter-branch
command is destructive!
You could use git filter-branch
with the --tree-filter
option. The manual for the --tree-filter
options states:
This is the filter for rewriting the tree and its contents. The argument is evaluated in shell with the working directory set to the root of the checked out tree. The new tree is then used as-is (new files are auto-added, disappeared files are auto-removed - neither .gitignore files nor any other ignore rules HAVE ANY EFFECT!).
So for each commit we want to create a new directory src
and move all files in the tree to it (excluding src/
itself of course).
git filter-branch --tree-filter '
mkdir -p src
for i in *; do
if [ "$i" != "src" ]; then
mv "$i" src/
fi
done
' --all
The afterwards you can add the untracked bar/
directory and hello.txt
as a new commit on top.
If you want bar/
and hello.txt
to be present in the repo from the start of the history, you will have to adjust the tree filter command slightly.
git filter-branch --tree-filter '
mkdir -p src
for i in *; do
if [ "$i" != "src" ]; then
mv "$i" src/
fi
done
cp -R "/path/to/bar" .
cp "/path/to/hello.txt" .
' --all