8

We use GIT and Visual Studio 2013 (Ultimate + Professional versions). The code for the solution and projects is in GIT and anytime we rename the file in Visual Studio, GIT sees them as

Before rename

c:\temp\testCodeSnippets>git status
# On branch master
nothing to commit (working directory clean)

After rename (from Visual Studio)

c:\temp\testCodeSnippets>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:    OldName.cs
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       NewName.cs
no changes added to commit (use "git add" and/or "git commit -a")

Renaming via git mv

c:\temp\testCodeSnippets>git mv OldName.cs NewName.cs

c:\temp\testCodeSnippets>git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       renamed:    OldName.cs -> NewName.cs
#

Basically, the renames performed in VS still need some manual work. Equally important; a lot of these renames happen when refactoring from inside the IDE i.e. a class/member/etc gets renamed and all references get changes. If the namespace or classname changes, the files get renamed.

So, is there any setting to toggle, any tool to install, any deity to appease so that renames from Visual Studio get mapped as renames to GIT too (like git mv does to make it smooth)?

DeepSpace101
  • 13,110
  • 9
  • 77
  • 127
  • 2
    AFAIK add+remove is equivalent to rename/move in git. It automatically figures out what you did. It's one of its big advantages over svn, which needs to track renames explicitly. – CodesInChaos Feb 10 '14 at 09:30
  • possible duplicate of [Getting Git to follow renamed and edited files](http://stackoverflow.com/questions/12785160/getting-git-to-follow-renamed-and-edited-files) – Edward Thomson Feb 10 '14 at 16:23
  • Visual Studio uses the same heuristics as git to detect renames. Are you seeing that `git status` reports these items as renames but Visual Studio does not? Can you show the files and some screen shots that provide more details? – Edward Thomson Feb 13 '14 at 04:48
  • @EdwardThomson: We're not using VS for git status reporting. We use git (bash) or Tortoise Git to interact with git but for development/refactoring etc. – DeepSpace101 Feb 13 '14 at 04:53
  • 1
    I see; as others have indicated, there is not a "rename" in Git, instead it looks at the similarity between an added and deleted file and suggests that a rename occurred in `git status` or `git diff`. Visual Studio does not change this behavior, it simply hasn't staged the changes. – Edward Thomson Feb 13 '14 at 05:15

1 Answers1

10

Core git (the command-line application) only identifies renames by detecting them between files that are staged for addition and files that are staged for deletion.

Visual Studio, however, does not stage changes you make during operation. Instead, it will stage the changes at commit time. There are several reasons for this, but primary is that staging a change places the file in the object database and doing this for each save would be incredibly costly.

You will need to manually stage the changes using git add and git rm and your renames will be reported as it always will, by comparing the added changes to the renamed changes.

(Note that Visual Studio - since it doesn't stage your changes as you go - examines both staged and unstaged files for rename detection. So Visual Studio will report renames before core git does. However, they should show the same results in status when you stage those changes.)

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187