0

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.

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
  • This might be a duplicate of http://stackoverflow.com/questions/13064613/how-to-prune-local-tracking-branches-that-do-not-exist-on-remote-anymore – Juuuuuu Dec 14 '16 at 16:02
  • How close to the initial state do you want it? Do you want the reflogs for surviving branches preserved? merge options? The most brutal answer is to just clone without refetching, ```git clone --reference=.git `git config remote.origin.url` ../newclone; cp -al .git/objects ../newclone/.git; rm ../newclone/.git/objects/info/alternates``` but that doesn't preserve reflogs or merge/pull options or anything, it's a completely pristine clone. – jthill Dec 14 '16 at 18:45
  • @jthill I really want an exact clone of Remote, so your solution appears to be valid. For now I went with [PetSerAl's answer](http://stackoverflow.com/a/41147559/3258851), but if you know your suggestion would be preferable and why, I might change my chosen answer! – Marc.2377 Dec 15 '16 at 01:54

2 Answers2

1

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.

user4003407
  • 21,204
  • 4
  • 50
  • 60
  • Right idea, but this will mirror the remote, including its remote-tracking, not quite what OP wants. – jthill Dec 14 '16 at 17:00
  • @jthill OP ask: *How to reset the **entire** Git repository*. If you want reset something else, like only branches and tags, then you need to change refspecs to whatever you want. For example: `refs/heads/*:refs/heads/* refs/tags/*:refs/tags/*`. – user4003407 Dec 14 '16 at 17:08
0

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.

Vampire
  • 35,631
  • 4
  • 76
  • 102