git stash `No local changes` 但 git status `ahead of origin/master by 3 commits`

标签 git

我执行了 git commit -am 之后立即执行了 git stash 并收到了消息
没有要保存的本地更改

当我运行 git status 时,我得到了
您的分支比“origin/master”领先 3 次提交。

这样对吗?

我正在处理一些事情并做出了一些 promise ,但还没有推送更改。现在我想“stash ”它们并返回到一个干净的版本(我最新的 pushed 更改 - 不知道如何引用它)

如何 stash 我尚未推送的工作并恢复到最新推送的 master 分支?

最佳答案

如果我理解正确的话,你需要:

1) 保存您的本地更改。

git add -A
git stash

2) 备份你的提交

git branch my_master_backup

3) 将你的 HEAD 重置回 origin/master

git reset --hard origin/master

4) 用“干净版”做一些你想做的工作

5) 恢复您对提交所做的更改。解决冲突(如果有)。

git merge my_master_backup

6) 删除备份分支。如果你不能删除,说明你还没有完全 merge 你的备份分支和主分支。

git branch -d my_master_backup

6) 从步骤 1 恢复本地更改:

git stash pop

PS:你可以避免创建备份分支并使用reflog .当你想恢复你的提交时,你需要执行 git merge HEAD@{X},其中 X 是来自 reflog 的所需提交的数量。

关于git stash `No local changes` 但 git status `ahead of origin/master by 3 commits`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32461214/

相关文章:

Git 不在 Mac 上 Homebrew 的 PATH 中

git - 在强制推送分支的 'lost' 提交上创建一个分支

git - 取消初始化 git 仓库

Git checkout to a commit 2 commits before hash

git - 当用作 git difftool 时,使 meld 显示用户友好的标题

Git 克隆因内存不足错误而失败 - "fatal: out of memory, malloc failed (tried to allocate 905574791 bytes)/fatal: index-pack failed"

git - git diff HEAD^ HEAD 的写法是否更短?

Git gc 显示错误 "Could not read commit"

git - 为什么 git merge 有时会创建一个提交,有时不会?

eclipse - EGit 推送操作给出错误 : "Error occurred during unpacking the remote end"