简体   繁体   English

本地 git 远程仓库丢失主分支

[英]local git remote repo lost master branch

After looking through the SO answers, I cannot find anything that comes close to what I think my problem is right now.在查看了 SO 答案后,我找不到任何与我认为我现在的问题相近的东西。

Being fairly new to git, I started a new project by following some of the newby recommendations.作为 git 的新手,我按照一些 newby 的建议开始了一个新项目。

My setup: a local NAS has the original master git repo, started by adding code from a directory with code not under git control.我的设置:本地 NAS 具有原始的主 git 存储库,首先从代码不受 git 控制的目录中添加代码。 Using TortoiseGit (TG) I added the base code and kept modifying, compiling and committing to the local repo and then pushing to the NAS repo.使用 TortoiseGit (TG) 我添加了基本代码并不断修改、编译和提交到本地 repo,然后推送到 NAS repo。

Initially, all went the way all of the tutorials show, but at one point I wanted to create a branch to test code meant to resolve a specific issue, which kept going for some time and became the de facto master branch.最初,一切都按照教程所示的方式进行,但有一次我想创建一个分支来测试旨在解决特定问题的代码,该分支持续了一段时间并成为事实上的主分支。 Somewhere along the line I wanted to merge this back into the master, but I made some bad choices, it seems, and now neither my supposed master nor my local repo any longer has any master branch and only one branch named Branch_new.在某个地方,我想将它合并回 master,但似乎我做出了一些错误的选择,现在我假定的 master 和我的本地 repo 都不再有任何 master 分支,只有一个名为 Branch_new 的分支。

While in theory I assume I could just carry on on this mislabeled branch, I would prefer to get back to a more conventional setup.虽然理论上我认为我可以继续这个贴错标签的分支,但我更愿意回到更传统的设置。

At this stage it is not very important that I keep all of the history and I could simply start over by pretending that the current code is my initial base, but as part of learning a bit more about the git way of doing things, I would prefer to learn how to extricate myself from the mess things are in.在这个阶段,保留所有历史记录并不是很重要,我可以简单地通过假装当前代码是我的初始基础重新开始,但作为学习更多关于 git 做事方式的一部分,我会更喜欢学习如何从混乱的事情中解脱出来。

TIA TIA

First of all, not everything is lost.首先,并非一切都丢失了。 By default, git cleans up commits which are not referenced by any branch only after two weeks (see git help gc ).默认情况下,git 仅在两周后清理未被任何分支引用的提交(参见git help gc )。 If two weeks have not passed, then you might recover your work.如果两周还没有过去,那么您可能会恢复您的工作。
Now getting to find these commits might get a bit tricky.现在找到这些提交可能会有点棘手。 There are different methods to achieve this, I like the reflog best.有不同的方法可以实现这一点,我最喜欢 reflog。 You can access it by either git reflog command, or looking at the raw file under .git/logs/HEAD , which will show you all the places (commits) that you've been at, and the command that made git switch to that commit or branch.您可以通过git reflog命令或查看.git/logs/HEAD下的原始文件来访问它,这将显示您去过的所有位置(提交),以及使 git 切换到该位置的命令提交或分支。
Once you find your commit, recreating a branch from it (to save it from garbage collection) is as simple as git branch <branch_name> <commit> .找到提交后,从中重新创建一个分支(以将其从垃圾收集中保存)就像git branch <branch_name> <commit>一样简单。 You can verify the history beforehand by git log <commit> , as all commits point to their ancestor, so you'll have the complete history up to that point.您可以通过git log <commit>预先验证历史记录,因为所有提交都指向它们的祖先,因此您将拥有到目前为止的完整历史记录。

As for what caused the mess you encountered, I cannot be sure without the reflog and the history of all your commands, starting from initializing the repository.至于是什么导致您遇到的混乱,如果没有 reflog 和所有命令的历史记录,我无法确定,从初始化存储库开始。 I did find, however, that most problems with new remotes happen due to the remote not being a bare repository (which sounds like the case from what you describe).但是,我确实发现,新遥控器的大多数问题都是由于遥控器不是裸存储库而发生的(这听起来像您所描述的情况)。 Remotes that you push to need to be bare, ie not have a working directory, but only the git metadata (whatever a regular clone will usually have under .git/ ).您推送的遥控器需要是裸露的,即没有工作目录,而只有 git 元数据(无论常规克隆通常在.git/下有什么)。 That's because pushing to a repo which has a branch checked out does make a mess.那是因为推送到已签出分支的仓库确实会造成混乱。
When initializing a repo that you want to act as a remote (so you can push to it), you should make it bare.当初始化一个你想作为远程的仓库时(所以你可以推送它),你应该让它裸露。 Read a bit more on git init --bare or git clone --bare , these should get you started setting up a remote correctly.阅读更多关于git init --baregit clone --bare的内容,这些应该让您开始正确设置遥控器。
Note that even if a repo is not bare, you can still clone from it.请注意,即使回购不是裸露的,您仍然可以从中克隆。 This causes most of the confusion around pushing to new repos.这导致了围绕推送到新存储库的大部分混乱。

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

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