如何在 git 中移动多个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2212857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:47:35  来源:igfitidea点击:

How do you move multiple files in git?

git

提问by garageàtrois

I try:

我尝试:

git mv a.py b.py src/

git mv a.py b.py src/

and get

并得到

fatal: multiple sources for the same target, source=b.py, destination=src/b.py

fatal: multiple sources for the same target, source=b.py, destination=src/b.py

Using the -n flag, like so git mv -n a.py b.py src/gives me:

使用 -n 标志,就像这样git mv -n a.py b.py src/给我:

Checking rename of 'a.py' to 'src/b.py'
Checking rename of 'b.py' to 'src/b.py'
fatal: multiple sources for the same target, source=b.py, destination=src/b.py

Am I doing something really stupid? I'm using git version 1.6.6.1

我真的在做一些愚蠢的事情吗?我正在使用 git 版本 1.6.6.1

采纳答案by CB Bailey

This has been fixed in the current master branch of git, it's in v1.7.0-rc0 but not in a release build yet.

这已在当前的 git 主分支中修复,它在 v1.7.0-rc0 中,但尚未在发布版本中。

http://git.kernel.org/?p=git/git.git;a=commit;h=af82559b435aa2a18f38a4f47a93729c8dc543d3

http://git.kernel.org/?p=git​​/git.git;a=commit;h=af82559b435aa2a18f38a4f47a93729c8dc543d3

In the mean time the simplest thing to do is to either git mvthe files individually or to just use mvand then update the index manually, e.g. with git add -Aif you have appropriate .gitignorepatterns.

同时,最简单的做法是git mv单独使用文件或仅使用mv然后手动更新索引,例如,git add -A如果您有适当的.gitignore模式。

回答by gaspard

I use bash loop:

我使用 bash 循环:

for FILE in src/*.h; do git mv $FILE include/; done

回答by Josh Lee

As long as there aren't any other changes in the working directory, the simplest way is just to move them yourself and then use git add -A. Behold:

只要工作目录中没有任何其他更改,最简单的方法就是自己移动它们,然后使用git add -A. 看:

$ ls
a.py b.py
$ mkdir src
$ mv *.py src
$ git status
# Changed but not updated:
#       deleted:    a.py
#       deleted:    b.py
# Untracked files:
#       src/
$ git add -A
$ git status
# Changes to be committed:
#       renamed:    a.py -> src/a.py
#       renamed:    b.py -> src/b.py

回答by Eusebio Rufian-Zilbermann

In Windows (posh~git):

在 Windows 中(豪华~git):

foreach ($file in get-childitem *.py) { git mv $file.name ./src }

回答by wich

Works for me:

对我有用:

$ git --version
git version 1.6.4.1
$ mkdir foo
$ cd foo
$ git init
Initialized empty Git repository in /home/wich/foo
$ touch a b c d
$ git add a b c d
$ git commit -m "foobar"
[master (root-commit) 11051bd] foo
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
 create mode 100644 b
 create mode 100644 c
 create mode 100644 d
$ mkdir bar
$ git mv a b c d bar
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       renamed:    a -> bar/a
#       renamed:    b -> bar/b
#       renamed:    c -> bar/c
#       renamed:    d -> bar/d
#