git 如何放弃对分支所做的所有更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4830056/
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 all changes made to a branch?
提问by Will
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.
我在一个分支(即design
)中工作,并且我进行了一些更改,但是我需要将它们全部丢弃并重置以匹配存储库版本。我以为git checkout design
会这样做,但它只是告诉我我已经在分支中design
并且我有 3 个修改过的文件。
How would I discard those changes and get the branch as it stands now on the remote server?
我将如何放弃这些更改并获得现在在远程服务器上的分支?
回答by ismail
Note: You CANNOT UNDOthis.
注意:您无法撤消此操作。
Try git checkout -f
this will discard any local changes which are notcommitted in ALLbranches and master.
尝试git checkout -f
这将丢弃所有未在所有分支和 master 中提交的本地更改。
回答by gor
git reset --hardcan help you if you want to throw away everything since your last commit
如果你想扔掉自上次提交以来的所有东西,git reset --hard可以帮助你
回答by Mike Kaganski
git diff master > branch.diff
git apply --reverse branch.diff
回答by mipadi
If you don't want anychanges in design
and definitely want it to just match a remote's branch, you can also just delete the branch and recreate it:
如果您不希望进行任何更改design
并且绝对希望它仅与远程分支匹配,您也可以删除该分支并重新创建它:
# Switch to some branch other than design
$ git br -D design
$ git co -b design origin/design # Will set up design to track origin's design branch
回答by Cesar A. Rivas
@Will, git immersionis 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
@Will,git immersion是一个非常好的和简单的 git 教程。它将向您展示如何在以下情况下撤消更改:未暂存、暂存和已提交。实验室 14-18
回答by user4948761
When you want to discard changes in your local branch, you can stash these changes using git stash command.
当您想放弃本地分支中的更改时,您可以使用 git stash 命令隐藏这些更改。
git stash save "some_name"
git stash 保存“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 pull 从主分支中提取最新代码。
回答by Clifford Harms
In the source root:
git reset ./ HEAD <--un-stage any staged changes
git checkout ./ <--discard any unstaged changes
在源根目录中:
git reset ./ HEAD <--un-stage any staged changes
git checkout ./ <--discard any unstaged changes
回答by thestar
git checkout -f
git checkout -f
This is suffice for your question. Only thing is, once its done, its done. There is no undo.
这足以回答你的问题。唯一的事情是,一旦完成,它就完成了。没有撤消。