自动存储

Rea*_*nly 9 git version-control

本节最后环节链:积攒和引用日志http://ftp.newartisans.com/pub/git.from.bottom.up.pdf建议经常积攒采取进步工作的快照.作者建议您可以使用cron作业定期存储您的工作,而无需手动执行存储.

存储的美妙之处在于它允许您将不显眼的版本控制应用于您的工作过程本身:即,您的工作树的各个阶段,每天.如果您愿意,您甚至可以定期使用存储,例如以下快照脚本:

$ cat <<EOF > /usr/local/bin/git-snapshot
#!/bin/sh
git stash && git stash apply
EOF
$ chmod +x $_
$ git snapshot
Run Code Online (Sandbox Code Playgroud)

没有理由你不能每小时从一个cron作业运行它,以及每周或每月运行reflog expire命令.

这种方法的问题是:

  1. 如果您的工作副本没有变化,"git stash apply"将导致您的上一个藏匿点应用于您的工作副本.
  2. 在执行cron作业和处理工作副本的用户之间可能存在竞争条件.例如,运行"git stash",然后用户打开文件,然后执行脚本的"git stash apply".

有没有人建议让这种自动存储工作更可靠?

Gre*_*ill 13

我肯定不会像那篇(其他优秀的)文章中描述的那样设置自动存储,原因就在于你引用的原因.

我更喜欢使用stash,因为它是打算使用的,我故意藏匿并在我工作时应用更改.对于定期备份,我使用适当的备份解决方案.在我看来,Git不能替代备份解决方案.


d0k*_*d0k 8

git stash实际上只是一个小shell脚本,它创建了一个未在任何分支中引用的提交.您可以在没有竞争条件的情况下模拟此行为:

#!/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
Run Code Online (Sandbox Code Playgroud)

脚本可能需要一些抛光,但我希望你明白:)