如何摆脱 git 中的本地提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5430657/
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
How to get rid of local commits in git?
提问by Tomasz B?achowicz
Let be honest, I ran into difficulties with my git repository. I've been working on the same project from two different machines (say at work and at home). There is also remote repository (origin
) where I keep the master copy of the code.
老实说,我的 git 存储库遇到了困难。我一直在用两台不同的机器(比如在工作和家里)处理同一个项目。还有远程存储库 ( origin
),我保存代码的主副本。
At the beginning I cloned the repository form origin master
and started new branch (newfeature
). On daily basis when I'm done with changes at work I push my commits from the branch newfeature
to origin
. The same when I work at home.
一开始我克隆了存储库表单origin master
并启动了新的分支 ( newfeature
)。每天,当我完成工作中的更改时,我会将我的提交从分支推newfeature
送到origin
. 我在家工作时也是如此。
Yesterday I finished working on new feature at home, so I checked our master
and then merged newfeature
branch. Everything went grand so I pushed my new master
branch to origin
and then deleted newfeature
branch locally and on remote. Earlier today at work I checked out master
that now contains new feature merged. When I run git status
now it says that there is nothing to commit, but also my local branch master
is ahead of origin master
by 38 commits.
昨天我在家里完成了新功能的工作,所以我检查了我们master
然后合并的newfeature
分支。一切都很顺利,所以我将我的新master
分支推送到origin
然后newfeature
在本地和远程删除了分支。今天早些时候在工作中,我检查了master
现在包含合并的新功能。当我git status
现在运行时,它说没有什么可提交的,但我的本地分支master
也领先origin master
38 个提交。
How can I get rid of any of those local commits that are ahead as I know that origin master
has the latest code?
我怎样才能摆脱我知道origin master
具有最新代码的任何本地提交?
采纳答案by Eugene Sajine
It seems to me there is some mistake in the description of your problem.
在我看来,您的问题描述中存在一些错误。
You're saying that you had some commits that you have pushed to the origin from home. Now you cam to work and you checked out master branch and it says that it is ahead of origin/master?? As i understand the origin/master should be ahead.
你是说你有一些提交,你已经从家里推到了原点。现在你开始工作,你检查了 master 分支,它说它领先于 origin/master?? 据我了解,起源/大师应该领先。
I these circumstances what you need to do is:
在这些情况下,您需要做的是:
make sure you're looking at the fully up-to-date version of you repo, meaning it has all latest info from remote
git fetch
确保您查看的是最新版本的 repo,这意味着它包含来自远程的所有最新信息
git fetch
This will fetch from all remotes configured, then you will be able to compare the state of you master branch vs origin/master and see if there are really differences or just not synchronized properly
这将从配置的所有遥控器中获取,然后您将能够比较 master 分支与 origin/master 的状态,看看是否真的存在差异或只是没有正确同步
In you description of workflow i would expect all your problems to be gone by just
git checkout master
git pull
Now as you're saying that the
reset --hard
brought you to the state that you don't like - you don't need to clone from remote again. Just do this:git reflog show master
在您对工作流程的描述中,我希望您的所有问题都会消失
git checkout master
git pull
现在,正如您所说,这
reset --hard
将您带到了您不喜欢的状态 - 您不需要再次从远程克隆。只需这样做:git reflog show master
And then reset your master back to the commit it was pointing to before the reset --hard
然后将你的主人重置回它在重置之前指向的提交 --hard
You should be on the square one.
你应该在广场上。
回答by Laurent Pireyn
You should reset your master
branch on origin/master
:
您应该在以下位置重置您的master
分支origin/master
:
git checkout master
git reset --hard origin/master
Warning:A hard reset will discard all the changes in your working copy and make it exactly like it was in origin/master
.
警告:硬重置将放弃工作副本中的所有更改,并使其与origin/master
.
If I didn't get your question right, or you have second thoughts, remember that git reflog
is your friend.
如果我没有正确回答你的问题,或者你有第二个想法,请记住那git reflog
是你的朋友。