如何丢弃 Git 中的本地提交?

发布于 2024-09-26 08:29:40 字数 306 浏览 7 评论 0原文

我一直在做一些事情,在做了一些事情之后,我觉得它完全搞砸了。所以我尝试了以下顺序:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

慵挽 2024-10-03 08:29:40
git reset --hard origin/master

将删除不在 origin/master 中的所有提交,其中 origin 是存储库名称,master 是分支的名称。

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.

﹎☆浅夏丿初晴 2024-10-03 08:29:40

顺便说一句,除了 mipadi 的答案(顺便说一下,它应该可以工作)之外,您应该知道这样做:

git branch -D master
git checkout master

也可以完全按照您想要的方式进行操作,而无需不必重新下载所有内容(您的引用已转述)。这是因为您的本地存储库包含远程存储库的副本(并且该副本与您的本地目录不同,甚至与您签出的分支不同)。

清除分支是完全安全的,并且重建该分支非常快并且不涉及网络流量。请记住,git 在设计上主要是一个本地存储库。即使是远程分支在本地也有一份副本。只有一点元数据告诉 git 特定的本地副本实际上是远程分支。在 git 中,所有文件始终都在您的硬盘上。

如果您除了 master 之外没有任何分支,您应该:

git checkout -b 'temp'
git branch -D master
git checkout master
git branch -D temp

As an aside, apart from the answer by mipadi (which should work by the way), you should know that doing:

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

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:

git checkout -b 'temp'
git branch -D master
git checkout master
git branch -D temp
美人骨 2024-10-03 08:29:40

我所做的就是尝试将硬盘重置为 HEAD。这将清除所有本地提交:

git reset --hard HEAD^

What I do is I try to reset hard to HEAD. This will wipe out all the local commits:

git reset --hard HEAD^
在风中等你 2024-10-03 08:29:40

我见过遥控器不同步并需要更新的情况。如果 reset --hardbranch -D 无法工作,请尝试

git pull origin
git reset --hard 

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

git pull origin
git reset --hard 
魔法唧唧 2024-10-03 08:29:40

您需要运行

git fetch

以获取所有更改,然后您将不会收到“您的分支领先”的消息。

You need to run

git fetch

To get all changes and then you will not receive message with "your branch is ahead".

橘寄 2024-10-03 08:29:40

我必须做一个 :

git checkout -b master

正如 git 所说它不存在,因为它已被擦除

git -D master

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 -D master
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文