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
git stash --include-untracked
(or with the shorthand-u
)
Or git stash --all
which stashes all files, depending on what you want to save...
git gc
(no mandatory but to make the copy faster)- 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:
git reset --hard Head
# restore all filesgit stash pop
# restore uncommitted changes