3

I'd like to back up (via dropbox or google drive or similar) the unpushed commits in my local repository. Backing up the entire repository (with git push --mirror or similar) is redundant, since 99% of its commits exist in the central shared repo, as well as on all the other developers' machines on our team. Also, a full clone of the repo may exceed my quota in my target backup system.

I think what I want is some sort of git bundle command, but I'm not sure how to pass the equivalent of --all not in any remote refs to it.

Matt McHenry
  • 20,009
  • 8
  • 65
  • 64

2 Answers2

2

This answer sets out the basic principle of using ^<remote>/<branch> to exclude all commits reachable from the remote branch.

This script should get you going:

# See https://stackoverflow.com/a/18359250/3150057 for awk branch parsing.
# Get all local branch names.
LOCAL_HEADS=$(git branch | awk -F ' +' '! /\(no branch\)/ {print $2}')
# Get all remote branch names, prefixed with ^ to 'not' them.
REMOTE_HEADS=$(git branch -r | awk -F ' +' '! /\(no branch\)/ {print "^"$2}')

# -- is needed at the end to tell git we are only giving it revisions.
# List all commits reachable from local branches and tags but exclude those reachable by the remote branches.
git rev-list $REMOTE_HEADS $LOCAL_HEADS --tags --
# Pass the same args to create a bundle of all the above commits.
git bundle create my-backup-today.bundle $REMOTE_HEADS $LOCAL_HEADS --tags --
Community
  • 1
  • 1
Henry Blyth
  • 1,700
  • 1
  • 15
  • 23
  • I'm at a different machine to the setup I made to test the above script so I can't say for sure, but [this answer](http://stackoverflow.com/a/5720366/3150057) mentions the `--tags` option. However, I think that will be *all tags*, not just those pointing to commits going into the bundle. – Henry Blyth Jan 03 '14 at 08:58
  • Having read the man page for `rev-list`, `--tags` will include all tags as commits. You can limit with `--tags=`, but there's no need since you want to add all commits bar those in `$REMOTE_HEADS`. Updated the answer to include tags. – Henry Blyth Jan 03 '14 at 23:48
0

Unless your repository is huge, I'm not sure that I see the benefit of bypassing git push.

Once you've done it the first time your pushes become incremental, and getting data back out of your private remote is super easy. Your new private-remote/* remote branches visually show you what's been pushed and what hasn't.

With git bundle your workflow becomes more complex, and you lose visibility of which commits are backed up.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • I would schedule these backups to happen periodically in the background, so visibility is not really necessary (and may even be a negative distraction). – Matt McHenry Jan 01 '14 at 20:12