Skip to content

Git - Move Operation

Git - Moving or Renaming Files and Directories

Section titled “Git - Moving or Renaming Files and Directories”

Git provides the git mv command to move or rename files and directories within your repository. Using git mv is important because it tells Git that a file has been moved or renamed, rather than simply deleted and a new, unrelated file added. This helps Git track the history of the file across renames.

Using git mv old_name new_name is equivalent to running these three commands:

  1. mv old_name new_name (the standard system move/rename command)
  2. git rm old_name (stages the removal of the old name)
  3. git add new_name (stages the addition of the new name)

git mv does all this in one step and correctly records the rename.

Suppose Tom decides to move a source file string_utils.c into a newly created src/ directory. The current project structure might be:

[tom@dev-machine project]$ pwd
/home/tom/project
[tom@dev-machine project]$ ls
README.md string_utils.c Makefile

Tom first creates the src directory, then uses git mv:

[tom@dev-machine project]$ mkdir src
[tom@dev-machine project]$ git mv string_utils.c src/string_utils.c
[tom@dev-machine project]$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: string_utils.c -> src/string_utils.c

Git status shows that string_utils.c has been renamed (moved). The change is already staged.

If Tom wanted to rename README.md to README.txt:

[tom@dev-machine project]$ git mv README.md README.txt
[tom@dev-machine project]$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README.txt

To make these changes permanent in the repository’s history, Tom needs to commit them:

[tom@dev-machine project]$ git commit -m "Organized source files into src/ directory and renamed README"
[main 7d9ea97] Organized source files into src/ directory and renamed README
2 files changed, 0 insertions(+), 0 deletions(-)
rename string_utils.c => src/string_utils.c (100%)
rename README.md => README.txt (100%)

After committing, Tom can push these changes to the remote repository so other developers (like Jerry) will see the new structure when they pull:

[tom@dev-machine project]$ git push origin main

When Jerry pulls the changes, his local repository will reflect the new file locations and names.

[jerry@dev-machine project]$ git pull origin main
remote: Enumerating objects: ..., done.
...
From github.com:your-repo/project
e86f062..7d9ea97 main -> origin/main
Updating e86f062..7d9ea97
Fast-forward
string_utils.c | 0
README.md => README.txt | 0
{string_utils.c => src/string_utils.c} | 0
3 files changed, 0 insertions(+), 0 deletions(-)
rename string_utils.c => src/string_utils.c (100%)
rename README.md => README.txt (100%)
create mode 100644 src/string_utils.c
delete mode 100644 string_utils.c
[jerry@dev-machine project]$ ls
README.txt src/ Makefile
[jerry@dev-machine project]$ ls src/
string_utils.c