开发者

Undoing a git push from a different computer

Ok, so I work on a project from home and work using 2 compute开发者_运维技巧rs with a remote repo.

When I got home today, my home computer was 2 days behind my work computer and I did a

git push origin master

accidentally which has screwed up the remote repo (newly cloned code is now broken).

I've seen the accepted answer from this question where he had a reference to the last good code.

My question is, how can I undo my changes if the last good copy that I want is at work and I'm at home? Can't I just tell the remote server to undo the last change pushed?


No, you cannot tell the origin repo to "undo" the last change pushed remotely. You could SSH in and do a git reset --hard [revision] if you have access, though. However…

If you didn't --force the push, Git didn't overwrite anything on the origin repository. In fact, if you pushed and git didn't refuse it, that means you were up-to-date with what was in the origin repo. Assuming you pushed from your work computer to origin, then you pushed distinct changes from your home computer to origin and they were accepted, that would lead me to believe you committed them on different branches.

In order to tell you what to do to get out of this situation, you need to determine what the situation is with branches and whatnot. Once you figure it out, you can "undo" the last change by getting your local repo in the condition you want origin to be in (using git reset --hard [revision]) then you can do a git push --force to get that state to be reflected in the origin repo.

If you have other people working with the origin repo, though, you're going to want to simply fix the problem, not overwrite it. To do that, you have to merge the changes you want into your local repo, then commit them as a new change and push.


You can use git reflog show origin/master to show the where the origin/master branch was before the push. There is probably a way to get more details (like the date and time of each push/fetch that changed it) from the git reflog command, but I don't know what they are. So you can just do cat .git/logs/refs/remotes/origin/master and it should allow you to figure out which commit you're trying to go back to. More recent changes are at the bottom.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜