Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically stashing

The section Last links in the chain: Stashing and the reflog in http://ftp.newartisans.com/pub/git.from.bottom.up.pdf recommends stashing often to take snapshots of your work in progress. The author goes as far as recommending that you can use a cron job to stash your work regularly, without having to do a stash manually.

The beauty of stash is that it lets you apply unobtrusive version control to your working process itself: namely, the various stages of your working tree from day to day. You can even use stash on a regular basis if you like, with something like the following snapshot script:

$ cat <<EOF > /usr/local/bin/git-snapshot
#!/bin/sh
git stash && git stash apply
EOF
$ chmod +x $_
$ git snapshot

There’s no reason you couldn’t run this from a cron job every hour, along with running the reflog expire command every week or month.

The problem with this approach is:

  1. If there are no changes to your working copy, the "git stash apply" will cause your last stash to be applied over your working copy.
  2. There could be race conditions between when the cron job executes and the user working on the working copy. For example, "git stash" runs, then the user opens the file, then the script's "git stash apply" is executed.

Does anybody have suggestions for making this automatic stashing work more reliably?

like image 239
readonly Avatar asked Nov 17 '08 21:11

readonly


People also ask

What is auto-Stash deposit?

Auto-Stash is a suite of money-saving tools that allows users to save and invest automatically, and we at Stash think they are a few of our most important financial tools. Auto-Stash takes the weight off your shoulders by putting your savings on autopilot—it will invest your money, and you won't have to lift a finger.

How do you get to auto-Stash?

To adjust Set Schedule on the app: To change the amount Go to Auto-Stash (tab on iOS, menu item on Android) Select Set Schedule Choose your portfolio Tap the investment you want to change Enter the amount you want to set Select Save to confirm your change To change the frequency Go to Auto-Stash (tab on iOS,…

How do you use auto-Stash?

Enroll in Round-Ups, and every time you spend with the debit card associated with your linked checking account, Stash will round up your purchase to the nearest dollar. For example, if you spend $10.50, we'll automatically round up your purchase by $. 50.

Does Stash have a automatically invest for you?

Stash offers access to a banking account with no overdraft fees or minimum balance through Green Dot Bank. It lets you save automatically using roundups and auto-invest, and it also lets you set goals by separating your cash into spaces for specific purposes.


2 Answers

I certainly wouldn't set up automatic stashing as described in that (otherwise excellent) article, for exactly the reasons you cite.

I prefer to use the stash as it is intended to be used, where I deliberately stash and apply changes as I'm working. For periodic backups, I use a proper backup solution. In my opinion, Git is not a substitute for a backup solution.

like image 72
Greg Hewgill Avatar answered Nov 03 '22 01:11

Greg Hewgill


git stash is actually just a little shell script that creates a commit which is not referenced in any branch. You could emulate this behaviour without race conditions:

#!/bin/sh
GIT_DIR=$(git rev-parse --git-dir) || exit
ref_stash=refs/stash

w_commit=$(git stash create) # creates a commit for the wip

# gather some info
head=$(git log --no-color --abbrev-commit --pretty=oneline -n 1 HEAD --)
branch=$(git symbolic-ref -q HEAD)
branch=${branch#refs/heads/}
msg=$(printf 'WIP on %s: %s' "$branch" "$head")

# Make sure the reflog for stash is kept.
: >>"$GIT_DIR/logs/$ref_stash"

git update-ref -m "$msg" $ref_stash $w_commit

The script may need some polishing but I hope you get the idea :)

like image 28
d0k Avatar answered Nov 03 '22 01:11

d0k