【问题标题】:when I committed my local changes to a new branch, pushed that branch, then switched branches, and switched back, all my file changes were lost当我将本地更改提交到新分支,推送该分支,然后切换分支并切换回来时,我所有的文件更改都丢失了
【发布时间】:2020-06-08 19:13:38
【问题描述】:

我的工作流程如下。我有一个只有一个分支(Master)的远程仓库。

在我的本地仓库中,我有一个文件。我编辑了它。然后我使用git checkout -b newbranchname 创建了一个新分支。我将我的文件添加到这个新分支以进行提交。然后我提交了这个文件。然后我使用git push -u origin newbranchname 推送了这个文件。我去了我的远程仓库,你瞧,我的新分支和我的文件可用于拉取请求。

然后,在我的本地仓库中,我决定使用git checkout master 回到我的主分支。然后我添加、提交和推送了一个不同的文件到我的远程主分支。然后,我使用git checkout newbranchname 回到我创建的分支。当我打开我推送的第一个文件时,我所有的编辑都消失了。在我的远程仓库中,我可以看到我推送和拉取的所有更改。但是,我不明白为什么我所有的本地更改都消失了。有人可以解释一下吗?

编辑:添加来自git reflog 的输出以提供更多上下文。 'zoom' 也是我创建的新分支:

`brendathompson@Brendas-Air Coding % git reflog
71a7da2 (HEAD -> master) HEAD@{0}: checkout: moving from master to master
71a7da2 (HEAD -> master) HEAD@{1}: commit: setting up site queries
cab9374 HEAD@{2}: checkout: moving from zoom to master
db8374a (origin/zoom, zoom) HEAD@{3}: commit: added zoom to map
cab9374 HEAD@{4}: checkout: moving from master to zoom
cab9374 HEAD@{5}: commit: added modal popup feature
0b51d5c HEAD@{6}: commit: added alert popup, deleted unecessary extra shape
8a0654f HEAD@{7}: commit: deleted unecessary background image code
a73475c HEAD@{8}: pull origin master: Merge made by the 'recursive' strategy.
4d9eb53 HEAD@{9}: commit: changed height and width in past map css
acbb2a7 HEAD@{10}: pull origin master: Merge made by the 'recursive' strategy.
8f5c8c0 HEAD@{11}: commit: edited height and width of image map
f02c46c HEAD@{12}: checkout: moving from IMAGE-MAP-EDITS to master
dd534f9 (origin/IMAGE-MAP-EDITS, IMAGE-MAP-EDITS) HEAD@{13}: commit: added javascript to make map responsive successfully
2f5a2af HEAD@{14}: checkout: moving from master to IMAGE-MAP-EDITS
f02c46c HEAD@{15}: checkout: moving from IMAGE-MAP-EDITS to master
2f5a2af HEAD@{16}: commit: added a more detailed title
92559fe HEAD@{17}: pull origin master: Fast-forward
a2e05cc HEAD@{18}: commit: changed title of image map
f02c46c HEAD@{19}: checkout: moving from master to IMAGE-MAP-EDITS
f02c46c HEAD@{20}: pull origin master: Merge made by the 'recursive' strategy.
6552161 HEAD@{21}: commit: removed unecessary lines from present_map.html`

编辑#2:好的,所以当我在新分支上提交更改时我没有看到我的文件看起来像什么的原因可能是当我尝试切换到新分支时,我实际上从未切换到新分支分支,因为我收到此错误: brendathompson@Brendas-Air Coding % git checkout zoom error: Your local changes to the following files would be overwritten by checkout: present_map.html Please commit your changes or stash them before you switch branches. Aborting 但是,我确实在新分支(缩放)上提交了对文件的更改,因为我可以在远程仓库中看到更改...

编辑#3:为了响应之前的错误消息,我提交了对引发错误的 html 文件的更改。但是,当我尝试结帐时,我仍然遇到同样的错误。

编辑#4:其他用户评论说我在此处描述的工作流程与 git reflog 中显示的工作流程不匹配。首先,我会注意到我在问题中列出的命令之后做了一些额外的事情(例如,我试图通过从远程仓库中提取来解决问题),因此这些额外的命令不包含在我的问题中列出的命令中问题。其次,我在这里列出了命令在终端中的确切显示方式——因为在发布问题之前我根本没有清除终端,所以我能够准确地列出我的命令是如何编写它们的。所以,我不确定为什么 git reflog 不能反映我的工作流程。我现在不记得了,因为这是大约 24 小时前,但也许这是因为我关闭了终端,再次打开它,做了一些事情,然后调用了 reflog(我不知道 reflog 是如何工作的,我也可以'不记得这是否是我实际所做的)。

编辑#5:要访问分支,我在master 分支上使用了git stash save file,然后使用了git checkout newbranchname。这使我能够成功访问分支和我的文件。

【问题讨论】:

  • 请添加更多信息以及您所做的示例...也许您还可以提供git reflog的输出?
  • 文件应该在那里......但如果你提交了,请检查 reflog git reflog
  • 冷冷地看看分支中的修订及其关系......类似于git log --oneline --graph --all --decoration

标签: git github


【解决方案1】:

当您将本地更改推送到新分支时,这意味着您已经保存了它们,因此您必须从界面 GitHub 或(GitLab 用户的合并请求)发出拉取请求,然后在主分支中合并新分支分支

请在这里查看我的答案Git: Merge local branch to remote branches A and B without inserting code from A into B/ B into A

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 2016-12-18
    • 2022-11-02
    • 2020-12-13
    • 2020-12-27
    • 2022-11-28
    • 2023-01-25
    • 2013-12-09
    • 1970-01-01
    相关资源
    最近更新 更多