5

I am wondering what the difference in Git would be if I used the mv command versus moving it in Windows explorer. From Perforce I know it's important because the version system keep track of the file origin. I am wondering if this is just as important in Git or is it just the same as moving files in the file system?

The reason I am asking is because we are moving a lot of files around, and using the command line is pretty tedious.

Michael Durrant
  • 13,101
  • 5
  • 34
  • 60
Serguei Fedorov
  • 191
  • 1
  • 2
  • 7

2 Answers2

8

If you move the file in Windows Explorer, you still have to git add the new file location and git rm (or git add --all) the old file location. Once those two things are done, git will work out on its own that the new file is mostly identical to the old file, and will automatically display it as a move in commands like git status.

The benefit of git mv is that it combines the "Explorer move", git add and git rm all into one operation. When you're moving a single file, I see no reason not to use mv.

When I need to move or rename entire folders, I use Windows Explorer because that sort of thing is much easier in a GUI. When I'm done shuffling things around, I can tell git to process all the changes I made with a single git add --all . command from the top directory.

Ixrec
  • 27,621
  • 15
  • 80
  • 87
  • mv foo/bar/baz . is so much harder. :-P – Gort the Robot Apr 17 '15 at 23:55
  • If move commands are complicated enough, you can try http://stackoverflow.com/questions/20629302/better-way-to-rename-files-based-on-multiple-patterns/25597051#25597051. I also have written a [git filter renaming files using a sed script](https://github.com/michipili/anvil/blob/master/doc/AnvilRevisionHistory.md#rewrite-and-rename-files-using-sed-scripts). – Michaël Le Barbier Apr 18 '15 at 02:41
1

Whether its window or unix, git mv basically combines three actions:

  • a file system move
  • a git delete of the original file
  • a git add of the new file.

So, without git rm, one might do (this is Unix/OSX but the steps are similar in windows unless using cygwin terminal emulator and then u probably can):

$ mv newfile movedfile
$ 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:    newfile

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

        movedfile

no changes added to commit (use "git add" and/or "git commit -a")
$ git rm newfile
rm 'newfile'
$ git add movedfile
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    newfile -> movedfile

$ git commit -m"renamed"
[master 46332e4] renamed
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename newfile => movedfile (100%)
$ git status
On branch master
nothing to commit, working directory clean

whereas with git rm one can just do:

$ git mv newfile2 renamed_newfile2
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    newfile2 -> renamed_newfile2

$ git commit -m"renamed"
[master 8d1e296] renamed
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename newfile2 => renamed_newfile2 (100%)
$ git status
On branch master
nothing to commit, working directory clean
Michael Durrant
  • 13,101
  • 5
  • 34
  • 60