简体   繁体   English

如何重置整个Git存储库,而不只是重置“ Master”分支,以匹配Remote?

[英]How to reset the entire Git repository, and not only the “Master” branch, to match Remote?

What the title says. 标题怎么说。

I want to reset each and every local branch to match my Remote repository, including deleting some branches and tags which only exists locally, without having to delete everything and cloning from scratch. 我想重置每个本地分支以匹配我的远程存储库,包括删除一些仅在本地存在的分支和标签,而不必删除所有内容并从头进行克隆。 All I could find are instructions on how to reset a specific branch, but not the entire repository. 我能找到的只是关于如何重置特定分支的说明,而不是整个存储库的说明。

Even better if it can be done from the TortoiseGit Shell extension. 如果可以从TortoiseGit Shell扩展中完成,那就更好了。 But I'm also fine with the command line. 但是我对命令行也没问题。

You can do it by following commands: 您可以通过以下命令进行操作:

git checkout --orphan @
git fetch <Remote> refs/*:refs/* --refmap= --prune --force

where <Remote> is remote repository you want to use. 其中<Remote>是要使用的远程存储库。 You simply fetch all remote refs ( refs/*:refs/* ) with --prune and --force flags to remove and force update local references. 您只需使用--prune--force标志fetch所有远程引用( refs/*:refs/* ),即可删除并强制更新本地引用。

The following line will reset all local branches that have a configured upstream branch to the state of the upstream branch 下面的行会将所有已配置上游分支的本地分支重置为上游分支的状态

git checkout @{0} && git for-each-ref refs/heads --format '%(refname:strip=2)' | xargs -ri sh -c 'git rev-parse {}@{u} >/dev/null 2>&1 && git branch -f {} $(git rev-parse {}@{u})'

You will end up with a detached HEAD due to the first command, because you cannot reset the currently checked out branch, so checkout the branch you want to have in your working directory after doing this. 由于第一个命令,您将最终获得一个分离的HEAD ,因为您无法重置当前已签出的分支,因此请在执行此操作后签出您想在工作目录中拥有的分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM