Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-mv

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
like image 699
Lernkurve Avatar asked Aug 27 '15 11:08

Lernkurve


People also ask

What is git mv?

git-mv - Move or rename a file, a directory, or a symlink.

Should I use git mv?

Git move or mv helps us to rename or move files within a git repository without deleting its history. If we move or rename a file within a git repository then it thinks that a file is deleted and a new file is added.


2 Answers

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.

like image 94
axelduch Avatar answered Nov 15 '22 11:11

axelduch


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.

like image 41
Tobia Tesan Avatar answered Nov 15 '22 12:11

Tobia Tesan