We have a problem where we forgot to add a directory of build-dependant files to version control several weeks ago and we now want to get them added so that we can build when we check out older revisions.
I know we can use git rebase -i to edit a series of commits, but that this should not be done to history that has already been pushed since this will cause merge issues for the rest of the team.
I also know that using filter-branch will allow us to remove files from older commits that have already been pushed, and forcibly pushing these change out will not cause problems for others (I've successfully done this already).
My point is there seems to be times where editing pushed history seems OK, and times where it is not, so I have hope that our problem is resolvable.
Is it possible to add these files to history that has already been shared?
Yes, but it will change that history. Anyone that has pulled this data will have to deal with it. See the section in the rebase man-page in the section Recovering from Upstream Rebase for what will need done. It's a good idea to let anyone affected know before continuing so they can commit any changes
If you are fine with doing this, checkout the first commit that should have had the missing files, create a branch, add the files and ammend the commit.
git checkout <commit hash>
git branch new_branch
<add the missing files>
git add .
git commit --amend <-- a text editor will open, just keep the message and continue
At this point, the tree will look like this, with B being the commit prior to being amended and B' the commit after amending. C - D - E are the commits since then.
- A - B - C - D - E
\
- B'
Now a rebase of C - D - E onto B' to 'fix' the history.
<`B` should still be checked out>
git rebase -i E
An editor will open listing the commits to be moved, B may be the first one in the list -- if it is remove it, leaving just C, D and E in the file. Close and save the file to begin the rebase.
If the files were never added in a later commit, there should not be any conflicts -- but if there are they will need to be resolved.
The tree will now look like this
- A - B - C - D - E
\
- B' - C' - D' - E' <-- E' will be checked out.
If the commits B - C - D - E are no longer referenced by branches they will not show up in git log and will be removed by the garbage collection at a later time. This leaves you with A - B' - C' - D' - E'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With