开发者

How to get rid of local commits in git?

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.开发者_如何学Go

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.

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.

How can I get rid of any of those local commits that are ahead as I know that origin master has the latest code?


You should reset your master branch on 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.

If I didn't get your question right, or you have second thoughts, remember that git reflog is your friend.


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.

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

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

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

And then reset your master back to the commit it was pointing to before the reset --hard

You should be on the square one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜