Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

git - 丢弃Git中的本地提交(Throw away local commits in Git)

Due to some bad cherry-picking, my local Git repository is currently five commits ahead of the origin, and not in a good state.

(由于一些糟糕的挑选,我的本地Git存储库目前在原点之前提交了五个提交,并且状态不佳。)

I want to get rid of all these commits and start over again.

(我想摆脱所有这些提交并重新开始。)

Obviously, deleting my working directory and re-cloning would do it, but downloading everything from GitHub again seems like overkill, and not a good use of my time.

(显然,删除我的工作目录并重新克隆会这样做,但是再次从GitHub下载所有内容似乎有些过分,而且不能很好地利用我的时间。)

Maybe git revert is what I need, but I don't want to end up 10 commits ahead of the origin (or even six), even if it does get the code itself back to the right state.

(也许git revert是我需要的东西,但是我不希望在原点(甚至六个)之前结束10次提交,即使它确实让代码本身恢复到正确的状态。)

I just want to pretend the last half-hour never happened.

(我只想假装最后半小时从未发生过。)

Is there a simple command that will do this?

(是否有一个简单的命令可以做到这一点?)

It seems like an obvious use case, but I'm not finding any examples of it.

(这似乎是一个明显的用例,但我没有找到它的任何例子。)


Note that this question is specifically about commits , not about:

(请注意,这个问题具体是关于提交而不是关于:)

  • untracked files

    (未跟踪的文件)

  • unstaged changes

    (未分期的变化)

  • staged, but uncommitted changes

    (上演但未提交的变更)

  ask by David Moles translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If your excess commits are only visible to you, you can just do git reset --hard origin/<branch_name> to move back to where the origin is.

(如果您的超额提交仅对您可见,您可以执行git reset --hard origin/<branch_name>以返回原点所在的位置。)

This will reset the state of the repository to the previous commit, and it will discard all local changes.

(这会将存储库的状态重置为先前的提交,并将丢弃所有本地更改。)

Doing a git revert makes new commits to remove old commits in a way that keeps everyone's history sane.

(做一个git revert做了一个新的提交,以一种让每个人的历史保持清醒的方式删除旧的提交。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...