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.

7

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.