我有一个git存储库,其结构如下所示:
+--repo.git
|
+----+bootstrap.py
+----+buildout.cfg
+----+.gitignore
+----+webapp
|
+---------+manage.py
+---------+modules
+---------+templates
+---------+static
+---------+...
+---------+...
Run Code Online (Sandbox Code Playgroud)
我想将webapp文件夹的内容向上移动一级.我的结果回购应该是这样的:
+--repo.git
|
+----+bootstrap.py
+----+buildout.cfg
+----+.gitignore
+----+manage.py
+----+modules
+----+templates
+----+static
+----+...
+----+...
Run Code Online (Sandbox Code Playgroud)
我可以通过简单地将webapp目录的所有文件移动一级,删除空webapp目录然后提交更改来完成此操作吗?这会保留webapp目录下文件的提交历史吗?
虽然对很多人来说这是一个非常简单的问题,但我想确定一下.我想要的最后一件事就是喝汤.
我尝试移动文件,但我丢失了提交历史记录,因为git没有真正处理移动或重命名.我知道即使它在日志中显示为新文件,仍然可以使用一些选项查看文件的提交历史记录git log.
根据我的阅读,实现这一目标的最佳方法是使用git-filter.我对shell或git不是很好,所以有人可以告诉我我需要执行上述操作.
Sum*_*eek 54
刚刚这样做,实际上非常简单.正确的方法是 -
git mv repo.git/webapp/* repo.git/.
Run Code Online (Sandbox Code Playgroud)
然后
git rm repo.git/webapp
Run Code Online (Sandbox Code Playgroud)
接下来是
git add *
git commit -m "Folders moved out of webapp directory :-)"
Run Code Online (Sandbox Code Playgroud)
Ale*_*lex 23
Sumeet的另一个变体 - 在"webapp"上面的存储库目录中执行以下命令:
git mv webapp/* ./ -k
Run Code Online (Sandbox Code Playgroud)
-k - 包含尚未受版本控制的文件,否则您将获得:
fatal: not under version control, source=webapp/somefile, destination=somefile
Run Code Online (Sandbox Code Playgroud)
JLR*_*she 11
通过从目标文件夹执行此操作,我能够使其正常工作:
git mv webapp/* .
Run Code Online (Sandbox Code Playgroud)
似乎这在 Windows shell 中不起作用(失败并出现错误Bad source),但如果您使用 Git Bash shell,它会在 Windows 中工作,它扩展了*通配符。
在 Windows 中,您可以执行以下操作:
当您在子文件夹中时
for /f %f in ('dir /b') do git mv %f ../
Run Code Online (Sandbox Code Playgroud)
结果子文件夹中的所有对象都将在父文件夹中
请注意:当您在子文件夹中有名称等于子文件夹的对象时,可能会出现一些错误
您提到的解决方案应该有效,因为 git 首先根据文件的哈希值跟踪更改,然后是它们的位置。
如果作为移动文件的一部分,您更改了文件的内容,这将不起作用。
底部情况,尝试一下,如果它不起作用,您可以在将更改推送到主存储库之前还原更改:)。这也是我非常喜欢 git 的原因之一。
我忘了提到要查看重命名后的更改,您需要使用“--follow”参数。检查这个例子
首先,我创建了一个新的 git repo
94:workspace augusto$ mkdir gittest
94:workspace augusto$ cd gittest/
94:gittest augusto$ git init
Initialized empty Git repository in /Volumes/Data/dev/workspace/gittest/.git/
Run Code Online (Sandbox Code Playgroud)
然后在文件夹/测试中创建一个文件
94:gittest augusto$ mkdir folder
94:gittest augusto$ vi folder/test
94:gittest augusto$ git add folder/test
94:gittest augusto$ git commit -am "added file"
[master (root-commit) 7128f82] added file
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 folder/test
Run Code Online (Sandbox Code Playgroud)
然后将文件移动到新文件夹/测试
94:gittest augusto$ mkdir newfolder
94:gittest augusto$ mv folder/test newfolder/
94:gittest augusto$ git add newfolder/test
94:gittest augusto$ git commit -am "moved/renamed file"
[master 4da41f5] moved/renamed file
1 files changed, 0 insertions(+), 0 deletions(-)
rename {folder => newfolder}/test (100%)
Run Code Online (Sandbox Code Playgroud)
并git log --follow newfolder/test显示完整的历史记录(我添加了参数 -p 以显示更多信息,例如路径)。
94:gittest augusto$ git log --follow -p newfolder/test
commit 4da41f5868ab12146e11820d9813e5a2ac29a591
Author: Augusto Rodriguez <xxxx@gmail.com>
Date: Sat Aug 20 18:20:37 2011 +0100
moved/renamed file
diff --git a/folder/test b/newfolder/test
similarity index 100%
rename from folder/test
rename to newfolder/test
commit 7128f8232be45fd76616f88d7e164a840e1917d5
Author: Augusto Rodriguez <xxxx@gmail.com>
Date: Sat Aug 20 18:19:58 2011 +0100
added file
diff --git a/folder/test b/folder/test
new file mode 100644
index 0000000..3b2aed8
--- /dev/null
+++ b/folder/test
@@ -0,0 +1 @@
+this is a new file
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助!
如果您使用 PowerShell,您可以从项目根目录运行此命令,它将把 webapp 的内容放在那里。
Get-ChildItem .\webapp\ | ForEach-Object { git mv $_.FullName .\ }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40767 次 |
| 最近记录: |