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?
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With