如何放弃对分支所做的所有更改?

发布于 2024-10-14 22:30:25 字数 176 浏览 2 评论 0原文

我正在一个分支(即 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 技术交流群。

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

发布评论

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

评论(15

浅紫色的梦幻 2024-10-21 22:30:25

注意:您无法撤消此操作。

尝试 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.

∞琼窗梦回ˉ 2024-10-21 22:30:25

如果您想丢弃自上次提交以来的所有内容, git reset --hard 可以帮助您

git reset --hard can help you if you want to throw away everything since your last commit

撩人痒 2024-10-21 22:30:25
git diff master > branch.diff
git apply --reverse branch.diff
git diff master > branch.diff
git apply --reverse branch.diff
就是爱搞怪 2024-10-21 22:30:25

如果您不希望对设计进行任何更改,并且确实希望它仅匹配远程分支,您也可以删除该分支并重新创建它:

# 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

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:

# 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
与之呼应 2024-10-21 22:30:25

当您想放弃本地分支中的更改时,可以使用 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.

唐婉 2024-10-21 22:30:25

在源根目录中:
<代码>
git reset ./ HEAD <--取消暂存任何暂存的更改
git checkout ./ <--放弃任何未暂存的更改

In the source root:

git reset ./ HEAD <--un-stage any staged changes
git checkout ./ <--discard any unstaged changes

小…楫夜泊 2024-10-21 22:30:25

@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

却一份温柔 2024-10-21 22:30:25

放弃所有更改的可逆方法:

我在进行合并后发现了这个问题,并且忘记立即签出开发。您猜对了:我开始直接在 ma​​ster 上修改一些文件。噢!由于我的情况并不独特(我们都做过,不是吗;->),我将提供一种可逆的方法,我曾经放弃所有更改以获得ma​​ster 看起来又开发了。

在执行 git diff 来查看修改了哪些文件并评估错误范围后,我执行了:

git stash
git stash clear

首先存储所有更改后,接下来清除它们。对 ma​​ster 错误的文件所做的所有更改均已消失,并且奇偶校验已恢复。

假设我现在想要恢复这些更改。我可以做到这一点。第一步是找到我刚刚清除/删除的存储的哈希值:

git fsck --no-reflog | awk '/dangling commit/ {print $3}'

在学习哈希值后,我成功恢复了未提交的更改:

git stash apply hash-of-cleared-stash

我并不是真的想恢复这些更改,只是想验证我是否可以将它们恢复,所以我又清除了它们。

另一种选择是将存储应用到不同的分支,而不是擦除更改。因此,在清除因在错误分支上工作而进行的更改方面,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:

git stash
git stash clear

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:

git fsck --no-reflog | awk '/dangling commit/ {print $3}'

After learning the hash, I successfully restored the uncommitted changes with:

git stash apply hash-of-cleared-stash

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.

跨年 2024-10-21 22:30:25

为了使分支完全像遥控器一样......

git fetch --all
git reset --hard origin/<BRANCH_NAME>
git clean -fdx # STAY ALERT! Untracked (.gitignore) directories and files will be gone!

To make the branch exactly like the remote...

git fetch --all
git reset --hard origin/<BRANCH_NAME>
git clean -fdx # STAY ALERT! Untracked (.gitignore) directories and files will be gone!
合约呢 2024-10-21 22:30:25

对于其他人,如果您只想恢复对单个文件的更改:

git restore <fileName>

For others, if you want to just revert changes on a single file:

git restore <fileName>
南渊 2024-10-21 22:30:25

git checkout -f

这足以满足您的问题。唯一的事情是,一旦完成,就完成了。无法撤消。

git checkout -f

This is suffice for your question. Only thing is, once its done, its done. There is no undo.

枯叶蝶 2024-10-21 22:30:25

要删除新添加的文件,

git checkout -f

git reset --hard

git reset --hard HEAD^

请执行以下命令:-

git reset HEAD <文件>

这适用于修改后的文件和新添加的文件。

List to work on this

git checkout -f

git reset --hard

git reset --hard HEAD^

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.

赴月观长安 2024-10-21 22:30:25

所以,这里有很多遗留的答案。我将向您展示如何在 2021 年在一个分支上重新开始:

当您进行了数十次提交并更改了数十个文件并且需要重置时

git checkout master
git pull origin master
git checkout -b feat-foo-v2 # make a second version of feat-foo branch

现在您有一个新鲜的分支。但您可能仍然做了很多仍然很好的工作,您只需要提取那些文件即可。在您的 root git 目录中:

git checkout feat-foo -- path/to/file/to/be/used.java

现在您拥有旧分支中单个文件的副本。多做几次,旧分支就会过时。现在您可以随意删除该分支,并将 feat-foo-v2 重命名为 feat-foo

假设您有一个包含一些更改的文件,并且您只想提取其中的一些更改。我建议熟悉 git checkout 上的 --patch 选项:

git checkout -p feat-foo -- path/to/file.java

将打开一个对话框,允许您选择要保留的文件更改部分。

当你不能只创建一个新分支时
由于某种原因,您只是迷恋您的功能分支,并且不愿意或无法放弃它。您知道您需要重置一些文件,但您不需要重置整个文件。创建一个全新的分支实际上只是一个不必要的步骤。我们可以从 master 中提取文件:

git checkout master -- path/to/file.java

现在文件被重置了!如果您只想重置文件的部分--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

git checkout master
git pull origin master
git checkout -b feat-foo-v2 # make a second version of feat-foo branch

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:

git checkout feat-foo -- path/to/file/to/be/used.java

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 to feat-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 on git checkout:

git checkout -p feat-foo -- path/to/file.java

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:

git checkout master -- path/to/file.java

and now a file is reset! And if you just want to reset part of a file, --patch should work the same way.

卸妝后依然美 2024-10-21 22:30:25

如果您想重做/重做分支上的所有更改:

git pull origin master --rebase # or, denote the latest "base" or "master" commit on your branch
git push
git reset --soft origin/<current branch name>
# re-evaluate all your changes, tweaking them at will
git reset --soft origin/master
# commit your tweaks, push

If you want to redo/re-do all the changes on your branch:

git pull origin master --rebase # or, denote the latest "base" or "master" commit on your branch
git push
git reset --soft origin/<current branch name>
# re-evaluate all your changes, tweaking them at will
git reset --soft origin/master
# commit your tweaks, push
幸福不弃 2024-10-21 22:30:25

怎么样 rm -rf 其次是 git clone.git

阅读所有这些建议后,这正是我最终所做的并且它有效完美!

How about rm -rf <working dir> followed by git clone <repo>.git

After reading all these suggestions, its exactly what I ended up doing and it worked perfectly!

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