Skip to main content
Chapter 28 of 61 · 3 sections · Page 123

Git: Renaming

Rename directories, files, local branches, and remote-tracking branches safely.

Original chapter reference: Chapter 28: Renaming. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Parameter Details -f or --force Force renaming or moving of a file even if the target exists

Section 28.1: Rename Folders

To rename a folder from oldName to newName

Source example · PDF page 123
git mv directoryToFolder/oldName directoryToFolder/newName

Followed by git commit and/or git push

If this error occurs:

fatal: renaming 'directoryToFolder/oldName' failed: Invalid argument

Use the following command:

Source example · PDF page 123
git mv directoryToFolder/oldName temp && git mv temp directoryToFolder/newName

Section 28.2: rename a local and the remote branch

the easiest way is to have the local branch checked out:

Source example · PDF page 123
git checkout old_branch

then rename the local branch, delete the old remote and set the new renamed branch as upstream:

Source example · PDF page 123
git branch -m new_branch
git push origin :old_branch
git push --set-upstream origin new_branch

Section 28.3: Renaming a local branch

You can rename branch in local repository using this command:

Source example · PDF page 123
git branch -m old_name new_name
Buy me a coffee
Daily Practice
0/0