Is there a difference between git mv and the MS-DOS move?
Asked Answered
B

2

6

Question

Does it make a difference if I move Git tracked files with the git mv command as opposed to just moving the files with MS-DOS move or the Windows Explorer?

Back in the Subversion days, it was necessary to use for example the TortoiseSVN SVN Move versioned files here command to keep the history intact.

I would have expected it to work the same way in Git, but a test (see example below) showed that Git detects by itself that the file has been moved and that the history is kept intact.

So why use git mv at all?

Example

C:\test>git init
C:\test>mkdir folder
C:\test>cd folder
C:\test\folder>echo "1" > file.txt
C:\test\folder>git add .
C:\test\folder>git commit -m "Initial commit"
C:\test\folder>echo "2" >> file.txt
C:\test\folder>git add .
C:\test\folder>git commit -m "Update file.txt"
C:\test\folder>move file.txt ..
C:\test\folder>cd ..

C:\test>git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    folder/file.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file.txt

no changes added to commit (use "git add" and/or "git commit -a")

C:\test>git add -A
C:\test>git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    folder/file.txt -> file.txt

C:\test>git commit -m "Moved file.txt with the move command"

The entire history has been retained despite not using git mv and Git says that is has detected a renaming.

C:\test>git log --oneline --follow file.txt
6bd3c05 Moved file.txt with the move command
5b55aea Update file.txt
5b9b255 Initial commit
Bonnes answered 27/8, 2015 at 11:30 Comment(0)
A
4

git mv does the staging for you as well, so that you don't need to git rm olfdfile and git add newfile after physically moving the file via mv/MOVE/explorer.exe.

This is on purpose.

Air answered 27/8, 2015 at 11:33 Comment(0)
W
6

When you use git mv it will index the modifications for you

If you do it manually with your OS, you will need to do

git add -A <previous-path-to-file-that-was-moved>
git add <new-path-to-file>

So that git understands you actually renamed the file.

Warmedover answered 27/8, 2015 at 11:34 Comment(1)
If you simply do git add . after a manual move, will this work? I'm asking for auto-generated html files through a static generator like pelican which reside on the gh-pages branch. Since several new files are added/removed on each build, you can't be expected to separately run a git command for each file?Rinna
A
4

git mv does the staging for you as well, so that you don't need to git rm olfdfile and git add newfile after physically moving the file via mv/MOVE/explorer.exe.

This is on purpose.

Air answered 27/8, 2015 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.