16

When inside a git repository in Powershell or CMD, issuing

git mv * whatever

will return

fatal: bad source, source=*, destination=whatever

This works fine when using MSYS (Git Bash).

2
  • 3
    CMD and PowerShell does not expand globing characters, like bash does.
    – user364455
    Mar 9, 2016 at 16:24
  • @PetSerAl you should post this as the answer along with suggesting to use the bash shell; totally solves the problem! Thanks :)
    – Lea Hayes
    Feb 18, 2017 at 9:34

5 Answers 5

8

As has been pointed out by others in this thread, the star does not automatically expand to file names in PowerShell. Thus, the command let get-childItem needs to be used, which explicitely tells PowerShell to expand wildcard characters.

You want to put the result of get-childItem * into parentheses so that they are expanded before the entire git mv command is executed:

git mv (get-childItem *) whatever

To save a few key strokes, you might want to use the alias gci for get-childItem:

git mv (gci *) whatever
2
  • This is so clean, and ought to be the accepted answer.
    – Aankhen
    Apr 20, 2021 at 13:10
  • Use -k flag to skip commands that produce errors. For example, if you're copying items to a subdirectory that already exists, you'll get can not move directory into itself.
    – bendodge
    Oct 4, 2021 at 21:33
5

As @PetSerAI said, the Windows command prompt and PowerShell will not expand the globing characters and it makes git mv fail.

Use a bash shell like MSYS instead.

7
  • Could you please elaborate on how does Powershell interpret globbing characters?
    – LuxDie
    Mar 31, 2017 at 3:19
  • I'm not really sure, sorry :( I think it has to do with how PS works - in that it pipes objects. e.g. gist.github.com/DNNX/1481234
    – georgiosd
    Mar 31, 2017 at 5:43
  • 1
    While I agree that PowerShell does not handle this particularly well, on Windows many people live in PowerShell, and just saying "use a bash shell" to a PowerShell user is like telling a vim user to "use emacs instead." This answer below does provide a halfway-decent workaround in PowerShell. Oct 7, 2020 at 22:45
  • 1
    and why would we use what looks like bloatware (i installed git, not bash) when we have two perfectly good shells already?
    – StingyJack
    Jan 14, 2021 at 17:19
  • 1
    Also the problem is not with cmd or ps, they have been capable of wildcarding file match for like, ever. dir *.txt for example. Git just assumes its working in a *nix shell.
    – StingyJack
    Jan 14, 2021 at 17:27
3

Using PowerShell to move the content of source to whatever folder you can run this command:

Get-ChildItem .\source\ | ForEach-Object { git mv $_.FullName .\whatever\ }

Your directory structure would look like this before:

+--C:\code\Project\
|
+----+bootstrap.py
+----+requirements.txt
+----+.gitignore
+----+source
|
+---------+manage.py
+---------+modules
+---------+templates
+---------+static
+----+whatever
|
+---------+cool.py

And after running would look like this:

+--C:\code\Project\
|
+----+bootstrap.py
+----+requirements.txt
+----+.gitignore
+----+source
+----+whatever
|
+---------+cool.py
+---------+manage.py
+---------+modules
+---------+templates
+---------+static
1

Solution for cmd

for %i in (*) do git mv %i whatever/
0

This also happens when you "source" directory doesn't exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.