Git和Dropbox可以一起使用吗?
当前回答
另一种方法:
到目前为止,所有的答案,包括最受欢迎的@Dan的答案,都提出了使用Dropbox来集中共享存储库的想法,而不是使用专注于git的服务,如github, bitbucket等。
但是,由于最初的问题并没有具体说明“有效地使用Git和Dropbox”的真正含义,让我们研究另一种方法: “使用Dropbox仅同步工作树。”
操作步骤如下:
在项目目录中,创建一个空的.git目录(例如mkdir -p myproject/.git) 取消同步Dropbox中的。git目录。如果使用Dropbox应用程序:进入“首选项”,“同步”,“选择要同步的文件夹”,其中。git目录需要去掉标记。这将删除.git目录。 在工程目录下运行git init
如果.git已经存在,它也可以工作,那么只执行步骤2。Dropbox会在网站上保留一份git文件的副本。
步骤2将导致Dropbox不同步git系统结构,这是这种方法的预期结果。
为什么要使用这种方法呢?
尚未发布的更改将在Dropbox上备份,并在不同设备之间同步。 如果Dropbox在设备之间同步时搞砸了,git状态和git diff将很方便地整理事情。 它节省了Dropbox帐户的空间(整个历史将不会存储在那里) 它避免了@dubek和@Ates在对@Dan的回答的评论中提出的担忧,以及@clu在另一个回答中提出的担忧。
其他地方的远程存在(github等)将很好地使用这种方法。
在不同的分支上工作会带来一些问题,需要注意:
一个潜在的问题是,当用户签出不同的分支时,Dropbox(不必要的?)可能会同步许多文件。 如果两个或多个Dropbox同步设备签出了不同的分支,对两个设备未提交的更改可能会丢失,
解决这些问题的一种方法是使用git工作树将分支签出保存在单独的目录中。
其他回答
我认为Dropbox上的Git很棒。我一直在用它。我有多台电脑(两台在家里,一台在公司),我把Dropbox作为一个中央的存储库。因为我不想把它托管在公共服务上,而且我也没有可以通过SSH访问的服务器,Dropbox通过在后台同步来解决这个问题(非常快)。
设置是这样的:
~/project $ git init
~/project $ git add .
~/project $ git commit -m "first commit"
~/project $ cd ~/Dropbox/git
~/Dropbox/git $ git init --bare project.git
~/Dropbox/git $ cd ~/project
~/project $ git remote add origin ~/Dropbox/git/project.git
~/project $ git push -u origin master
从那里,你可以克隆~/Dropbox/git/项目。git目录(无论它是否属于你的Dropbox帐户或在多个帐户之间共享)并执行所有正常的git操作-它们将自动同步到你所有的其他机器。
我写了一篇博客文章“关于版本控制”,其中介绍了环境设置背后的原因。它基于我的Ruby on Rails开发经验,但实际上它可以应用于任何东西。
还有一个开源项目(一个跨平台[Linux, Mac, Win]脚本的集合),它用少量(3-4)命令完成存储库管理的所有细节。
https://github.com/karalabe/gitbox/wiki
示例用法如下:
$ gitbox create myapp
Creating empty repository...
Initializing new repository...
Repository successfully created.
$ gitbox clone myapp
Cloning repository...
Repository successfully cloned.
之后正常的git使用:
$ echo “Some change” > somefile.txt
$ git add somefile.txt
$ git commit –m “Created some file”
$ git push
查看项目wiki和手册以获得完整的命令参考和教程。
在我看来,Dropbox只适用于个人用途,因为你不想麻烦地获得一个中央回收主机。对于任何专业开发来说,你可能会制造更多的问题,而不是解决更多的问题,就像已经在帖子中多次提到的那样,Dropbox并不是为这个用例而设计的。也就是说,在没有任何第三方插件或工具的情况下,在Dropbox上转储存储库的一个非常安全的方法是使用捆绑包。我在我的.gitconfig中有以下别名以节省输入:
[alias]
bundle-push = "!cd \"${GIT_PREFIX:-.}\" && if path=\"$(git config remote.\"$1\".url)\" && [ \"${path:0:1}\" = / ]; then git bundle create \"$path\" --all && git fetch \"$1\"; else echo \"Not a bundle remote\"; exit 1; fi #"
bundle-fetch = "!cd \"${GIT_PREFIX:-.}\" && if path=\"$(git config remote.\"$1\".url)\" && [ \"${path:0:1}\" = / ]; then git bundle verify \"$path\" && git fetch \"$1\"; else echo \"Not a bundle remote\"; exit 1; fi #"
bundle-new = "!cd \"${GIT_PREFIX:-.}\" && if [ -z \"${1:-}\" -o -z \"${2:-}\" ]; then echo \"Usage: git bundle-new <file> <remote name>\"; exit 1; elif [ -e \"$2\" ]; then echo \"File exist\"; exit 1; else git bundle create \"$2\" --all && git remote add -f \"$1\" \"$(realpath \"$2\")\"; fi #"
例子:
# Create bundle remote (in local repo)
$ git bundle-new dropbox ~/Dropbox/my-repo.bundle
# Fetch updates from dropbox
$ git bundle-fetch dropbox
# NOTE: writes over previous bundle. Thus, roughly equivalent to push --force --prune --all
$ git bundle-push
正确的方法是使用git-remote-dropbox: https://github.com/anishathalye/git-remote-dropbox
在Dropbox中创建自己的裸回购会导致很多问题。Anish(库的创造者)解释得很好:
The root cause of these problems is that the Dropbox desktop client is designed for syncing files, not Git repositories. Without special handling for Git repositories, it doesn’t maintain the same guarantees as Git. Operations on the remote repository are no longer atomic, and concurrent operations or unlucky timing with synchronization can result in a corrupted repository. Traditional Git remotes run code on the server side to make this work properly, but we can’t do that. Solution: It is possible to solve this properly. It is possible to use Git with Dropbox and have the same safety and consistency guarantees as a traditional Git remote, even when there are multiple users and concurrent operations! For a user, it’s as simple as using git-remote-dropbox, a Git remote helper that acts as a transparent bidirectional bridge between Git and Dropbox and maintains all the guarantees of a traditional Git remote. It’s even safe to use with shared folders, so it can be used for collaboration (yay unlimited private repos with unlimited collaborators!). With the remote helper, it’s possible to use Dropbox as a Git remote and continue using all the regular Git commands like git clone, git pull, and git push, and everything will just work as expected.
我不认为使用Git和Dropbox是正确的选择……想想两者的特点:
Git:
允许您拥有一个中央存储库 允许您使用自己的更改拥有自己的存储库 允许您从中央存储库发送和接收更改 允许多人更改相同的文件,他们合并他们或要求你合并他们,如果它不能这样做 web和桌面客户端是否允许访问中央存储库
Dropbox:
将所有内容保存在中央存储库中 允许您在服务器中拥有自己的文件版本 强制您从中央存储库发送和接收更改 如果多人更改相同的文件,第一个提交的文件将被稍后提交的文件所取代,并且不会发生合并,这很麻烦(这肯定是它最大的缺点) 具有web和桌面客户端以允许访问中央存储库。
如果你担心泄露你的文件,为什么不加密呢?然后你就可以得到Dropbox to Git的最大优势,那就是拥有公共和私人文件……