简体   繁体   中英

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. 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. You simply fetch all remote refs ( refs/*:refs/* ) with --prune and --force flags to remove and force update local references.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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