1

I have a repo say A and I want to add a new repo B in it using git subtree

From inside the repo A, I can run the following command

git subtree add --prefix=.github/workflows <my-repo>.git master --squash

and the new repo is cloned inside .github/workflows folder

Now I run some find and replace commands on .github/workflows using sed. For this reason, it's important that it's a fresh git subtree add so that my variables which need to be replaced are present.

So, I can do something like this

rm -rf .github/workflows/*
git subtree add --prefix=.github/workflows <my-repo>.git master --squash
sed -i '' -e 's/MY_LABEL/MYTAG/g' .github/workflows/*.yml

This will do the job, but it will unnecessarily create commits everytime I do subtree add. However, can this be achieved using just git commands (not using rm -rf)

I plan to use this in a pre-commit hook so that it's always run before a user commits. What's the correct way to do it?

kosta
  • 4,302
  • 10
  • 50
  • 104

1 Answers1

0

A subtree pull (possibly with squash) would save you the rm command, although it might require to resolve conflict. I creates commit too and an history of the pulls made from <my-repo>.git.

I believe the subtree subcommand woud always create commits when you use it to update the subdirectory. Git stores files in a tree structure, to which each commit points to. Hence, to change a different file, the tree changes and the commit changes.

That’s said, if your repository is not shared, you may use history rewriting to change the commit that did a previous add of the subtree and update this previous commit.

Clément Joly
  • 858
  • 9
  • 27