I have a git repository and found an old version of some of the corresponding files that I now want to insert into the git history for reference. The problem is that I do not want it to have any effect on the rest of the history. I only want to have all the history of the code in one place and thus be able to find all previous versions in the history. (Also, it is just a local repository and thus I do not have to worry about rewriting the history.) I tried to use git rebase
with the -Xtheirs
strategy but it only removed conflicting changes but kept all non-conflicting ones. Here is an exemplary repository:
mkdir insert_commit
cd insert_commit
git init
echo a > file1
git add file1
git commit -m "A"
# file1 now contains
# a
echo b > file1
git add file1
git commit -m "B"
# file1 now contains
# b
echo c >> file1
git add file1
git commit -m "C"
# file1 now contains
# b
# c
So the history is A->B->C
. Now I have a version of the code that I will call X
that is supposed to go between B
and C
, resulting in the history A->B->X->C
. The files in X
are:
file1:
x
file2:
x
I want to insert X
as specified above, but I do not want the end result C
to change, meaning I want to have C
as:
file1:
b
c
file2:
-> does not exist
My approach was to do:
git rebase -i HEAD~2 -Xtheirs
# select to edit "B"
echo x > file1
echo x > file2
# (or in reality copy all files of the old version into the repository)
git add file1 file2
git commit -m "X"
# file1 now contains
# x
# file2 now contains
# x
git rebase --continue
# file1 now correctly contains
# b
# c
# file2 now incorrectly still exists and contains
# x
Is there any way other than to insert a revert commit directly behind "X" to keep the history after the insertion point unchanged?