Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make git mark a deleted and a new file as a file move?

Tags:

git

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?

like image 435
pupeno Avatar asked Jan 11 '09 16:01

pupeno


People also ask

Does git detect moved files?

In Git's case, it will try to auto-detect renames or moves on git add or git commit ; if a file is deleted and a new file is created, and those files have a majority of lines in common, Git will automatically detect that the file was moved and git mv isn't necessary.

Is there a git move command?

We use the git mv command in git to rename and move files. We only use the command for convenience. It does not rename or move the actual file, but rather deletes the existing file and creates a new file with a new name or in another folder.

What is git add a?

In review, git add is the first command in a chain of operations that directs Git to "save" a snapshot of the current project state, into the commit history. When used on its own, git add will promote pending changes from the working directory to the staging area.


1 Answers

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.

additionally, for moves around directories, you may need to:

  1. cd to the top of that directory structure.
  2. Run git add -A .
  3. Run git status to verify that the "new file" is now a "renamed" file

If git status still shows "new file" and not "renamed" you need to follow Hank Gay’s advice and do the move and modify in two separate commits.

like image 72
Bombe Avatar answered Oct 09 '22 08:10

Bombe