如何在 git 中备份私有分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/923956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to back up private branches in git
提问by Art
I have a local branch for day-to-day dev work in git. My workflow is:
我有一个本地分支,用于 git 中的日常开发工作。我的工作流程是:
- Do stuff on local_branch, commit
- Fetch origin/master
- Rebase local_branch to catch up with new stuff from origin/master
- 在 local_branch 上做事,提交
- 获取原点/主
- 重新定位 local_branch 以赶上来自 origin/master 的新东西
It all works fine, however most of the recommendations I encountered say that one should not "push" private branches, on which rebase is regularly performed.
一切都很好,但是我遇到的大多数建议都说不应该“推送”私有分支,在这些分支上定期执行 rebase。
The problem here is that in this case local branch is not backed up to a server and the only way to save the work is to merge it back to "pushable" branch (i.e. origin/master)
这里的问题是,在这种情况下,本地分支没有备份到服务器,保存工作的唯一方法是将其合并回“可推送”分支(即 origin/master)
What would be your recommendations on the workflow in this case?
在这种情况下,您对工作流程有什么建议?
Thanks!
谢谢!
UPDATE: I realised that one of the original requirements I had (avoiding usage of external utilities) is unnecessary limiting.
更新:我意识到我的原始要求之一(避免使用外部实用程序)是不必要的限制。
My current solution is to store all my repositories in a cloud-synchronised folder - this way I get backup for free.
我目前的解决方案是将我所有的存储库存储在一个云同步文件夹中 - 这样我就可以免费获得备份。
采纳答案by Pat Notz
I use the --mirror option and push to a personal backup repository:
我使用 --mirror 选项并推送到个人备份存储库:
Add it as a remote:
将其添加为遥控器:
git remote add bak server:/path/to/backup/repo
Do the backup:
做备份:
git push --mirror bak
This will automatically make your backup repository look like your active one -- branches will be created, deleted, updated (even forced/non-fastforwards) as needed. You can make an alias for this too:
这将自动使您的备份存储库看起来像您的活动存储库——将根据需要创建、删除、更新(甚至强制/非快进)分支。您也可以为此创建别名:
git config alias.bak "push --mirror bak"
Then, it's just a matter of running "git bak" when you want to do a backup. You could also throw this into a cron job.
然后,当您想要进行备份时,只需运行“git bak”即可。你也可以把它放到一个 cron 工作中。
回答by FelipeC
There's nothing wrong with pushing personal branches. It is generally discouraged because people might start work based on your branch, and when you rebase, their changes are left floating.
推个人分支没有错。通常不鼓励这样做,因为人们可能会根据您的分支开始工作,而当您变基时,他们的更改会保持浮动。
What I do is use a prefix to denote "this is my branch, use it on your own risk", like: fc-general-cleanup.
我所做的是使用前缀来表示“这是我的分支,使用它的风险由您自己承担”,例如:fc-general-cleanup。
回答by Dan Moulding
Another option would be to push "local_branch" to the "origin" repo, but to it's own branch in that repo (not "master"), i.e.:
另一种选择是将“local_branch”推送到“origin”repo,但推送到该repo中它自己的分支(不是“master”),即:
git push origin local_branch:local_backup
git push origin local_branch:local_backup
Then when you are ready to make another backup (and after you've been doing some work and rebasing) just delete the backup branch from the origin repo before you push it out again:
然后,当您准备好进行另一个备份时(并且在您进行了一些工作和变基之后),只需在再次推出之前从原始存储库中删除备份分支:
git push origin :local_backup
<=== deletes the branch from origin
git push origin :local_backup
<=== 从原点删除分支
git push origin local_branch:local_backup
git push origin local_branch:local_backup
This way you won't run into problems pushing "local_branch" after it has been rebased from "origin/master".
这样,在从“origin/master”重新定位后,您就不会遇到推送“local_branch”的问题。
And if deleting your backup branches makes you nervous (until after you've finally committed your work to "master"), you could always keep pushing to a new branch with a new name (e.g. "local_backup1", "local_backup2", etc).
如果删除你的备份分支让你感到紧张(直到你最终将你的工作提交给“master”),你可以一直推送到一个新名称的新分支(例如“local_backup1”、“local_backup2”等) .
回答by Cebjyre
Can you set up another remote repository that you do push all of your branches to? The other thing to consider is just backing up everything (important) on your local machine, including the git repo.
您可以设置另一个远程存储库,将所有分支推送到其中吗?另一件要考虑的事情是备份本地机器上的所有内容(重要的),包括 git repo。
回答by Dan Moulding
There is nothing wrong with pushing to the same branch that you are rebasing from. These diagrams should illustrate why this works fine:
推送到您要重新定位的同一分支并没有错。这些图表应该说明为什么这可以正常工作:
Lets say this is what the commit graph looks like after you've branched local_branch and made a couple of commits to it (C and D). Someone else has made one commit (E) to origin/master since you branched local_branch:
假设这是在您分支 local_branch 并对其进行了几次提交(C 和 D)之后提交图的样子。自从您分支 local_branch 以来,其他人已经向 origin/master 提交了一次提交 (E):
A -- B -- E [origin/master] \ \ \-- C -- D [local_branch]
Then after running "git rebase origin/master", the commit graph will look like the next diagram. "origin/master" is still the same, but "local_branch" has been rebased:
然后在运行“git rebase origin/master”之后,提交图将如下图所示。"origin/master" 仍然是一样的,但 "local_branch" 已经重新定位:
A -- B -- E [origin/master] \ \ \-- C -- D [local_branch]
At this stage, if you do "git push origin local_branch:master", then it will result in a simple fast-forward. "origin/master" and "local_branch" will be identical:
在这个阶段,如果你执行“git push origin local_branch:master”,那么它会导致一个简单的快进。“origin/master”和“local_branch”将是相同的:
A -- B -- E -- C -- D [origin/master],[local_branch]
Now you are free to do more work on "local_branch". Eventually you might get something like this:
现在您可以自由地在“local_branch”上做更多的工作。最终你可能会得到这样的东西:
A -- B -- E -- C -- D -- G -- I [origin/master] \ \ \-- F -- H [local_branch]
Notice this looks a lot like the starting graph. You can keep repeating this process over and over.
请注意,这看起来很像起始图。你可以一遍又一遍地重复这个过程。
You should avoid pushing to some otherbranch, one that you are not rebasing from. That is where you will run into trouble (to the other branch, it will look like your "local_branch's" history has suddenly been re-written, after you've rebased from "origin/master").
您应该避免推送到其他某个分支,即您未从其重新定位的分支。这就是你会遇到麻烦的地方(对于另一个分支,在你从“origin/master”重新定位后,你的“local_branch”历史看起来突然被重写了)。
回答by Don Branson
Here's what I do. But - this isn't private. I do this so that I can collaborate with myself (so to speak). It lets me work on the same branch on two or more boxes. if other people had access to the shared repo, they could see the work I'm doing on the branch. Of course, on my home repos, nobody else has access, so it's still private. On github, all the world could see my stuff. Like they really care. ;)
这就是我所做的。但是 - 这不是私人的。我这样做是为了我可以与自己合作(可以这么说)。它让我可以在两个或多个盒子上的同一个分支上工作。如果其他人可以访问共享存储库,他们可以看到我在分支上所做的工作。当然,在我的家庭存储库中,没有其他人可以访问,所以它仍然是私有的。在github上,全世界都可以看到我的东西。就像他们真的很在乎一样。;)
回答by VonC
Rather than relying on Dropbox to synchronize allthe files of a git repo, I would rather use git bundle
, which produces only onefile (including all your private branches), and sync that file with DropBox.
See "Git with Dropbox"
与其依赖 Dropbox 来同步git repo 的所有文件,我宁愿使用git bundle
,它只生成一个文件(包括您的所有私有分支),并将该文件与 DropBox 同步。
参见“带有 Dropbox 的 Git”
In "Backup a Local Git Repository", Yarsmentioned having sync errors with Dropbox.
在“备份本地 Git 存储库”中,Yars提到了与 Dropbox 的同步错误。