git command to move a folder inside another
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:
-
git add include
-
git mv common/ include/
but it fails with this error
fatal: bad source, source=myrepo/common, destination=myrepo/include
-
I tried
git mv common/ include/common
but I get the same error
Any idea how to achieve this?
Solution 1:
One of the nicest things about git is that you don't need to track file renames explicitly. Git will figure it out by comparing the contents of the files.
So, in your case, don't work so hard: Ref: Git mv docs
$ mkdir include
$ git mv common include
$ git rm -r common
$ git add include/common
Running git status
should show you something like this:
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: common/file.txt -> include/common/file.txt
#
Solution 2:
git mv common include
should work.
From the git mv
man page:
git mv [-f] [-n] [-k] <source> ... <destination directory>
In the second form, the last argument has to be an existing directory; the given sources will be moved into this directory.
The index is updated after successful completion, but the change must still be committed.
No "git add
" should be done before the move.
Note: "git mv A B/
", when B
does not exist as a directory, should error out, but it didn't.
See commit c57f628 by Matthieu Moy (moy
) for Git 1.9/2.0 (Q1 2014):
Git used to trim the trailing slash, and make the command equivalent to '
git mv file no-such-dir
', which created the fileno-such-dir
(while the trailing slash explicitly stated that it could only be a directory).This patch skips the trailing slash removal for the destination path.
The path with its trailing slash is passed to rename(2), which errors out with the appropriate message:
$ git mv file no-such-dir/
fatal: renaming 'file' failed: Not a directory
Solution 3:
Command:
$ git mv oldFolderName newFolderName
It usually works fine.
Error "bad source ..." typically indicates that after last commit there were some renames in the source directory and hence git mv
cannot find the expected file.
The solution is simple - just commit before applying git mv
.