0

I have two files A (containing foo) and B (containing bar). I want to delete A and rename B to A so that I'm left with one file called A containing bar. And I want Git to track of the history of the bar text.

However Git treats it as if B has been deleted and A changed, thereby losing the content history.

By way of example:

$ git init
Initialized empty Git repository in /Users/tamlyn/git-test/.git/

$ echo foo > A.txt

$ echo bar > B.txt

$ git add .

$ git commit -m "Initial"
[master (root-commit) 043862d] Initial
 2 files changed, 2 insertions(+)
 create mode 100644 A.txt
 create mode 100644 B.txt

$ git mv B.txt C.txt

$ git commit -m "Moved"
[master 5a74a66] Moved
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename B.txt => C.txt (100%)

$ git rm A.txt
rm 'A.txt'

$ git mv C.txt A.txt

$ git commit -m "Move and delete"
[master 6b653a9] Move and delete
 2 files changed, 1 insertion(+), 2 deletions(-)
 delete mode 100644 C.txt

$ git log --stat -M
commit 6b653a91732ea0be92cc1e4e8d6b2af1a1133c70
Author: Tamlyn Rhodes <tamlyn@***.org>
Date:   Wed Dec 9 15:46:16 2015 +0000

    Move and delete

 A.txt | 2 +-
 C.txt | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

commit 5a74a663acd8563f8d2217de7369c89a591a7827
Author: Tamlyn Rhodes <tamlyn@***.org>
Date:   Wed Dec 9 15:45:43 2015 +0000

    Moved

 B.txt => C.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

commit 043862d818562b23f4ef43c3fac36b7c384e0d89
Author: Tamlyn Rhodes <tamlyn@***.org>
Date:   Wed Dec 9 15:45:13 2015 +0000

    Initial

 A.txt | 1 +
 B.txt | 1 +
 2 files changed, 2 insertions(+)
Tamlyn
  • 22,122
  • 12
  • 111
  • 127
  • Every time I see a Git question I think of [XKCD](https://xkcd.com/1597/) – Draco18s no longer trusts SE Dec 09 '15 at 16:05
  • Possible duplicate of [Is it possible to move/rename files in git and maintain their history?](http://stackoverflow.com/questions/2314652/is-it-possible-to-move-rename-files-in-git-and-maintain-their-history) – Kristján Dec 09 '15 at 16:11
  • @Kristján thanks but I don't think that answers my question. Please see my sample shell output for details. – Tamlyn Dec 09 '15 at 16:26
  • I'm not sure I understand what you mean by "losing the content history". However, is it possible to break the deletion of `B.txt` into a single commit, then move `C.txt` to `B.txt` in its own commit? – Marshall Davis Dec 09 '15 at 16:29

0 Answers0