I've moved a file manually and then I've modified it. According to Git, it is a new file and a removed file. Is there any way to force Git into treating it as a file move?
Git will automatically detect the move/rename if your modification is not too severe. Just git add
the new file, and git rm
the old file. git status
will then show whether it has detected the rename. If it hasn’t you need to follow Hank Gay’s advice and make two separate commits.
Its all a perceptual thing. Git is generally rather good at recognising moves, because GIT is a content tracker
All that really depends is how your "stat" displays it. The only difference here is the -M flag.
git log --stat -M
commit 9c034a76d394352134ee2f4ede8a209ebec96288
Author: Kent Fredric
Date: Fri Jan 9 22:13:51 2009 +1300
Category Restructure
lib/Gentoo/Repository.pm | 10 +++++-----
lib/Gentoo/{ => Repository}/Base.pm | 2 +-
lib/Gentoo/{ => Repository}/Category.pm | 12 ++++++------
lib/Gentoo/{ => Repository}/Package.pm | 10 +++++-----
lib/Gentoo/{ => Repository}/Types.pm | 10 +++++-----
5 files changed, 22 insertions(+), 22 deletions(-)
git log --stat
commit 9c034a76d394352134ee2f4ede8a209ebec96288
Author: Kent Fredric
Date: Fri Jan 9 22:13:51 2009 +1300
Category Restructure
lib/Gentoo/Base.pm | 36 ------------------------
lib/Gentoo/Category.pm | 51 ----------------------------------
lib/Gentoo/Package.pm | 41 ---------------------------
lib/Gentoo/Repository.pm | 10 +++---
lib/Gentoo/Repository/Base.pm | 36 ++++++++++++++++++++++++
lib/Gentoo/Repository/Category.pm | 51 ++++++++++++++++++++++++++++++++++
lib/Gentoo/Repository/Package.pm | 41 +++++++++++++++++++++++++++
lib/Gentoo/Repository/Types.pm | 55 +++++++++++++++++++++++++++++++++++++
lib/Gentoo/Types.pm | 55 -------------------------------------
9 files changed, 188 insertions(+), 188 deletions(-)
git help log
-M
Detect renames.
-C
Detect copies as well as renames. See also --find-copies-harder.
git diff -M or git log -M should automatically detect such changes as a rename with minor changes as long as they indeed are. If your minor changes are not minor, you can reduce the similarity threashold, e.g.
$ git log -M20 -p --stat
to reduce it from the default 50% to 20%.
This is a quick solution if you've renamed a file, made some changes to it, Git doesn't realize it's a rename, and you haven't committed your changes. Let's say the file was named 'blah' and now it's named 'foo'.
1) Rename 'foo' to a temp name:
% mv foo foo.tmp
2) Checkout 'blah':
% git checkout blah
3) Rename 'blah' to 'foo' so that Git knows about it:
% git mv blah foo
4) Now rename 'foo.tmp' back to 'foo'.
% mv foo.tmp foo
This last step is what gets your changed content back into 'foo'.
Hope that makes sense.