Little Git Tricks: Mark Files as Moved

When you use the File Explorer (or Windows Explorer) to move files around in your repository, Git knows nothing about that action and treats your files as deleted and newly created:

 IMAGE The moved files are deleted and newly created

We can commit that change, but that makes it impossible to see the history of those files before this commit. We could prevent that by reverting our change and try to use git mv to move the files around. This works for single files and sometimes even for folders on Windows. Most often, especially when I move a whole folder up to the parent folder, I get an error.

Luckily for us, there is a much simpler way. We move the files around as we like (with the File Explorer) and then run this command in the common parent folder of all changed files:

If you now check the status of your repository, you see that Git tracks them as moved files:

Git now sees the files as moved

With the -A option for git add we can keep the history and do not need any special workflow to move files around. This little trick saved me a lot of time and I hope it will help you as well.

1 thought on “Little Git Tricks: Mark Files as Moved”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.