I’m pretty new to git, so forgive me if this is obvious (or impossible)!
I am using it for CMS websites. I often run into the situation where I have made modifications to a 3rd-party component that is then updated.
How can I add the changes from the updated component as if they had happened before my changes? or is this ‘rewriting history’?
Do I create a branch from an old commit where the component was introduced, add the updated code and then merge into master? submodules? or am I barking up the wrong tree and this situation is better handled another way?
(originally this answer assumed the 3rd party component came from a public git repo, @jacob-dorman has clarified that he obtains updates by copying the component’s code from snapshots of a larger project)
You need to maintain a repository with two branches:
master: every time you get a new snapshot of the component, commit it to this branchtweaks: a branch based off themasterbranch, which contains the commits that constitute your custom tweaksEvery time you update the ‘master’ branch with new snapshots of the component, rebase the
tweaksbranch on top of themasterbranch:This will leave your tweaks as effectively being the last thing that happened.
So, starting from the very beginning, this is what it would look like on the command line…
Grab the first snapshot & store it on the ‘master’ branch
That sets up your ‘master’ branch.
Add your tweaks on a new ‘tweaks’ branch
Later, when some interesting updates have been made to original component…
Grab a new component snapshot, update ‘master’ & ‘tweaks’ branches
Your master branch now has an updated history of the component, with a commit for each snapshot update. Finally rebase the ‘tweaks’ branch on to the master branch – each of the commits on your ‘tweaks’ branch will be replayed, one after the other, on top of the new tip of the master branch:
More tweaks?
If you ever need to add more tweaks, just check the branch out and add them: