git command to move a folder inside another

Viewed 227610

I have created a folder common with a bunch of source files and folders.

Now I want to move the common folder into the include folder so it looks like include/common

I tried these:

  1. git add include

  2. git mv common/ include/

    but it fails with this error

    fatal: bad source, source=myrepo/common, destination=myrepo/include

  3. I tried git mv common/ include/common but I get the same error

Any idea how to achieve this?

15 Answers

You can use this script.

# git mv a folder and sub folders in windows 

function Move-GitFolder {
    param (
        $target,
        $destination
    )
    
    Get-ChildItem $target -recurse |
    Where-Object { ! $_.PSIsContainer } |
    ForEach-Object { 
        $fullTargetFolder = [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $target))
        $fullDestinationFolder = [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $destination))
        $fileDestination = $_.Directory.FullName.Replace($fullTargetFolder.TrimEnd('\'), $fullDestinationFolder.TrimEnd('\'))

        New-Item -ItemType Directory -Force -Path $fileDestination | Out-Null

        $filePath = Join-Path $fileDestination $_.Name

        git mv $_.FullName $filePath
        
    }
}

Usage

Move-GitFolder <Target folder> <Destination folder>

The advantage of this solution over other solutions is that it moves folders and files recursively and even creates the folder structure if it doesn't exist.

Move your files and use git stage

You can move your files using your favorite tools (command line, graphical file explorer) and git will figure out that you made move instead of deletion and creation once you staged your operations, as suggested by @Andres but if you make too many modifications, as Erik Kaplun pointed out, explicit git mv could help.

Stage is not used in some applications

Some GUI git tools like GitHub Desktop does not provide direct access to stage: you can select and deselect modifications, but it does not modify the underlying git stage. You need to use a more powerful git tool, like the command-line tool or the authentic git GUI to stage the changes, then GitHub Desktop too will show the operation as rename.

I had similar problem, but in folder which I wanted to move I had files which I was not tracking.

let's say I had files

a/file1
a/untracked1
b/file2
b/untracked2

And I wanted to move only tracked files to subfolder subdir, so the goal was:

subdir/a/file1
subdir/a/untracked1
subdir/b/file2
subdir/b/untracked2

what I had done was:

  • I created new folder and moved all files that I was interested in moving: mkdir tmpdir && mv a b tmpdir
  • checked out old files git checkout a b
  • created new dir and moved clean folders (without untracked files) to new subdir: mkdir subdir && mv a b subdir
  • added all files from subdir (so Git could add only tracked previously files - it was somekind of git add --update with directory change trick): git add subdir (normally this would add even untracked files - this would require creating .gitignore file)
  • git status shows now only moved files
  • moved rest of files from tmpdir to subdir: mv tmpdir/* subdir
  • git status looks like we executed git mv :)

Another important note that I have missed, and fixed the "Bad Source" Error instantly:

(Just thought I will share my mistake just in case someone encounters it.. :))

Make sure the the correct path is selected in the git Console when you run the command:

- git mv Source Destination

If needed, use:

- cd SourceFolder

And then the mv command.

If you are using GitLab, its integrated Web-IDE, can be used for some file management tasks including move and rename ( without local clone )

Click on Web-IDE on the repository main window.

enter image description here

Right click on the folder and select Rename/Move ( for example moving src to foo/bar/src). The changes will be shown in the web-ide.

Remember to commit!

enter image description here

With Git Tortoise it is very easy.

Right drag source to destination ( select source, hold the right mouse key, drag to destination, release mouse ) .

A menu will be displayed which has Git Move versioned items and Git Move and rename versioned items.

Of course the git repo should be cloned locally

Git move with git tortoise

I solved this on windows by doing this:

  • Open Power shell console
  • run dir
  • Alt-click and drag over the file/folder name column, then copy
  • Paste to notepad++
  • run replace with regex: replace (.*) with git mv ".\\\1" ".\\<New_Folder_Here>\"
  • copy all text from notepad++ into powershell
  • hit enter

I resolved this by using this command mv <parent_folder/folder_to_move> <new_folder> while in the working directory of <new_folder>. What this command does is rename the <folder_to_move> into the <new_folder> name. After which you can rename the target folder(folder_to_move) by using mv <new_folder>(formerly <folder_to_move>) <new_name_of_folder>

mv <new_folder> <new_name_of_folder>

Related