In a Git repository, how to properly rename a directory?

In a Git repository, how to properly rename a directory? I think it should work to copy the directory to be renamed to a new directory with desired name, and delete the old directory, and git add, git commit and push everything. But is this the best way?

  • Git repository within Git repository
  • Troubleshooting misplaced .git directory (nothing to commit)
  • Git config with directory scope, containing multiple repositories
  • How to track directories but not their files with Git?
  • git status - is there a way to show changes only in a specific directory?
  • SVN merge just a directory add from a checkin (without directory contents)
  • git branch without history
  • Stopping a git gc --aggressive, is that a bad thing?
  • Ansible git module not checking out a branch
  • How to extract a subset of history from Git?
  • Using maven-release-plugin with git-1.8.5
  • Running a batch file in git shell
  • 7 Solutions collect form web for “In a Git repository, how to properly rename a directory?”

    Basic rename (or move):

    git mv <old name> <new name>

    Case sensitive rename—eg. from casesensitive to CaseSensitive—you must use a two step:

    git mv casesensitive tmp
    git mv tmp CaseSensitive

    (More about case sensitivity in Git…)

    …followed by commit and push would be the simplest way to rename a directory in a git repo.

    If you receive this error: fatal: renaming ‘foldername’ failed: Invalid argument

    Try this:

    git mv foldername tempname && git mv tempname folderName

    1. Change a folder’s name from oldfolder to newfolder

    git mv oldfolder newfolder

    2. If newfolder is already in your repository & you’d like to override it and use:- force

    git mv -f oldfolder newfolder

    Don’t forget to add the changes to index & commit them after renaming with git mv.

    3. Renaming foldername to folderName on case insensitive file systems

    Simple renaming with a normal mv command(not git mv) won’t get recognized as a filechange from git. If you try it with the ‘git mv’ command like in the following line

    git mv foldername folderName

    If you’re using a case insensitive filesystem, e.g. you’re on a Mac and you didn’t configure it to be case sensitive, you’ll experience an error message like this one:

    fatal: renaming ‘foldername’ failed: Invalid argument

    And here is what you can do in order to make it work:-

    git mv foldername tempname && git mv tempname folderName

    This splits up the renaming process by renaming the folder at first to a completely different foldername. After renaming it to the different foldername the folder can finally be renamed to the new folderName. After those ‘git mv’s, again, do not forget to add and commit the changes. Though this is probably not a beautiful technique, it works perfectly fine. The filesystem will still not recognize a change of the letter cases, but git does due to renaming it to a new foldername, and that’s all we wanted 🙂

    You can rename the directory using the file system. Then you can do git rm <old directory> and git add <new directory> (Help page). Then you can commit and push.

    Git will detect that the contents are the same and that it’s just a rename operation, and it’ll appear as a rename entry in the history. You can check that this is the case before the commit using git status

    From Web Application I think you can’t, but you can rename all the folders in Git Client, it will move your files in the new renamed folders, than commit and push to remote repository.

    I had a very similar issue:
    I had to rename different folders from uppercase to lowercase (like Abc -> abc), I’ve renamed all the folders with a dummy name (like ‘abc___’) and than committed to remote repository, after that I renamed all the folders to the original name with the lowercase (like abc) and it took them!

    git rm -rf --cached path/to/your/directories

    Then re-add and commit.

    Simply rename the folder. git is a “content-tracker”, so the SHA1 hashes are the same and git knows, that you rename it. The only thing that changes is the tree-object.

    rm <directory>
    git add .
    git commit
    Git Baby is a git and github fan, let's start git clone.