After a recent failure of my hard drive, due to which I have lost 7 days of work, because I forgot about pushing my commits, I decided to create some sort of an automated process that will run and automatically commit and push my changes, since I definetely don't want to trust myself about remebering it.
I didn't want to push into my normal branches on which I worked, because that would create a mess in my git log. A better solution on which I have decided was that I would create a brancha named "backup" to which all my code from my current branch on which I was working, would be pushed to and I would use my backup branch as last resort only.
For example if I am working on a branch feature/add_spinners_for_activities, periodically my code should be copied to the branch "backup" and then pushed to the repo.
I created this script, which I'll schedule to run every 2 hours:
#!/bin/sh
set -x
timestamp() {
date +"at %H:%M:%S on %d/%m/%Y"
}
temp=$(git branch --show-current)
if [[ "$temp" = "backup" ]]
then
echo "error, cannot backup the backup branch"
exit -1
fi
git show-ref --verify --quiet refs/heads/backup
#0 if branch doesn't exist
if [$? == 0]
then
printf "BACKUP DOES NOT EXIST\n"
git stash
git stash branch backup
git checkout backup
git commit -am "Regular auto-commit $(timestamp)"
git push --set-upstream origin backup
git checkout $temp
git stash pop
else
printf "BACKUP EXISTS\n"
git stash
git stash branch temp-backup-branch
git commit -am "Regular auto-commit $(timestamp)"
git checkout backup
git merge -X theirs temp-backup-branch
git branch -D temp-backup-branch
git push --set-upstream origin backup
git checkout $temp
git stash pop
fi
I want to focus on the "else" part, since it is the most important part. Firsty I stash the changes from the branch I am on, then I create a new temporary branch using that stash. After that I commit the files and checkout my "backup" branch. I merge the temporary branch to my backup branch and delete the temporary one. After taht I push the changes to the repo. After that I return to the branch I was working on, and unstash the changes, but unfortunetely I get a merge error.
How is it even possible? My knowledge is probably lacking in this matter, but doesn't executing the command git stash just move my code to the stash and after that I am free to move the changes back to my branch? How is it possible that I even get a merge error, since I don't make any changes on my source branch? What should I do, so I don't have the merge error and what do you think about my solution to the "backup" problem?