1

I want to backup a Git versioned project, including the working directory changes, and a pointer to the remote repository.

The backed-up folder should contain the working directory files, and needed .git files in order to allow the user to navigate to the project and run git pull (or another git command) safely. It doesn't matter if the remote directory needs to be downloaded again and check "integrity" with the working directory.

I can't backup the whole .git folder, as it contains very large files.

I tried these commands:

  • git --aggressive
  • git reflog expire --expire=now --all
  • git repack -ad
  • git prune

But they still leave large (>200 MB) *.pack files in .git/objects/pack folder.

  • I tried removing the objects folder but that makes the repository invalid: fatal: Not a git repository (or any of the parent directories): .git.
    • I tried git init after removing objects folder, but that gives the error fatal: bad object HEAD, and I can't do any other command.
  • I tried removing all files and folders inside .git, except for .git/config, and it's still not a valid git repository.
    • I tried git init after removing those folders, but then the changes on the working directory become "untracked", even though they are the same files on the remote repository, so I'm forced to run git clean -fd and download the repository again.

Is there a way to create a bare bones Git repository, with minimum pack files, while maintaining the "tracked" status of the working directory?

Jorjon
  • 5,316
  • 1
  • 41
  • 58

2 Answers2

0

You can create:

  • an archive of the working tree:

    zip -r myrepo.zip myrepo -x *.git*
    
  • a bundle of the .git repo (as explained here):

    cd myrepo
    git bundle create /tmp/repositoryname.bundle --all
    

That would give you two files (easier to copy over any location you want), with the minimum size.
But if your repo is big, you might want to create an incremental bundle, with only the last few commits in it (provided the target location where you might use that bundle has already the rest of the history)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Pack files in .git/objects contain the history of old versions. If you want to backup git repo with working directory changes, you should keep the pack files integral. There is also a way to push it to bare repo and then clone it to anywhere you want. Detail steps as below:

  1. Create a bare repo for backup by git init --bare.
  2. Push the repo to bare repo:

git remote add backup <path for bare repo> git push backup --all

In bare repo path, add the remote repo information in the .git/config file:

[remote "origin"]
    url = <remote repo URL>
    fetch = +refs/heads/*:refs/remotes/origin/*
  1. Clone backup repo anywhere for you want by git clone <path for bare repo>. In the cloned repo, if you want the changes directly push to remote repo, you can change the URL in .git/config or use git remote set-url origin <remote URL>.
Marina Liu
  • 36,876
  • 5
  • 61
  • 74