TortoiseGit: Moving files to new folder not recognized as move

9,217

Solution 1

Remember one thing: Git tracks file content only.

From Moving Files section of Pro Git book v2:

Unlike many other VCS systems, Git doesn’t explicitly track file movement. If you rename a file in Git, no metadata is stored in Git that tells it you renamed the file. However, Git is pretty smart about figuring that out after the fact

And, moving and rename is the same thing in Git.

So, Git detects the moving/rename automatically only works under the premise that those files are not modified.

Git does these things when you moving/rename file only:

  1. A tracked file is deleted, git gets the SHA-1 of that file content, assume the value is abc.
  2. A untrack file is added, git calculates the SHA-1 of that file content, assume the value is abc, too.
  3. If you commit at this moment, git will figure out they have the same SHA-1 value, then treat them as a rename.

Suppose you modified some tracked files and also moving them into other folder. Then, unfortunately, git can not detect them as rename/moving automatically when commit.

If you really want git to track rename/moving automatically, you need to do:

Make sure you DO NOT modify the file(s) and only rename/move file(s) in one commit.


If you want to modify file and move file at the same time, and want git to detect them as rename/moving by using TortoiseGit, then in your case:

  1. Right click on that file, and click TortoiseGit -> Rename... item of context menu.
  2. prefix new_folder\ to the filename, see: enter image description here
  3. commit

Note: you need to rename files one by one. Suppose lot of work to do. So, better rename file only.

Note2: If you modify a lot on file, git can not figure out it is a rename/moving.

Solution 2

From the TortoiseGit documentation:

If you want to move files around inside your working tree, perhaps to a different sub-folder, you can use the right-mouse drag-and-drop handler:

A. select the files or directories you want to move

B. right-drag them to the new location inside the working tree

C. release the right mouse button

D. in the popup menu select Context Menu → Git Move versioned files here

Solution 3

Git will handle this on the back end after you commit. While you are handling the uncommitted files it will initially treat the creates & deletes as separate actions, but after you commit it will then try to resolve the deletes with creates to see if it was actually just a file move. You can see this for yourself in the commit log with "old file ==> new file" messages when git finds a file move.

Alternatively, you can use git mv if it makes you feel safer https://git-scm.com/docs/git-mv

Share:
9,217

Related videos on Youtube

Dries
Author by

Dries

Updated on September 18, 2022

Comments

  • Dries
    Dries almost 2 years

    I'm trying to move some files in my repository to a new folder. I want git / TortoiseGit to recognize that this is a move instead of a delete and multiple adds.
    How can I make this work? I checked the documentation of TortoiseGit and searched Google but that didn't seem to be very useful.

    So, what I have is this structure:

    repo_root/somefile.txt
    

    What I want:

    repo_root/new_folder/somefile.txt
    

    Of course "somefile.txt" represents all files and folders that are being moved.

  • DavidPostill
    DavidPostill about 8 years
    Great. Now this is an excellent answer ;)
  • Tsahi Asher
    Tsahi Asher almost 6 years
    Not entirely accurate. Up to a certain extent, Git can detect moves even if the content has changed. I read somewhere on SO you can change up to 50% of the file and Git will still detect it as a rename, and some command line parameter can even increase this threshold. I'm not a Git expert, but that's what I know.