简体   繁体   English

恢复.git文件,而本地存储库是更新的

[英]Recover .git file while the local repository is the updated one

A year ago I opened a Bitbucket account and used it to follow versions of a local folder (on my personal laptop). 一年前,我开设了一个Bitbucket帐户,并使用它来跟踪本地文件夹的版本(在我的个人笔记本电脑上)。

Since then my laptop "died" and I bought a new one. 从那以后,我的笔记本电脑“死了”,所以我买了一本新笔记本电脑。 My files were backed up on a Mega cloud, so they recovered successfully. 我的文件已备份到Mega云中,因此它们已成功恢复。 Though, the .git file wasn't recovered. 但是,.git文件未恢复。 Now I want to connect the updated local repository to the old outdated Bitbucket repository. 现在,我想将更新的本地存储库连接到旧的过时的Bitbucket存储库。

How can I do it without deleting the current local updates? 在不删除当前本地更新的情况下该如何做?

Appreciate your help! 感谢您的帮助!

It's odd to me that the .git directory would not have been restored in your backup. 奇怪的是,您的备份中没有还原.git目录。 From a perspective outside git, the files in this directory are no different from any other files. 从git以外的角度来看,此目录中的文件与其他任何文件都没有不同。 I would want to be sure I understood the reason for that before deciding what to do next. 在确定下一步要做什么之前,我想确定自己已经理解了这一原因。

But since you refer to the "updated local repository", I'll assume you've already taken steps to create a new git repo at the local path, regardless of what was going on before[1]. 但是由于您是指“更新的本地存储库”,所以我假设您已经采取了在本地路径上创建新的git repo的步骤,而不管以前发生了什么[1]。 In that case, you presumably have something like this: 在这种情况下,您大概会有以下内容:

On Bitbucket

A -- B -- C -- D <--(master)


Locally

E -- F <--(master)

I'm assuming that the first commit in your new repo is a newer state of the directory, compared to the last commit that was saved in BitBucket. 我假设与保存在BitBucket中的最后一次提交相比,新回购中的第一次提交是目录的较新状态。

So the first thing to do is to configure the bitbucket repository as a remote 所以要做的第一件事是将bitbucket存储库配置为远程

git remote add origin url://of/remote/repo

where the URL is the same one you would use for cloning. 该网址与您用于克隆的网址相同。 Then 然后

git fetch

and you have locally 你在本地

A -- B -- C -- D <--(origin/master)

E -- F <--(master)

Next you need to re-parent E onto D . 接下来,您需要将E re-parentD See the git filter-branch docs ( https://git-scm.com/docs/git-filter-branch ), specifically relating to the --parent-filter option. 请参阅git filter-branch文档( https://git-scm.com/docs/git-filter-branch ),特别是有关--parent-filter选项的信息。 There are several examples showing different ways to accomplish this task. 有几个示例显示了完成此任务的不同方法。


[1] If I'm misreading your statement, and you haven't yet recreated the local repo, that's actually simpler, because that means there's no local history to be grafted in. In that case, you could do something like [1]如果我误读了您的声明,而您尚未重新创建本地存储库,则实际上更简单,因为这意味着没有本地历史记录可以移植。在这种情况下,您可以执行类似的操作

cd /path/of/local/repo
git clone -n url://of/remote/repo old-repo
mv old-repo/.git .
rmdir old-repo

Then stage and commit your current state. 然后登台并提交您的当前状态。 It's a bit of a kludge, transplanting the .git folder like that; 像这样移植.git文件夹有点麻烦。 but it seems to work in my tests and I know of no reason it should fail. 但它似乎在我的测试中有效,我不知道它应该失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM