将本地存储库分支重置为就像远程存储库 HEAD 一样
如何将本地分支重置为与远程存储库上的分支一样?
我尝试过:
git reset --hard HEAD
但是 git status
声称我已修改文件:
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: java/com/mycompany/TestContacts.java
modified: java/com/mycompany/TestParser.java
How do I reset my local branch to be just like the branch on the remote repository?
I tried:
git reset --hard HEAD
But git status
claims I have modified files:
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: java/com/mycompany/TestContacts.java
modified: java/com/mycompany/TestParser.java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
如果您不介意保存本地更改,但仍想更新存储库以匹配 origin/HEAD,则可以简单地存储本地更改,然后拉取:
If you don't mind saving your local changes, yet still want to update your repository to match origin/HEAD, you can simply stash your local changes and then pull:
将分支设置为与远程分支完全匹配可以通过两个步骤完成:
如果您想在执行此操作之前保存当前分支的状态(以防万一),您可以执行以下操作:
现在您的工作已保存在分支“my-saved”上-work”,以防您决定要找回它(或者想稍后查看它或将其与更新的分支进行比较)。
请注意,第一个示例假设远程存储库的名称为“origin”,并且远程存储库中名为“master”的分支与本地存储库中当前签出的分支匹配。
顺便说一句,您所处的这种情况看起来非常像常见的情况,即推送已完成到非裸存储库当前签出的分支。您最近是否推送到了本地存储库?如果不是,那么不用担心——一定是其他原因导致这些文件意外地最终被修改。否则,您应该注意,不建议推送到非裸存储库(特别是不要推送到当前签出的分支)。
Setting your branch to exactly match the remote branch can be done in two steps:
If you want to save your current branch's state before doing this (just in case), you can do:
Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).
Note that the first example assumes that the remote repo's name is "origin" and that the branch named "master" in the remote repo matches the currently checked-out branch in your local repo.
BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).
首先,使用 git reset 重置为之前获取的
HEAD
相应上游分支的名称:指定
@{u}
或其详细形式@{upstream}
的优点是远程存储库和分支的名称没有予以明确指定。在 Windows 或 PowerShell 中,指定"@{u}"
(带双引号)。接下来,根据需要,使用 git clean 删除未跟踪的文件,也可以选择使用
-x
:最后,根据需要,获取最新的更改:
First, use git reset to reset to the previously fetched
HEAD
of the corresponding upstream branch:The advantage of specifying
@{u}
or its verbose form@{upstream}
is that the name of the remote repo and branch don't have to be explicitly specified. On Windows or with PowerShell, specify"@{u}"
(with double quotes).Next, as needed, use git clean to remove untracked files, optionally also with
-x
:Finally, as needed, get the latest changes:
我需要做(接受的答案中的解决方案):
其次是:
删除本地文件
要查看将删除哪些文件(而不实际删除它们):
I needed to do (the solution in the accepted answer):
Followed by:
to remove local files
To see what files will be removed (without actually removing them):
git reset --hard HEAD
实际上只重置到最后提交的状态。在这种情况下,HEAD 指的是您的分支的 HEAD。如果您有多个提交,这将不起作用。
您可能想要做的就是重置为原始头或远程存储库的名称。我可能会做类似
“小心一点”之类的事情。硬重置无法轻易撤消。最好按照 Dan 的建议进行操作,并在重置之前分支出更改的副本。
git reset --hard HEAD
actually only resets to the last committed state. In this case HEAD refers to the HEAD of your branch.If you have several commits, this won't work..
What you probably want to do, is reset to the head of origin or whatever you remote repository is called. I'd probably just do something like
Be careful though. Hard resets cannot easily be undone. It is better to do as Dan suggests, and branch off a copy of your changes before resetting.
上述所有建议都是正确的,但通常要真正重置您的项目,您甚至还需要删除
.gitignore
中的文件。要获得与从远程删除项目目录并重新克隆相同的道德效果,请执行以下操作:
警告:
git clean -x -d -f
不可逆,您可能会丢失文件和数据(例如,您使用.gitignore
忽略的内容)。All of the above suggests are right, but often to really reset your project, you also need to remove even files that are in your
.gitignore
.To get the moral equivalent of erasing your project directory and re-cloning from the remote is:
Warning:
git clean -x -d -f
is irreversible and you may lose files and data (e.g. things you have ignored using.gitignore
).使用下面的命令。这些命令也会从本地 git 中删除所有未跟踪的文件
Use the commands below. These commands will remove all untracked files from local git too
这个问题在这里混合了两个问题:
git status 说
没有什么可提交的,工作目录干净。
一站式答案是:
git fetch --prune
(可选)更新远程存储库的本地快照。更多命令仅限本地。git reset --hard @{upstream}
将本地分支指针指向远程快照所在的位置,并设置索引和工作目录为远程快照的文件git clean -d --force
删除阻碍 git 说“工作目录干净”的未跟踪文件和目录。The question mixes two issues here:
git status
saysnothing to commit, working directory clean.
The one-stop-answer is:
git fetch --prune
(optional) Updates the local snapshot of the remote repo. Further commands are local only.git reset --hard @{upstream}
Puts the local branch pointer to where the snapshot of the remote is, as well as set the index and the working directory to the files of that commit.git clean -d --force
Removes untracked files and directories which hinder git to say “working directory clean”.假设远程存储库是
origin
,并且您对branch_name
感兴趣:此外,您还可以将
origin
的当前分支重置为头
。工作原理:
git fetch origin
从远程下载最新版本,无需尝试合并或变基任何内容。然后
git reset
将
分支重置为您刚刚获取的内容。--hard
选项更改工作树中的所有文件,以匹配origin/branch_name
中的文件。Provided that the remote repository is
origin
, and that you're interested inbranch_name
:Also, you go for reset the current branch of
origin
toHEAD
.How it works:
git fetch origin
downloads the latest from remote without trying to merge or rebase anything.Then the
git reset
resets the<branch_name>
branch to what you just fetched. The--hard
option changes all the files in your working tree to match the files inorigin/branch_name
.您可以获取原点并重置来解决问题
您可以在执行重置之前保存更改,如下所示,
重置后,如果您想要恢复更改,您可以简单地运行,
You can fetch the origin and reset to solve the problem
You can save your changes before doing the reset like below,
And after resetting, if you want that changes back, you can simply run,
这是我经常面对的事情,&我已经概括了 Wolfgang 上面提供的脚本,使其可以与任何分支一起使用,
我还添加了“您确定吗”提示,&一些反馈输出
This is something I face regularly, & I've generalised the script Wolfgang provided above to work with any branch
I also added an "are you sure" prompt, & some feedback output
2023 简单解决方案:
重置到
origin/HEAD
并不总是有效,因为它不一定是当前分支的最新提交。2023 simple solution :
Reseting to
origin/HEAD
won't always work as it's not necessarily the latest commit of your current branch.我做了:
要完全重置分支
注释,您应该签出到另一个分支才能删除所需的分支
I did:
to totally reset branch
note, you should checkout to another branch to be able to delete required branch
这是一个脚本,可以自动执行最流行的答案的建议......
请参阅 https://stackoverflow.com/a/13308579/1497139 了解支持分支的改进版本
Here is a script that automates what the most popular answer suggests ...
See https://stackoverflow.com/a/13308579/1497139 for an improved version that supports branches
答案
被低估了(-d 删除目录)。
谢谢!
The answer
was underrated (-d to remove directories).
Thanks!
如果你像我一样遇到问题,你已经提交了一些更改,但现在,无论出于何种原因你想摆脱它,最快的方法是使用 git reset ,如下所示
: 2 个不需要的提交,因此数字为 2。您可以将其更改为您自己的要重置的提交数。
所以回答你的问题 - 如果你比远程存储库 HEAD 提前 5 次提交,你应该运行这个命令:
请注意,你将丢失所做的更改,所以要小心!
If you had a problem as me, that you have already committed some changes, but now, for any reason you want to get rid of it, the quickest way is to use
git reset
like this:I had 2 not needed commits, hence the number 2. You can change it to your own number of commits to reset.
So answering your question - if you're 5 commits ahead of remote repository HEAD, you should run this command:
Notice that you will lose the changes you've made, so be careful!
前面的答案假设要重置的分支是当前分支(已签出)。在评论中,OP hap497 澄清该分支确实已签出,但这并不是原始问题明确要求的。由于至少存在一个“重复”问题,将分支完全重置为存储库状态,它不假设分支已签出,这里有一个替代方案:
如果分支“mybranch”当前未签出,则将其重置为远程分支“myremote/mybranch”的头,您可以使用此 低级 命令:
此方法将签出分支保留为是的,并且工作树未受影响。它只是将 mybranch 的头移动到另一个提交,无论第二个参数给出什么。如果需要将多个分支更新到新的远程头,这尤其有用。
不过,执行此操作时要小心,并使用 gitk 或类似工具来仔细检查源和目标。如果你不小心在当前分支上执行此操作(git 不会阻止你这样做),你可能会感到困惑,因为新分支内容与工作树不匹配,工作树没有改变(要修复,请再次更新分支,到之前的位置)。
Previous answers assume that the branch to be reset is the current branch (checked out). In comments, OP hap497 clarified that the branch is indeed checked out, but this is not explicitly required by the original question. Since there is at least one "duplicate" question, Reset branch completely to repository state, which does not assume that the branch is checked out, here's an alternative:
If branch "mybranch" is not currently checked out, to reset it to remote branch "myremote/mybranch"'s head, you can use this low-level command:
This method leaves the checked out branch as it is, and the working tree untouched. It simply moves mybranch's head to another commit, whatever is given as the second argument. This is especially helpful if multiple branches need to be updated to new remote heads.
Use caution when doing this, though, and use
gitk
or a similar tool to double check source and destination. If you accidentally do this on the current branch (and git will not keep you from this), you may become confused, because the new branch content does not match the working tree, which did not change (to fix, update the branch again, to where it was before).这是我经常使用的:
请注意,最好不要对本地 master/develop 分支进行更改,而是签出到另一个分支进行任何更改,并在分支名称前面加上更改类型,例如
feat/
、chore/
、fix/
等。因此,您只需要拉取更改,而不需要从 master 推送任何更改。对于其他人贡献的其他分支也是如此。因此,仅当您碰巧将更改提交到其他人已提交的分支并且需要重置时,才应使用上述内容。否则,将来避免推送到其他人推送到的分支,而是通过签出分支签出并推送到所述分支。如果您想将本地分支重置为上游分支中的最新提交,到目前为止对我有用的是:
检查您的遥控器,确保您的上游和原点符合您的预期,如果不符合预期,则使用 git远程添加上游<插入 URL>,例如您派生的原始 GitHub 存储库的上游,和/或
git 远程添加源<插入派生的 GitHub 存储库的 URL>
。在 GitHub 上,您还可以签出与本地分支同名的分支,以便将工作保存在那里,尽管如果原始开发具有与本地保存的工作分支相同的更改,则没有必要这样做。我使用开发分支作为示例,但它可以是任何现有的分支名称。
然后,如果您需要在存在任何冲突的情况下将这些更改与另一个分支合并,并保留开发中的更改,请使用:
While use
来保留branch_name的冲突更改。否则,请使用带有
git mergetool
的合并工具。将所有更改放在一起:
请注意,您可以使用提交哈希、其他分支名称等来代替上游/开发。使用 CLI 工具(例如 Oh My Zsh)检查您的分支是否为绿色,表明没有任何内容需要提交,并且工作目录是干净的(可以通过 git status 确认或验证)。请注意,与上游开发相比,如果提交自动添加了任何内容,例如 UML 图、许可证头等,这实际上可能会添加提交,因此在这种情况下,您可以在
origindevelopment发送到
上游开发
。This is what I use often:
Note that it is good practice not to make changes to your local master/develop branch, but instead checkout to another branch for any change, with the branch name prepended by the type of change, e.g.
feat/
,chore/
,fix/
, etc. Thus you only need to pull changes, not push any changes from master. Same thing for other branches that others contribute to. So the above should only be used if you have happened to commit changes to a branch that others have committed to, and need to reset. Otherwise in future avoid pushing to a branch that others push to, instead checkout and push to the said branch via the checked out branch.If you want to reset your local branch to the latest commit in the upstream branch, what works for me so far is:
Check your remotes, make sure your upstream and origin are what you expect, if not as expected then use
git remote add upstream <insert URL>
, e.g. of the original GitHub repo that you forked from, and/orgit remote add origin <insert URL of the forked GitHub repo>
.On GitHub, you can also checkout the branch with the same name as the local one, in order to save the work there, although this isn't necessary if origin develop has the same changes as the local saved-work branch. I'm using the develop branch as an example, but it can be any existing branch name.
Then if you need to merge these changes with another branch while where there are any conflicts, preserving the changes in develop, use:
While use
to preserve branch_name's conflicting changes. Otherwise use a mergetool with
git mergetool
.With all the changes together:
Note that instead of upstream/develop you could use a commit hash, other branch name, etc. Use a CLI tool such as Oh My Zsh to check that your branch is green indicating that there is nothing to commit and the working directory is clean (which is confirmed or also verifiable by
git status
). Note that this may actually add commits compared to upstream develop if there is anything automatically added by a commit, e.g. UML diagrams, license headers, etc., so in that case, you could then pull the changes onorigin develop
toupstream develop
, if needed.只需 3 个命令即可使其工作
Only 3 commands will make it work
这里评价最高的答案没有按预期重置我的本地代码。
而是:
检查默认远程分支的名称(这不是 git 的东西,因此请在 GitHub 中检查),然后替换 main 或 master在下面的步骤 4 中这样
save current stuff 替换下面步骤 4 中的 main 或 master
git stash -u
从远程更新
git fetch origin
重置到远程默认分支(但请参阅上面的步骤 1)
git reset --hard origin/main
The top rated answer here did not reset my local code as expected.
Instead:
check the name of your default remote branch (this is not a git thing so check in GitHub) then replace main or master in step 4 below with this
save current stuff
git stash -u
update from remote
git fetch origin
reset to remote default branch (but see step 1 above)
git reset --hard origin/main
如果您想返回工作目录和索引的
HEAD
状态,那么您应该git reset --hard HEAD
,而不是HEAD ^。 (这可能是一个错字,就像
--hard
的单破折号与双破折号一样。)至于您关于为什么这些文件显示在已修改状态的具体问题,看起来也许您进行了软重置而不是硬重置。这将导致
HEAD
提交中更改的文件看起来就像已暂存一样,这可能就是您在这里看到的情况。If you want to go back to the
HEAD
state for both the working directory and the index, then you shouldgit reset --hard HEAD
, rather than toHEAD^
. (This may have been a typo, just like the single versus double dash for--hard
.)As for your specific question as to why those files appear in the status as modified, it looks like perhaps you did a soft reset instead of a hard reset. This will cause the files that were changed in the
HEAD
commit to appear as if they were staged, which is likely what you are seeing here.大量的重置和清理似乎对我本地 git 存储库中未跟踪和修改的文件没有任何影响(我尝试了上面的所有选项)。我对此的唯一解决方案是 rm 本地存储库并从远程重新克隆它。
幸运的是,我没有任何其他我关心的分支。
xkcd:Git
No amount of reset and cleaning seemed to have any effect on untracked and modified files in my local git repo (I tried all the options above). My only solution to this was to rm the local repo and re-clone it from the remote.
Fortunately I didn't have any other branches I cared about.
xkcd: Git
首先,检查 git status 是否有任何本地更改。如果是,请将它们藏起来。
然后执行:
它将当前本地分支重置为用于 git push 的同一远程分支。
当配置了 git config push.default current 时,这特别有用。例如,当您的分支是
abc
且远程分支是origin
时,它会将其重置为origin/abc
。请参阅 Git 修订版,了解有关 <代码>@{推}
First, check with
git status
if you have any local changes. If yes, stash them.Then execute:
It will reset the current local branch to the same remote branch which would be used for
git push
.This is especially useful when
git config push.default current
is configured. For example, when your branch isabc
and remote isorigin
, it will reset it toorigin/abc
.Please see Git revisions for more details about
@{push}
我见过的唯一适用于所有情况的解决方案是删除并重新克隆。也许还有另一种方法,但显然这种方法不会让旧状态留在那里,所以我更喜欢它。如果你经常在 git 中搞砸事情,你可以将 Bash 一行设置为宏:
* 假设你的 .git 文件没有损坏
The only solution that works in all cases that I've seen is to delete and reclone. Maybe there's another way, but obviously this way leaves no chance of old state being left there, so I prefer it. Bash one-liner you can set as a macro if you often mess things up in git:
* assumes your .git files aren't corrupt
您是否忘记创建功能分支并错误地直接提交到 master 上?
您现在可以创建功能分支并将 master 设置回来,而不会影响工作树(本地文件系统),以避免触发构建、测试和文件锁问题:
Have you forgotten to create a feature-branch and have committed directly on master by mistake?
You can create the feature branch now and set master back without affecting the worktree (local filesystem) to avoid triggering builds, tests and trouble with file-locks:
git stash
git stash apply
git pull