-1

When I use git reset --mixed on a branch where I used git mv, the renames are replaced by delete and recreate.

Is there a way to avoid that?

Vincent J
  • 4,968
  • 4
  • 40
  • 50
  • renames are not really tracked by git so if it's detected as a deletion and an addition of a file with a different name, that's good. – eftshift0 May 23 '23 at 11:11
  • you are describing the expected behvior of git, I tried to explain it in my answer. Can you give more details on your intentions ? for example: for what kind of task do you use `git reset` ? – LeGEC May 23 '23 at 11:29

2 Answers2

2

Try using soft instead of mixed.

git reset --soft <commit>
git commit -m "Preserve renames"

It moves the branch pointer to a specific commit without modifying the staging area or the working directory. This means that it keeps the changes you made, including renames, in the staging area.

Alka Singla
  • 369
  • 2
  • 7
0

In git, there is no difference between "rename" and "deleted + added".

If you look more closely at what happened after your reset, you should see that git status mentions the deletion of a tracked file and the presence of an untracked file.

If you know of one individual rename and run :

git add <old_name> <new_name>

git status show now indicate that a rename is staged.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • 1
    https://git-scm.com/docs/git-mv says you can rename files. and git status indicates: "renamed:" – Vincent J May 25 '23 at 16:50
  • `git mv` combines *an actual rename of your file/directory on disk* together with actions in git index to track the renamed files instead of the original files. Inside git storage, there is no such thing as a renaming, it is the exact same thing as `git rm --cached old; git add new` – LeGEC May 25 '23 at 18:00
  • see [this answer](https://stackoverflow.com/a/60231214/86072) or [this answer](https://stackoverflow.com/a/60927364/86072) – LeGEC May 25 '23 at 18:04