Home > Software design >  Efficient way to back up a git repository along with the current staged & unstaged changes?
Efficient way to back up a git repository along with the current staged & unstaged changes?

Time:06-03

I'm looking for a more efficient / fast way to make a temporary copy of a git project before starting to make crazy git commands. Yes, git never deletes things, and they can be recovered, but sometimes it will take a lot of figuring out, especially if you have uncommitted changes, so it's just easier to make a copy, and if things go south, then copy from there.

I'd like to copy the git repository itself, the changes in the working directory, to preserve what is staged and unstaged, but to leave out whatever is .gitignored, as those files are generated by the build system, and can be regenerated from the sources.

I tried rsync -avz project project.2 --filter ":- .gitignore", but apparantly it did not copy everything, after doing a git status in the newly made copy, git reports files missing, and some type changes - which is wierd, but that's the result...

CodePudding user response:

I don't know if it's the more efficient but it's an easy one to do...

Backup

  1. git stash --include-untracked (or with the shorthand -u)

Or git stash --all which stashes all files, depending on what you want to save...

  1. git gc (no mandatory but to make the copy faster)
  2. Copy/rsync only the .git folder (that now contains everything to be able to restore the working directory in the exact same state) in another empty directory

Restore

In the backup directory or after copying back the .git folder:

  1. git reset --hard Head # restore all files
  2. git stash pop # restore uncommitted changes
  •  Tags:  
  • git
  • Related