Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical use case of 'git rm' and 'git mv' with git?

Tags:

git

git-rm

git-mv

I know that git provides the 'git rm' and 'git mv' to remove/move files or directories. But, I can't see the practical use case for this.

Normally, I just mv or rm the files or whatever in the command line, and after I'm done with all the necessary actions, I can just run 'git add -u' and 'git add .', as I asked and got answers in here.

Am I missing something? Are there any cases that only 'git rm' and 'git mv' capable of?

like image 410
prosseek Avatar asked Sep 03 '10 20:09

prosseek


People also ask

What is the use of git mv?

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 rm used for?

The git rm command can be used to remove individual files or a collection of files. The primary function of git rm is to remove tracked files from the Git index. Additionally, git rm can be used to remove files from both the staging index and the working directory.

Is git mv necessary?

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.

What is the difference between rm and git rm?

Git rm vs rmThe git rm command removes the file from both the git repository and the local file system. The rm command, on the other hand, only removes the file from the file system.


2 Answers

git mv and git rm are about updating the index directly. While directly moving or deleting files in the working tree won't immediately affect the index.

The GitFaq does present git mv as:

just a convenience. The effect is indistinguishable from removing the file and adding another with different name and the same content.

like image 116
VonC Avatar answered Oct 22 '22 19:10

VonC


git rm and git mv allows you to be more specific/granular about what you're doing. If you've deleted two directories and want to split up those deletions between two commits, you can use git rm to remove only one directory from the staging area, rather than getting them both via git -u

like image 44
bdukes Avatar answered Oct 22 '22 21:10

bdukes