git stash `无本地更改`,但 git status`提前 3 次提交`

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32461214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:21:40  来源:igfitidea点击:

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

git

提问by Phil

I did a git commit -amfollowed immediately by git stashand got the message
No local changes to save

git commit -am立即做了一个跟进git stash并收到了消息
No local changes to save

When I run git statusI get
Your branch is ahead of 'origin/master' by 3 commits.

当我跑步时,git status我得到
Your branch is ahead of 'origin/master' by 3 commits.

Is this right?

这是正确的吗?

I was working on some stuff and made some commits but haven't pushed the changes. Now I want to 'stash' them and go back to a clean version (my latest pushedchanges - don't know how to refer to this)

我正在做一些事情并进行了一些提交,但没有推动更改。现在我想“隐藏”它们并返回一个干净的版本(我的最新pushed更改 - 不知道如何引用)

How do I stash my work that I haven't pushed yet and revert to the latest pushed master branch?

我如何隐藏我尚未推送的工作并恢复到最新推送的主分支?

采纳答案by Phil

Thanks for the help everyone. What I did was
git reset --soft origin/master git stash save 'stashing my unfinished changes'

谢谢大家的帮助。我所做的是
git reset --soft origin/master git stash save 'stashing my unfinished changes'

This undid all my commits but left all my changed files. I was then able to stash the changes since they weren't committed yet. It automatically reset my local files to origin/master after I stashed.

这取消了我所有的提交,但留下了我所有更改的文件。然后我能够隐藏更改,因为它们尚未提交。在我藏匿后,它会自动将我的本地文件重置为 origin/master。

回答by neshkeev

If I understand if right, you need:

如果我理解正确,您需要:

1) Save your local changes.

1) 保存您的本地更改。

git add -A
git stash

2) Backup your commits

2)备份你的提交

git branch my_master_backup

3) Reset your HEAD back to origin/master

3) 将你的 HEAD 重置回原点/主

git reset --hard origin/master

4) Do some work that you want to to with the "clean version"

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

5) Restore changes that you have made with your commits. Fix conflicts if any.

5) 恢复您对提交所做的更改。如果有冲突,请修复。

git merge my_master_backup

6) Drop the backup branch. If you can't delete it means that you haven't fully merged your backup branch and the master.

6) 删除备份分支。如果您不能删除,则表示您还没有完全合并您的备份分支和主分支。

git branch -d my_master_backup

6) Restore your local changes from the step 1:

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

git stash pop

PS: you can avoid creating a backup branch and use reflog. When you want to restore your commits you need to execute git merge HEAD@{X}where X is the number of the desired commit from reflog.

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

回答by Thomas Stringer

From your question and the comments, it sounds like what you want to do is go backto the remote repository's master branch commit, but retain the changes you have made since you have done this.

从您的问题和评论来看,听起来您想要做的是返回远程存储库的主分支提交,但保留自您完成此操作以来所做的更改

What you can do is just create a new local branch where the remote (assuming it is called origin) branch is (assuming you are working with master).

您可以做的只是创建一个新的本地分支,其中远程(假设它被称为origin)分支(假设您正在使用master)。

$ git checkout -b original-master origin/master

This will create a new branch called original-master(and check out this branch, bringing your working directory back to where origin/masteris pointing to) that will be at the same commit as the remote origin's masterbranch.

这将创建一个名为新的分支original-master(并检查了这个分支,使您的工作目录回哪里origin/master指向),这将是在同一个commit为远程originmaster分支。

At this point, the 3 commits you have from origin/masterto (local) masterwill be preserved, but you can start a different branch of development with a parent of origin/master(by having a new branch called original-master).

此时,您从origin/masterto(本地)的 3 次提交master将被保留,但您可以使用 的父级origin/master(通过创建一个名为 的新分支original-master)开始一个不同的开发分支。