如何设置Git分支进行自动备份 [英] How can I set up Git branch for automatic backups

查看:112
本文介绍了如何设置Git分支进行自动备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到关于如何备份存储库已经存在疑问(答案通常是 git bundle ),但是我的目标是特定的设置.

I realize there are already questions on how to back up a repository (and the answer is usually git bundle), but I'd aiming specific setup.

我试图定期将当前分支的快照"提交给另一个分支,以进行备份.我正在使用一个批处理文件,它看起来像这样(我尝试了更多的变体,超出了此处列出的范围):

I'd trying to periodically commit a "snapshot" of the current branch to another branch for backup purposes. I'm using a batch file, and it looks like this (I've tried more variations than I can list here):

git stash
git checkout backup
git add .
git commit -m "Automatic Backup  %Time%"  
git push --all origin
git stash pop
git checkout -

我想要的行为是每次提交都应该是目录当前状态的准确快照,而不管我在运行该目录时签出了哪个分支.如果较新的提交更改了文件,则希望在发生合并冲突时优先使用该文件.

The behavior I'm aiming for is that each commit should be an exact snapshot of the current state of the directory, regardless of what branch I have checked out when I run it. If a newer commit changes a file, I want it to take precedence if a merge conflict arises.

我实际上不是在尝试合并分支,我只是在尝试获取磁盘快照时存在的文件.因此,如果分支A在文件A上有更改,而分支B在同一文件上有冲突的更改,但是分支B已检出,则备份分支应该最终得到分支B的更改,这些更改当前在磁盘上的文件中,忽略其他任何内容.

I'm not actually trying to merge branches, I'm just trying to get the files as they exist at the time of the snapshot on the disk. So if branch A has a change on file A, and branch B has a conflicting change on the same file, but branch B is checked out, the backup branch should end up getting branch B's changes, which are currently in the file on the disk, ignoring anything else.

以及我要尝试做的一个实际(?)示例:假设分支A的"myfile.txt"为"Hello World",分支B的"myfile.txt"为"Hello Dave".

And a practical(?) example of what I'm trying to do: Say Branch A has "myfile.txt" as "Hello World", and Branch B has "myfile.txt" as "Hello Dave".

当我结帐分支A并在文本编辑器中打开"myfile.txt"时,我希望它会有"Hello World".结帐B时,我希望它有"Hello Dave"字样.

When I checkout Branch A and open "myfile.txt" in a text editor I expect it'd have "Hello World". When I checkout B, I'd expect it to have "Hello Dave".

如果在一个分支中,提交1具有"Hello World",而提交2具有"Hello Dave",则不会发生冲突.我希望我的备份分支最终以提交1包含"Hello World",并以提交2包含"Hello Dave"为前提,假设提交1是在我先前签出分支A时发生的,而分支B是在提交2时发生的.

If within one branch commit 1 had "Hello World", and 2 had "Hello Dave" there wouldn't be a conflict. I want my back up branch to end up with commit 1 containing "Hello World", and commit 2 containing "Hello Dave" assuming commit 1 occurred while I had previously checked out branch A, and branch B when commit 2 occurred.

相信 git stash是我正在做的事情的关键,但它根本无法正常工作.我尝试了这些命令的几种不同组合,当回购处于各种状态时,它们都在错误的不同点返回了不同的错误变化,因此很难总结它们.我会说我的方法可能根本上是错误的,因此列出的命令只是为了概述我到目前为止已经尝试过的内容.

I believe git stash is the key to what I'm doing, but it simply isn't working. I tried several different combinations of those commands, and all of them returned different variations off errors, at different points while the repo was in various states, so it's really hard to summarize them. I'd say my approach is probably fundamentally wrong, so the commands listed are there just to give a picture of what I've tried so far.

但是无论我做什么,我要么会遇到合并冲突,要么什么都做不到.我想念什么?(如果我可以提供其他任何信息,请告诉我)

But no matter what I do I either get merge conflicts or nothing gets committed. What am I missing? (If there's any additional information I can provide, please let me know)

推荐答案

在我看来,您想自动备份工作目录的内容作为 backup 中的新提交code>分支并将所有分支推送到 origin .

It seems to me that you want to automatically backup the working directory contents as a new commit in the backup branch and the push all the branches to origin.

在那种情况下,您不想 checkout backup 分支,因为那样会更改您的工作目录内容.相反,您需要使用 git read-tree git update-index git commit-tree 的某种组合来构造新的提交 backup 分支,然后使用 git branch -f 将新制作的提交添加到 backup 分支.之后,您可以继续对 origin 进行常规推送.

In that case, you do not want to checkout the backup branch because that would change your working directory contents. Instead, you'll want to use some combination of git read-tree, git update-index and git commit-tree to fabricate a new commit for your backup branch and then use git branch -f to add the newly fabricated commit to the backup branch. After that, you can continue to do the regular push to origin.

链接: http://alx.github.io/gitbook/7_raw_git.html

更新:

我认为最简单的方法是这样的(假设您已经有一个名为 backup 的分支):

I think the easiest way to do this is like this (assuming you already have a branch called backup):

#!/bin/bash
BRANCH=backup
export GIT_INDEX_FILE=/tmp/git-backup-index.$$
git add .
git commit-tree $(git write-tree) -p $(git show-ref --hash --heads $BRANCH) -m "Automatic backup" | xargs git branch -f $BRANCH
rm -f "$GIT_INDEX_FILE"

(请注意,我尚未测试以上脚本...)

(Note that I haven't tested the above script...)

这篇关于如何设置Git分支进行自动备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆