How can I set up Git branch for automatic backups

Selali Adobor

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.

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.

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".

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".

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.

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)

Mikko Rantalainen

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.

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.

Links: http://alx.github.io/gitbook/7_raw_git.html

Update:

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...)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Flycheck and Clutter - how can I set it up?

분류에서Dev

How can I symlink to files in a specific Git ref/branch?

분류에서Dev

In git, how can I create an 'isolate branch' (act just the same as master branch)?

분류에서Dev

How can I set up logging for node-mongod-native?

분류에서Dev

How can I set up conditional associations in Rails?

분류에서Dev

How can I set up a house-wide sound system?

분류에서Dev

How do I check if git branch has a tracking branch?

분류에서Dev

Rails - how to start up a local server (thin) with a specific git branch

분류에서Dev

How to set up git for wordpress theme development

분류에서Dev

How can I set up a Site-to-site VPN between old and new versions of pfSense?

분류에서Dev

How can one prevent a git branch from being merged into another?

분류에서Dev

How do I set up a server for SSH?

분류에서Dev

How do I set up an email server?

분류에서Dev

How can I reset tmux's automatic session name numbering?

분류에서Dev

How can I get automatic screen rotation with GNOME?

분류에서Dev

How can I enable automatic resume on lid open?

분류에서Dev

How can I force git to prompt me for the email address to use, if not set in the repo config?

분류에서Dev

How can I create a feature branch from code changes in my development branch that I haven't checked in?

분류에서Dev

how to set up username and passwords for different git repos?

분류에서Dev

Can I set up a remote connection to Mongo on cloud nine?

분류에서Dev

Set Up SSH Agent for git

분류에서Dev

How do I remove "ghost" backups from Time Machine?

분류에서Dev

How to undo git merge branch

분류에서Dev

How to set Terminal tab title to show the current git branch (using zsh)?

분류에서Dev

How can I import bzr branches into git?

분류에서Dev

How do I set up a bash alias for a common working folder?

분류에서Dev

Can't remove remote branch in git

분류에서Dev

How can I set Crontab for a specific hour?

분류에서Dev

how can I set the position of the element?

Related 관련 기사

  1. 1

    Flycheck and Clutter - how can I set it up?

  2. 2

    How can I symlink to files in a specific Git ref/branch?

  3. 3

    In git, how can I create an 'isolate branch' (act just the same as master branch)?

  4. 4

    How can I set up logging for node-mongod-native?

  5. 5

    How can I set up conditional associations in Rails?

  6. 6

    How can I set up a house-wide sound system?

  7. 7

    How do I check if git branch has a tracking branch?

  8. 8

    Rails - how to start up a local server (thin) with a specific git branch

  9. 9

    How to set up git for wordpress theme development

  10. 10

    How can I set up a Site-to-site VPN between old and new versions of pfSense?

  11. 11

    How can one prevent a git branch from being merged into another?

  12. 12

    How do I set up a server for SSH?

  13. 13

    How do I set up an email server?

  14. 14

    How can I reset tmux's automatic session name numbering?

  15. 15

    How can I get automatic screen rotation with GNOME?

  16. 16

    How can I enable automatic resume on lid open?

  17. 17

    How can I force git to prompt me for the email address to use, if not set in the repo config?

  18. 18

    How can I create a feature branch from code changes in my development branch that I haven't checked in?

  19. 19

    how to set up username and passwords for different git repos?

  20. 20

    Can I set up a remote connection to Mongo on cloud nine?

  21. 21

    Set Up SSH Agent for git

  22. 22

    How do I remove "ghost" backups from Time Machine?

  23. 23

    How to undo git merge branch

  24. 24

    How to set Terminal tab title to show the current git branch (using zsh)?

  25. 25

    How can I import bzr branches into git?

  26. 26

    How do I set up a bash alias for a common working folder?

  27. 27

    Can't remove remote branch in git

  28. 28

    How can I set Crontab for a specific hour?

  29. 29

    how can I set the position of the element?

뜨겁다태그

보관