git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Proposal: tell git a file has been renamed

From
EEErik Cervin Edin <erik@cervined.in>
Date
Apr 23, 2023, 09:38 UTC
Message-ID
<CA+JQ7M8avZiZuBHUtkQu2WqtR4zozU3C2ayCjBxRsQcKjAr16g@mail.gmail.com>
In-Reply-To
<01d301d97567$0e9bc0b0$2bd34210$@nexbridge.com>
On Sat, Apr 22, 2023 at 9:57 PM Jeremy Morton <admin@game-point.net> wrote:
Show 12 quoted lines
>
> https://stackoverflow.com/a/1094392/178757
>
> says:
>
> git mv oldname newname
>
> is just shorthand for:
>
> mv oldname newname
> git add newname
> git rm oldname
That's what I though but it's more like:
  git show :0:oldname > newname
  git add newname
  git rm --cached oldname
  mv oldname newname

Basically, a move but also preserving staged/unstaged contents. But yes. How git handles renames can sometimes be a bit of a PITA.

On Sun, Apr 23, 2023, 12:16 AM <rsbecker@nexbridge.com> wrote:
Show 9 quoted lines
>
> $ git add file1
> $ commit -m "Commit 1"
> $ git mv file1 file2
> $ git status
> On branch master
> Changes to be committed:
>   (use "git restore --staged <file>..." to unstage)
>         renamed:    file1 -> file2
I think your point is better illustrated
  git add file1
  commit -m "Commit 1"
  echo 'Changed' > file1
  git mv file1 file2
  git status
Which yields:
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    file1 -> file2
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   file2
Or perhaps even:
  echo Initial > file1
  seq 1 10 >> file1 # We need a larger file to detect rename with change
  git add file1
  git commit -m 'Commit 1'
  seq -i 's/Initial/Changed/' file1
  git add file1 # Stage changes
  seq 11 20 >> file1 # Add some unstaged changes
  git mv file1 file2

This will result in a staged rename, as well as the staged change 'Initial' -> 'Changed' with a bunch of additional unstaged lines of numbers 11 to 20. NB that this doesn't play nice unless the contents of file1 in HEAD and index are similar enough (at least 50%?)

Previous: rsbecker@nexbridge.comNext: Kristoffer Haugsbakk
Message 7 of 24 in “Proposal: tell git a file has been renamed”
  1. Jeremy MortonApr 22, 2023
  2. rsbecker@nexbridge.comApr 22, 2023
  3. Jeremy MortonApr 22, 2023
  4. rsbecker@nexbridge.comApr 22, 2023
  5. Jeremy MortonApr 22, 2023
  6. rsbecker@nexbridge.comApr 22, 2023
  7. Erik Cervin EdinApr 23, 2023
  8. Kristoffer HaugsbakkApr 23, 2023
  9. Chris TorekApr 24, 2023
  10. Jeremy MortonApr 24, 2023
  11. Chris TorekApr 24, 2023
  12. Erik Cervin EdinApr 24, 2023
  13. Jeremy MortonApr 24, 2023
  14. Erik Cervin EdinApr 24, 2023
  15. Jeremy MortonApr 24, 2023
  16. Felipe ContrerasApr 24, 2023
  17. Jacob KellerApr 24, 2023
  18. Felipe ContrerasApr 24, 2023
  19. Jacob KellerApr 26, 2023
  20. Junio C HamanoApr 26, 2023
  21. Erik Cervin EdinMay 11, 2023
  22. Junio C HamanoApr 24, 2023
  23. Junio C HamanoApr 24, 2023
  24. Jeremy MortonApr 24, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.