KKAIROS
Practical guideIllustrative3 min read

Git Mv: rename a tracked file

git mv OLD NEW renames the file and stages the rename in one step. Tested here: git mv renames a file and the rename is staged.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-80-A: git mv renames a file and the rename is staged

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > old.txt; git add old.txt; git commit -qm one

Command:

git mv old.txt new.txt; ls; git status --porcelain

Recorded output:

new.txt
R  old.txt -> new.txt

Exit status: 0. The output matches the expected text written before the run.

TE-80-B: git mv into an existing directory keeps the file name and stages the rename

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > old.txt; git add old.txt; git commit -qm one
mkdir sub

Command:

git mv old.txt sub/; git status --porcelain

Recorded output:

R  old.txt -> sub/old.txt

Exit status: 0. This status and the output match what was written before the run.

TE-80-C: expected non-zero exit: git mv of a path that does not exist stops with a fatal error

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one

Command:

git mv nosuch.txt x.txt

Recorded output: none, standard output was empty.

Standard error from the same run:

fatal: bad source, source=nosuch.txt, destination=x.txt

Exit status: 128. This status and the output match what was written before the run.

TE-80-B: by the setup, sub is an existing directory. git mv old.txt sub/ kept the file name, and the status line reads R old.txt -> sub/old.txt. TE-80-C: git mv nosuch.txt x.txt exited with status 128, and standard error reads fatal: bad source, source=nosuch.txt, destination=x.txt.

Limits

The run shows the status view only. Not tested on this page: -v (verbose output), -n (dry run), -f (force the move or rename even when the target exists), -k (skip move and rename errors) and --sparse (allow updating entries outside the sparse-checkout cone). These option names and descriptions come from the usage text printed by git mv -h in git 2.34.1. No run here used any of them.

Ad

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-80-A | git mv renames a file and the rename is staged | 0 | PASS | 0ad2a1c76572 | | TE-80-B | git mv into an existing directory keeps the file name and stages the rename | 0 | PASS | a8f16c44d3c8 | | TE-80-C | expected non-zero exit: git mv of a path that does not exist stops with a fatal error | 128 | PASS | b75950e4567f |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides