I have a somewhat confusing question in Git.
Lets say, I have a file dir1/A.txt
committed and git preserves a history of commits
Now I need to copy the file into dir2/A.txt
(not move, but copy).
I know that there is a git mv
command but I need dir2/A.txt
to have the same history of commits as dir1/A.txt
, and dir1/A.txt
to still remain there.
I'm not planning to update A.txt
once the copy is created and all the future work will be done on dir2/A.txt
I know it sounds confusing, I'll add that this situation is on java based module (mavenized project) and we need to create a new version of code so that our customers will have the ability to have 2 different versions in runtime, the first version will be removed eventually when the alignment will be done. We can use maven versioning of course, I'm just newbie to Git and curious about what Git can provide here.
All you have to do is:
You will be able to see historical attributions (using git blame
) and full history of changes (using git log
) for both files.
Suppose you want to create a copy of file foo
called bar
. In that case the workflow you'd use would look like this:
git mv foo bar
git commit
SAVED=`git rev-parse HEAD`
git reset --hard HEAD^
git mv foo copy
git commit
git merge $SAVED # This will generate conflicts
git commit -a # Trivially resolved like this
git mv copy foo
git commit
After you execute the above commands, you end up with a revision history that looks like this:
( revision history ) ( files )
ORIG_HEAD foo
/ \ / \
SAVED ALTERNATE bar copy
\ / \ /
MERGED bar,copy
| |
RESTORED bar,foo
When you ask Git about the history of foo
, it will:
copy
between MERGED and RESTORED,copy
came from the ALTERNATE parent of MERGED, andfoo
between ORIG_HEAD and ALTERNATE.From there it will dig into the history of foo
.
When you ask Git about the history of bar
, it will:
bar
came from the SAVED parent of MERGED, and foo
between ORIG_HEAD and SAVED.From there it will dig into the history of foo
.
It's that simple. :)
You just need to force Git into a merge situation where you can accept two traceable copies of the file(s), and we do this with a parallel move of the original (which we soon revert).