Skip to content

Instantly share code, notes, and snippets.

@xtream1101
Created June 29, 2019 12:38
Show Gist options
  • Save xtream1101/fd79f3099f572967605fab24d976b179 to your computer and use it in GitHub Desktop.
Save xtream1101/fd79f3099f572967605fab24d976b179 to your computer and use it in GitHub Desktop.
Backup and restore a git repo using git bundle

Backup/archive a repo

  1. Clone the repo
git clone --mirror https://github.com/vuejs/vue
  1. cd into the cloned repo
  2. Create a bundle file in the parent directory
git bundle create ../vuejs_vue.bundle --all
  1. That bundle file is now a full archive of the repo, including all of its branches and tags

Restore a repo from a bundle file

Here we will restore the repo from the bundle and create a new remote origin that will contain all brnaches and tags

  1. Clone the repo from the bundle
git clone vuejs_vue.bundle
  1. Get all the branches locally to be pushed up to your origin later (from: https://gist.github.com/grimzy/a1d3aae40412634df29cf86bb74a6f72)
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
  1. Create a new repo on your git server and update the origin of the local repo
git remote set-url origin git@github.com/xtream1101/test-backup.git
  1. Push all branches and tags to the new remote origin
git push --all
git push --tags
@keyserjaya
Copy link

keyserjaya commented Dec 7, 2022

Got this error in windows power shell, please help :'

PS D:\WebProjects\Restore\spectator-v2.git\repo> git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
At line:1 char:54
+ git branch -r | grep -v '\->' | while read remote; do git branch --tr ...
+                                                      ~
Missing statement body in do loop.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingLoopStatement

@poa00
Copy link

poa00 commented May 27, 2024

Got this error in windows power shell, please help :'

PS D:\WebProjects\Restore\spectator-v2.git\repo> git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
At line:1 char:54
+ git branch -r | grep -v '\->' | while read remote; do git branch --tr ...
+                                                      ~
Missing statement body in do loop.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingLoopStatement

@keyserjaya This reply is pretty late (I just came across this script), but this is a bash (shell) script meant to be run in a Linux shell. If you have WSL2 installed, you can likely use the command line in a virtual instance of Linux to run the script, but it will not be supported by Windows PowerShell.

If you have git for Windows installed, you likely have a Bash terminal (shortcut in the Start Menu) which can run some Shell scripts but Bash on Windows does not support things like grep commands out-of-the-box. (There may be third-party Windows tools that simulate the program's functionality -- see wget for example -- but not that I know of for grep in particular.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment