Git checkout删除了最新的提交。

huangapple go评论64阅读模式
英文:

Git checkout erased newest commit

问题

Here's the translated content from your message:

最近,我对我的代码进行了大量更改,并使用以下命令进行了提交。

git add .
git commit -m "示例消息"
git push origin development

当它无法工作时,它显示无法找到名为development的分支。我认为这是因为它没有正确的凭据,所以我执行了gh auth login并重新进行了身份验证。
然后,我意识到我仍然在将代码推送到主分支,所以我使用以下命令切换到开发分支。

git checkout development

我认为这是切换分支的正确命令,但我不确定。问题是,不知何故,当我重复执行第一个代码块后,我对代码所做的所有更改都消失了。为什么一切都回到了旧版本,我如何才能恢复新代码?

尽管我在Git方面经验不足,但我相信由于我实际上没有删除任何文件,应该有某个地方有备份,只是不知道如何访问它。重新编写我的所有代码将非常烦人。

我已经尝试执行以下命令

git log --oneline

并且它给了我这个(消息已更改)。

3e3caf2 (HEAD -> development, origin/development) 后端函数获取数据
1fcd9d4 添加了匹配
66b9527 (origin/dev, dev) 删除了缓存
14b5cf7 初始提交
7e7a826 初始提交

尽管我提交了更改,最新的提交只显示了我的代码还原到的分支。我该怎么做才能恢复我的最新提交?

英文:

Recently I made a ton of changes to my code and committed using the following commands.

git add .
git commit -m "example message"
git push origin development

When it didn't work, it said it was because it couldn't find a branch named development. I though this was because it didn't have the right credentials, so I did gh auth login and re-authenticated.
I then realized I was still pushing to the main branch, and I so I switched to development using the following.

git checkout development

I'm under the impression that this is the correct command to switch branches, but I'm not sure. The problem is that somehow, after I repeated the first code block, all the changes I made to my code disappeared. Why did everything just revert back to an old version and how can I get the new code back?

Although I am very inexperienced in Git, I'm pretty that since I didn't actually delete any files, there should be a backup somewhere, I'm just not sure how to access it. Writing all my code again would extremely annoying.

I already tried to do the following command


git log --oneline

And it gave me this (messages changed).

3e3caf2 (HEAD -> development, origin/development) backend function to get data
1fcd9d4 added match
66b9527 (origin/dev, dev) removed cache
14b5cf7 initial commit
7e7a826 initial commit

Even though I committed my changes, the newest commit only shows the branch my code was reverted to. What can I do to get my newest commit back?

答案1

得分: 1

根据我理解你的问题,以及topsail在他的回答中提到的,你的更改应该存在于主分支上。

你可以通过运行以下命令来检查:

git log --oneline main

然后你将会得到你在主分支上添加的那个提交的提交哈希值,然后你只需在你的开发分支上运行cherry-pick来选择那个提交的哈希值。所以,当你在开发分支上检出时,可以执行以下命令:

git cherry-pick {commit_hash}

然后按照你的意图推送开发分支。

英文:

As far as I understand your question, and as topsail mentioned in his answer, your changes should be present on the main branch

The way you can check on it is by running

git log --oneline main

And you would get the commit hash from that commit you added on main branch, you can then just run a cherry-pick of that commit hash to your development branch. So, you would do this command while you're checked out on the development branch:

git cherry-pick {commit_hash}

And then, push the development branch as you intended.

huangapple
  • 本文由 发表于 2023年5月22日 08:38:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定