如何放弃对分支所做的所有更改?
我正在一个分支(即 design
)工作,并且我做了一些更改,但我需要将它们全部丢弃并重置它以匹配存储库版本。我以为 git checkout design 可以做到这一点,但它只是告诉我我已经在分支 design 中并且我有 3 个修改过的文件。
我如何放弃这些更改并获取远程服务器上现在的分支?
I'm working in a branch (i.e. design
) and I've made a number of changes, but I need to discard them all and reset it to match the repository version. I thought git checkout design
would do it, but it just tells me I'm already in branch design
and that I have 3 modified files.
How would I discard those changes and get the branch as it stands now on the remote server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
注意:您无法撤消此操作。
尝试 git checkout -f ,这将丢弃所有未在所有分支和主分支中提交的本地更改。
Note: You CANNOT UNDO this.
Try
git checkout -f
this will discard any local changes which are not committed in ALL branches and master.如果您想丢弃自上次提交以来的所有内容, git reset --hard 可以帮助您
git reset --hard can help you if you want to throw away everything since your last commit
如果您不希望对
设计
进行任何更改,并且确实希望它仅匹配远程分支,您也可以删除该分支并重新创建它:If you don't want any changes in
design
and definitely want it to just match a remote's branch, you can also just delete the branch and recreate it:当您想放弃本地分支中的更改时,可以使用 git stash 命令存储这些更改。
git stash save "some_name"
您的更改将被保存,您可以稍后检索这些更改(如果您愿意)或者您可以将其删除。
执行此操作后,您的分支将不会有任何未提交的代码,您可以使用 git pull 从主分支中提取最新代码。
When you want to discard changes in your local branch, you can stash these changes using git stash command.
git stash save "some_name"
Your changes will be saved and you can retrieve those later,if you want or you can delete it.
After doing this, your branch will not have any uncommitted code and you can pull the latest code from your main branch using git pull.
在源根目录中:
<代码>
git reset ./ HEAD <--取消暂存任何暂存的更改
git checkout ./ <--放弃任何未暂存的更改
In the source root:
git reset ./ HEAD <--un-stage any staged changes
git checkout ./ <--discard any unstaged changes
@Will,git immersion 是一个非常好的、简单的 git 教程。它将向您展示如何撤消以下情况的更改:未暂存、暂存和已提交。实验室 14-18
@Will, git immersion is a really nice and simple git tutorial. it will show you how to undo changes for the following cases: unstaged, staged and committed. labs 14-18
放弃所有更改的可逆方法:
我在进行合并后发现了这个问题,并且忘记立即签出开发。您猜对了:我开始直接在 master 上修改一些文件。噢!由于我的情况并不独特(我们都做过,不是吗;->),我将提供一种可逆的方法,我曾经放弃所有更改以获得master 看起来又开发了。
在执行 git diff 来查看修改了哪些文件并评估错误范围后,我执行了:
首先存储所有更改后,接下来清除它们。对 master 错误的文件所做的所有更改均已消失,并且奇偶校验已恢复。
假设我现在想要恢复这些更改。我可以做到这一点。第一步是找到我刚刚清除/删除的存储的哈希值:
在学习哈希值后,我成功恢复了未提交的更改:
我并不是真的想恢复这些更改,只是想验证我是否可以将它们恢复,所以我又清除了它们。
另一种选择是将存储应用到不同的分支,而不是擦除更改。因此,在清除因在错误分支上工作而进行的更改方面,
stash
为您提供了很大的灵活性,可以从错误中恢复。无论如何,如果您想要一种可逆的方法来清除分支的更改,那么在这个用例中,上述方法是一种危险性较小的方法。
REVERSIBLE Method to Discard All Changes:
I found this question after making a merge and forgetting to checkout develop immediately afterwards. You guessed it: I started modifying a few files directly on master. D'Oh! As my situation is hardly unique (we've all done it, haven't we ;->), I'll offer a reversible way I used to discard all changes to get master looking like develop again.
After doing a
git diff
to see what files were modified and assess the scope of my error, I executed:After first stashing all the changes, they were next cleared. All the changes made to the files in error to master were gone and parity restored.
Let's say I now wanted to restore those changes. I can do this. First step is to find the hash of the stash I just cleared/dropped:
After learning the hash, I successfully restored the uncommitted changes with:
I didn't really want to restore those changes, just wanted to validate I could get them back, so I cleared them again.
Another option is to apply the stash to a different branch, rather than wipe the changes. So in terms of clearing changes made from working on the wrong branch,
stash
gives you a lot of flexibility to recover from your boo-boo.Anyhoo, if you want a reversible means of clearing changes to a branch, the foregoing is a less dangerous way in this use-case.
为了使分支完全像遥控器一样......
To make the branch exactly like the remote...
对于其他人,如果您只想恢复对单个文件的更改:
For others, if you want to just revert changes on a single file:
git checkout -f
这足以满足您的问题。唯一的事情是,一旦完成,就完成了。无法撤消。
git checkout -f
This is suffice for your question. Only thing is, once its done, its done. There is no undo.
要删除新添加的文件,
请执行以下命令:-
git reset HEAD <文件>
这适用于修改后的文件和新添加的文件。
List to work on this
To remove a newly added file do the following command:-
git reset HEAD < file >
This will work for both modified file and newly added file.
所以,这里有很多遗留的答案。我将向您展示如何在 2021 年在一个分支上重新开始:
当您进行了数十次提交并更改了数十个文件并且需要重置时
现在您有一个新鲜的分支。但您可能仍然做了很多仍然很好的工作,您只需要提取那些文件即可。在您的 root git 目录中:
现在您拥有旧分支中单个文件的副本。多做几次,旧分支就会过时。现在您可以随意删除该分支,并将
feat-foo-v2
重命名为feat-foo
。假设您有一个包含一些更改的文件,并且您只想提取其中的一些更改。我建议熟悉
git checkout
上的--patch
选项:将打开一个对话框,允许您选择要保留的文件更改部分。
当你不能只创建一个新分支时
由于某种原因,您只是迷恋您的功能分支,并且不愿意或无法放弃它。您知道您需要重置一些文件,但您不需要重置整个文件。创建一个全新的分支实际上只是一个不必要的步骤。我们可以从 master 中提取新文件:
现在文件被重置了!如果您只想重置文件的部分,
--patch
应该以相同的方式工作。So, there's a number of legacy answers here. I'm going to show you how I start over on a branch in 2021:
When you have made dozens of commits and dozens of files changed and you need to reset
now you have a fresh branch. But you likely still did a bunch of work that is still good, you just need to pull in those files. While in your root git directory:
Now you have a copy of the individual file from the old branch. Do this a few more times, and the old branch will be obsolete. Now you can feel free to delete that branch, and rename
feat-foo-v2
tofeat-foo
.Let's say you have a file with some changes, and you only want to pull in some of those changes. I suggest getting familiar with the
--patch
option ongit checkout
:will open up a dialog that allows you to select the parts of the changes to the file you want to keep.
When you can't just make a new branch
For some reason, you're just enamored with your feature branch and you're unwilling or unable to give it up. You know you need to reset a few files, but you shouldn't need to reset the whole thing. Creating a whole new branch was actually just an un-necessary step. We can pull the fresh files from master:
and now a file is reset! And if you just want to reset part of a file,
--patch
should work the same way.如果您想重做/重做分支上的所有更改:
If you want to redo/re-do all the changes on your branch:
怎么样
rm -rf
其次是git clone.git
阅读所有这些建议后,这正是我最终所做的并且它有效完美!
How about
rm -rf <working dir>
followed bygit clone <repo>.git
After reading all these suggestions, its exactly what I ended up doing and it worked perfectly!