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).

LuxDie
  • 261
  • 1
  • 2
  • 7

5 Answers5

9

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
René Nyffenegger
  • 2,249
  • 7
  • 33
  • 46
  • This is so clean, and ought to be the accepted answer. – Aankhen Apr 20 '21 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 04 '21 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.

georgiosd
  • 159
  • 1
  • 5
  • Could you please elaborate on how does Powershell interpret globbing characters? – LuxDie Mar 31 '17 at 03:19
  • I'm not really sure, sorry :( I think it has to do with how PS works - in that it pipes objects. e.g. https://gist.github.com/DNNX/1481234 – georgiosd Mar 31 '17 at 05: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](https://superuser.com/a/1591487/413591) does provide a halfway-decent workaround in PowerShell. – Russ Frizzell-Carlton Oct 07 '20 at 22:45
  • @RussFrizzell-Carlton "many people" don't have git installed. It comes with bash shell by default on Windows. – georgiosd Oct 09 '20 at 07:19
  • 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 '21 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 '21 at 17:27
  • This is factually correct but not a solution. – Aankhen Apr 20 '21 at 13:12
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
Nick
  • 141
  • 4
1

Solution for cmd

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

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

Adam Rabung
  • 151
  • 1
  • 4