如何摆脱 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

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

How to get rid of local commits in git?

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 masterand started new branch (newfeature). On daily basis when I'm done with changes at work I push my commits from the branch newfeatureto 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 masterand then merged newfeaturebranch. Everything went grand so I pushed my new masterbranch to originand then deleted newfeaturebranch locally and on remote. Earlier today at work I checked out masterthat now contains new feature merged. When I run git statusnow it says that there is nothing to commit, but also my local branch masteris ahead of origin masterby 38 commits.

昨天我在家里完成了新功能的工作,所以我检查了我们master然后合并的newfeature分支。一切都很顺利,所以我将我的新master分支推送到origin然后newfeature在本地和远程删除了分支。今天早些时候在工作中,我检查了master现在包含合并的新功能。当我git status现在运行时,它说没有什么可提交的,但我的本地分支master也领先origin master38 个提交。

How can I get rid of any of those local commits that are ahead as I know that origin masterhas 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:

在这些情况下,您需要做的是:

  1. 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

  1. 确保您查看的是最新版本的 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 的状态,看看是否真的存在差异或只是没有正确同步

  1. In you description of workflow i would expect all your problems to be gone by just

    git checkout master

    git pull

  2. Now as you're saying that the reset --hardbrought 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

  1. 在您对工作流程的描述中,我希望您的所有问题都会消失

    git checkout master

    git pull

  2. 现在,正如您所说,这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 masterbranch 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 reflogis your friend.

如果我没有正确回答你的问题,或者你有第二个想法,请记住那git reflog是你的朋友。