2

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?

Nexon
  • 326
  • 1
  • 11
  • 4
    I'm going to be "that guy" and say that you should probably consider more comprehensive backup solutions that can cover all of your important data, not just your Git repos. – 0x5453 Aug 06 '21 at 21:24
  • Could you elaborate on this statement, "I didn't want to push into my normal branches on which I worked, because that would create a mess in my git log." – TTT Aug 06 '21 at 21:24
  • I ask because I push my *personal* branches at least once per day (usually at the end of the day if I haven't already), even with broken and unfinished commits (for which I prefix the commit message with "wip:"). When I'm in a better place I simply `rebase -i` and force push my branches out to the remote to clean them up before merging into a persistent branch. No mess, when I'm done. ;) – TTT Aug 06 '21 at 21:30
  • 1
    I agree with @0x5453. Git makes a half-half-decent (1/4-decent? 1/4-assed?) backup system. A real backup system is much better. (That aside, having some sort of auto-push isn't a *bad* idea.) – torek Aug 07 '21 at 00:16
  • @0x5453 I understand, that using a more comprehensive backup mechanizm would be better in some but: 1) I want this solution to be lighweight, so if I find a new computer, I'll just copy the script and put it into a scheduler 2) $ Automatic backup usually costs money – Nexon Aug 07 '21 at 07:30
  • @TTT, I just don't want to have a branch with thousands of commits. Sure I can squash them, but then I have to do this manually. I am not brave enough to trust myself about pushing my changes to github daily, besides, a good programmer is a lazy programmer ;). However if you guys have another proposition, for a backup system, I would also gladly read about it – Nexon Aug 07 '21 at 07:37
  • Also other than the idea of using this script as an backup mechanizm, the question "why do I get a merge error?" still stands, because I have no clue why – Nexon Aug 07 '21 at 07:39
  • @Nexon I don't follow what you mean about the thousands of commits. How often do you commit normally, and how often do you normally push? – TTT Aug 07 '21 at 19:45
  • @Nexon regarding the merge conflict, my guess is your command `git stash branch temp-backup-branch` is popping the stash, and then when you try to pop it again back to your original branch, maybe you're actually popping an older stash. You could confirm this by clearing your stash and running those commands. At your last pop command if you see "No stash entries found." then you know this is what's happening. – TTT Aug 07 '21 at 20:05
  • @TTT If I do automatic pushes and commits, I would do them frequently, like once per hour, so it would result with many many commit and pushes. If I do it manually, I rarely do it more than once per day. I tried to check what you wrote, but I made a discovery, when I did git stash, git stash branch temp-backup-branch, git checkout master I saw that the changes still were on that branch. When I stashed them and got back to my temp branch I saw that they were stashed also on that branch. I am puzzled, since I thought that each branch has it's own stash, but they seem to share it. – Nexon Aug 08 '21 at 07:30
  • 1
    @Nexon [this question](https://stackoverflow.com/q/20526355/184546) may help you. – TTT Aug 08 '21 at 19:43

0 Answers0