如何丢弃 Git 中的本地提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3882583/
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
How to discard local commits in Git?
提问by Daniel C. Sobral
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?
我想放弃我的本地提交,而不必清除我的本地目录并重新下载所有内容。我怎样才能做到这一点?
回答by mipadi
git reset --hard origin/master
will remove all commits not in origin/master
where origin
is the repo name and master
is the name of the branch.
将删除所有未提交在origin/master
这里origin
是回购的名字和master
是分支的名称。
回答by slebetman
As an aside, apart from the answer by mipadi (which should work by the way), you should know that doing:
顺便说一句,除了 mipadi 的答案(顺便说一下),您应该知道这样做:
git branch -D master
git checkout master
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).
也完全没有你想要的having to redownload everything
(你的引述解释)。那是因为您的本地存储库包含远程存储库的副本(并且该副本与您的本地目录不同,它甚至与您签出的分支不同)。
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.
清除一个分支是完全安全的,并且重建该分支非常快并且不涉及网络流量。请记住,git 主要是设计的本地存储库。即使是远程分支在本地也有副本。只有一点元数据告诉 git 一个特定的本地副本实际上是一个远程分支。在 git 中,所有文件一直都在你的硬盘上。
If you don't have any branches other than master, you should:
如果你除了 master 之外没有任何分支,你应该:
git checkout -b 'temp'
git branch -D master
git checkout master
git branch -D temp
回答by giang nguyen
What I do is I try to reset hard to HEAD. This will wipe out all the local commits:
我所做的是尝试重新设置为 HEAD。这将清除所有本地提交:
git reset --hard HEAD^
回答by Володимир Пас?ка
You need to run
你需要跑
git fetch
To get all changes and then you will not receive message with "your branch is ahead".
要获得所有更改,然后您将不会收到“您的分支在前面”的消息。
回答by Jim Clouse
I have seen instances where the remote became out of sync and needed to be updated. If a reset --hard
or a branch -D
fail to work, try
我见过遥控器不同步并需要更新的情况。如果 areset --hard
或 abranch -D
无法工作,请尝试
git pull origin
git reset --hard
回答by ramp
I had to do a :
我不得不做一个:
git checkout -b master
as git said that it doesn't exists, because it's been wipe with the
正如 git 所说它不存在,因为它已被擦除
git -D master