我的 Git 存储库位于错误的根目录中。我可以移动它吗?(../ 代替 。/)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1918111/
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:31:31  来源:igfitidea点击:

My Git repository is in the wrong root directory. Can I move it? (../ instead of ./)

git

提问by Mike

Somehow when I git inited my latest project a month or so ago I ran the command in the directory one directory higher than the root of my project.

不知何故,当我git init一个月前编辑我的最新项目时,我在比我的项目根目录高一个目录的目录中运行了命令。

So my repository is in the ./projectdirectory and not the ./project/my-new-projectdirectory. I don't know how I didn't realize the issue earlier, but I just never looked for the .git directory until now.

所以我的存储库在./project目录中而不是./project/my-new-project目录中。我不知道我之前怎么没有意识到这个问题,但直到现在我才找到 .git 目录。

Is there a way, without killing my project, to move the repository to the proper directory and then tell git what the new base of the project is? Just moving the directory doesn't work. Git thinks all files have been deleted.

有没有办法在不终止我的项目的情况下将存储库移动到正确的目录,然后告诉 git 项目的新基础是什么?只是移动目录是行不通的。Git 认为所有文件都已删除。

采纳答案by T.E.D.

Probably the simplest thing, unless you have already created some history you want to save, would be to just delete the .gitsubdirectory and redo the init in the correct directory.

可能最简单的事情是,除非您已经创建了一些要保存的历史记录,否则只需删除.git子目录并在正确的目录中重做 init。

If you used git to solve the problem, any solution would necessarily leave behind a lot of "moved this file here" history entries that aren't actually changes, but you fixing a screwup at creation time. Better to just create it right.

如果您使用 git 来解决问题,那么任何解决方案都必然会留下许多“将这个文件移到此处”的历史记录条目,这些条目实际上并未发生变化,但您在创建时修复了一个错误。更好地创建它。

回答by Abhishek Anand

I had the opposite problem - had to shift the git root to the parent directory (from project/src to project) To my extreme surprise, the following worked!!

我遇到了相反的问题 - 必须将 git root 转移到父目录(从 project/src 到 project) 令我非常惊讶的是,以下工作!

src$ mv .git ../ 
src$ cd ..
project$ git add src
project$ git commit -a

git cleverly detected that all the new files were renamed versions of old ones and no history was lost

git聪明地检测到所有新文件都是旧文件的重命名版本,没有丢失任何历史记录

You can try something similar... move the .git folder and add the files again before committing

您可以尝试类似的操作...在提交之前移动 .git 文件夹并再次添加文件

回答by holmesal

This worked for me, and kept all my history intact. From the incorrect root folder (the parent where you accidentally initialized the repo):

这对我有用,并保持了我所有的历史完整。从不正确的根文件夹(您不小心初始化 repo 的父文件夹):

Move the folder:

移动文件夹:

mv .git thecorrectfolder/

Re-initialize the git repo:

重新初始化 git 仓库:

cd thecorrectfolder/
git init

Re-add all the files, commit, and push:

重新添加所有文件,提交并推送:

git add .
git commit -am 'fixing things'
git push origin master

Done! Get yourself a beer.

完毕!给自己喝杯啤酒。

When you commit the git repo after re-initializing, you'll get a bunch of output that looks like this:

当你在重新初始化后提交 git repo 时,你会得到一堆看起来像这样的输出:

rename {ethanode/coffee => coffee}/app.coffee (100%)

In other words, all of your references from the parent folder and being renamed to use the correct folder.

换句话说,来自父文件夹的所有引用都被重命名为使用正确的文件夹。

回答by ndim

git filter-branchlets you rewrite history in that way. The git filter-branchman pageeven has your case as an example:

git filter-branch让您以这种方式改写历史。该git filter-branch手册页甚至有你的情况作为例子

To rewrite the repository to look as if foodir/ had been its project root, and discard all other history:

git filter-branch --subdirectory-filter foodir -- --all

重写存储库以使其看起来好像 foodir/ 是它的项目根目录,并丢弃所有其他历史记录:

git filter-branch --subdirectory-filter foodir -- --all

You probably want to git clonethe repo into a new subdirectory before (or after?) the git filter-branchrun. (Cloning before filter-branch and running the filter-branch on the new clone would have the advantage of leaving the original .git/dir in place as a backup in case something goes wrong.)

您可能希望git clonegit filter-branch运行之前(或之后?)将 repo 放入一个新的子目录中。(在 filter-branch 之前克隆并在新克隆上运行 filter-branch 的优点是将原始.git/目录留在原处作为备份,以防出现问题。)

回答by M. Reza Nasirloo

Git can remember files with their hashes,

Git 可以记住带有哈希值的文件,

Just move your .gitto root directory and tell gitto remember all files changes with --alloption.

只需将您移动.git到根目录并告诉git记住所有文件更改--all选项。

$ mv .git ../
$ cd ..
$ git add . --all 
$ git status // => you can see all the files recognized as renamed 100%
$ git commit -m "Moves repo to root directory."

回答by jkndrkn

Use git-mvto move your files "up" to the proper location, then git-rmthe "my-new-project" directory.

使用git-mv移动文件“上升”到适当的位置,然后git-rm在“我的新项目”目录中。

回答by Steve Barnes

Having just gone through this same problem my eventual solution was:

刚刚经历了同样的问题,我最终的解决方案是:

  1. Move the .git folder to where it needed to be.
  2. Change directory to the folder that I have just moved .git to
  3. Reset the folder contents to what git thinks that it should be: git reset --hard HEAD
  4. Check that the contents match what they should be with kdiff3 or another compare tool
  5. Delete the now un-versioned files at the old location.
  1. 将 .git 文件夹移动到它需要的位置。
  2. 将目录更改为我刚刚将 .git 移动到的文件夹
  3. 将文件夹内容重置为 git 认为应该是的内容: git reset --hard HEAD
  4. 检查内容是否与 kdiff3 或其他比较工具匹配
  5. 删除旧位置现在未版本化的文件。

It worked like magic with no impact on the history. - N.B.If you have made some changes be sure to commit them before the move of the .git directory.

它像魔术一样运作,对历史没有影响。-注意,如果您进行了一些更改,请确保在移动 .git 目录之前提交它们。

回答by user1565849

I came here looking for a way to move my repository anywhere.

我来这里是为了寻找一种将我的存储库移动到任何地方的方法

In case, I was not the only one, here's what I did in the end:

以防万一,我不是唯一一个,这是我最后所做的:

https://git.wiki.kernel.org/index.php/GitFaq#How_do_I_clone_a_repository_with_all_remotely_tracked_branches.3FI "git clone --mirror"d a bare copy of my repo and afterwards told it to not be bare anymore, so the files showed up in that new folder. (Then checked whether or not files and log had appeared.) Kept the old repo for a while, just in case...

https://git.wiki.kernel.org/index.php/GitFaq#How_do_I_clone_a_repository_with_all_remotely_tracked_branches.3F我“git clone --mirror”我的repo的裸副本然后告诉它不再是裸的,所以文件出现了在那个新文件夹中。(然后检查文件和日志是否已经出现。)将旧仓库保留一段时间,以防万一......

That way I was able to move my repo without losing history.

这样我就可以在不丢失历史记录的情况下移动我的回购。

Best Greetings, Dinah

最好的问候,黛娜

回答by V. Rance

There are two ways here:

这里有两种方法:

  1. cd TheWrongDirectory rm -rf .git

  2. just DELETE the .git folder and cd to the right directory.

  1. cd TheWrongDirectory rm -rf .git

  2. 只需删除 .git 文件夹和 cd 到正确的目录。