如何丢弃 Git 中的本地提交?
我一直在做一些事情,在做了一些事情之后,我觉得它完全搞砸了。所以我尝试了以下顺序:
git reset --hard
git rebase origin
git fetch
git pull
git checkout
此时我收到消息
Your branch is ahead of 'origin/master' by 2 commits.
我想放弃我的本地提交,而不必清除我的本地目录并重新下载所有内容。我怎样才能做到这一点?
I'd been working on something, and decided it was completely screwed...after having committed some of it. So I tried the following sequence:
git reset --hard
git rebase origin
git fetch
git pull
git checkout
At which point I got the message
Your branch is ahead of 'origin/master' by 2 commits.
I want to discard my local commits, without having to wipe out my local directory and redownload everything. How can I accomplish that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将删除不在
origin/master
中的所有提交,其中origin
是存储库名称,master
是分支的名称。will remove all commits not in
origin/master
whereorigin
is the repo name andmaster
is the name of the branch.顺便说一句,除了 mipadi 的答案(顺便说一下,它应该可以工作)之外,您应该知道这样做:
也可以完全按照您想要的方式进行操作,而无需
不必重新下载所有内容
(您的引用已转述)。这是因为您的本地存储库包含远程存储库的副本(并且该副本与您的本地目录不同,甚至与您签出的分支不同)。清除分支是完全安全的,并且重建该分支非常快并且不涉及网络流量。请记住,git 在设计上主要是一个本地存储库。即使是远程分支在本地也有一份副本。只有一点元数据告诉 git 特定的本地副本实际上是远程分支。在 git 中,所有文件始终都在您的硬盘上。
如果您除了 master 之外没有任何分支,您应该:
As an aside, apart from the answer by mipadi (which should work by the way), you should know that doing:
also does exactly what you want without
having to redownload everything
(your quote paraphrased). That is because your local repo contains a copy of the remote repo (and that copy is not the same as your local directory, it is not even the same as your checked out branch).Wiping out a branch is perfectly safe and reconstructing that branch is very fast and involves no network traffic. Remember, git is primarily a local repo by design. Even remote branches have a copy on the local. There's only a bit of metadata that tells git that a specific local copy is actually a remote branch. In git, all files are on your hard disk all the time.
If you don't have any branches other than master, you should:
我所做的就是尝试将硬盘重置为 HEAD。这将清除所有本地提交:
What I do is I try to reset hard to HEAD. This will wipe out all the local commits:
我见过遥控器不同步并需要更新的情况。如果
reset --hard
或branch -D
无法工作,请尝试I have seen instances where the remote became out of sync and needed to be updated. If a
reset --hard
or abranch -D
fail to work, try您需要运行
以获取所有更改,然后您将不会收到“您的分支领先”的消息。
You need to run
To get all changes and then you will not receive message with "your branch is ahead".
我必须做一个 :
正如 git 所说它不存在,因为它已被擦除
I had to do a :
as git said that it doesn't exists, because it's been wipe with the