Git和Dropbox可以一起使用吗?


当前回答

我喜欢丹·麦克内文投票最多的回答。我最后做了太多次git命令序列,决定做一个脚本。就是这样:

#!/bin/bash

# Usage
usage() {
    echo "Usage: ${0} -m [ master-branch-directory ] -r [ remote-branch-directory ] [ project-name ]"
    exit 1
}

# Defaults
defaults() {
    masterdir="${HOME}/Dropbox/git"
    remotedir="${PWD}"
    gitignorefile="# OS generated files #\n\n.DS_Store\n.DS_Store?\n.Spotlight-V100\n.Trashes\nehthumbs.db\nThumbs.db"
}

# Check if no arguments
if [ ${#} -eq 0 ] ; then
    echo "Error: No arguments specified"
    usage
fi

#Set defaults
defaults

# Parse arguments
while [ ${#} -ge 1 ]; do
    case "${1}" in
        '-h' | '--help' ) usage ;;
        '-m' )
            shift
            masterdir="${1}"
            ;;
        '-r' )
            shift
            remotedir="${1}"
            ;;
        * )
            projectname="${1##*/}"
            projectname="${projectname%.git}.git"
            ;;
    esac
    shift
done

# check if specified directories and project name exists
if [ -z "${projectname}" ]; then
    echo "Error: Project name not specified"
    usage
fi

if [ ! -d "${remotedir}" ]; then
    echo "Error: Remote directory ${remotedir} does not exist"
    usage
fi

if [ ! -d "${masterdir}" ]; then
    echo "Error: Master directory ${masterdir} does not exist"
    usage
fi

#absolute paths
remotedir="`( cd \"${remotedir}\" && pwd )`"
masterdir="`( cd \"${masterdir}\" && pwd )`"

#Make master git repository
cd "${masterdir}"
git init --bare "${projectname}"

#make local repository and push to master
cd "${remotedir}"
echo -e "${gitignorefile}" > .gitignore # default .gitignore file
git init
git add .
git commit -m "first commit"
git remote add origin "${masterdir}/${projectname}"
git push -u origin master

#done
echo "----- Locations -----"
echo "Remote branch location: ${remotedir}"
echo "Master branch location: ${masterdir}"
echo "Project Name: ${projectname}"

该脚本只需要一个项目名称。它将在~/Dropbox/git/中生成一个指定名称下的git存储库,并将当前目录的全部内容推送到新创建的origin主分支。如果给出了多个项目名称,则将使用最右边的项目名称参数。

可选地,-r命令参数指定将推送到源主机的远程分支。项目起源主节点的位置也可以用-m参数指定。一个默认的.gitignore文件也放置在远程分支目录中。目录和.gitignore文件默认值在脚本中指定。

其他回答

我认为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开发经验,但实际上它可以应用于任何东西。

对于使用Dropbox的小型团队:

如果每个开发人员在Dropbox上都有自己的可写裸存储库,这只对其他开发人员开放,那么这就有利于代码共享而没有损坏的风险!

然后,如果你想要一个集中的“主线”,你可以让一个开发人员从自己的回购中管理所有推送。

现在是2015年,就在三天前,一个基于Dropbox API v2的新工具已经被创建出来,可以安全地在Dropbox上使用git。它针对API工作,而不是使用桌面客户端,并正确地处理多个同时推送到共享文件夹中托管的存储库。

一旦配置,它允许设置一个完全像其他git远程一样的git远程。

git clone "dropbox::/path/to/repo"
git remote add origin "dropbox::/path/to/repo"

我也遇到过类似的问题,并为此创建了一个小脚本。我们的想法是尽可能简单地使用Dropbox和Git。目前,我已经快速实现了Ruby代码,不久将添加更多代码。

该脚本可在https://github.com/nuttylabs/box-git上访问。

这个答案是基于Mercurial的经验,而不是Git,但这个经验告诉我们,如果你在不同的时间从不同的机器(对我来说是Mac、Unix和Windows)更新相同的基于Dropbox的存储库,那么以这种方式使用Dropbox会导致存储库损坏。

I don't have a complete list of the things that can go wrong, but here's a specific example that bit me. Each machine has its own notion of line-ending characters and how upper/lower case characters are handled in file names. Dropbox and Git/Mercurial handle this slightly differently (I don't recall the exact differences). If Dropbox updates the repository behind Git/Mercurial's back, presto, broken repository. This happens immediately and invisibly, so you don't even know your repository is broken until you try to recover something from it.

在用这种方法解决了一次混乱之后,我一直在使用下面的食谱,而且非常成功,没有任何问题的迹象。只需将存储库移出Dropbox。其他事情都用Dropbox;文档,JAR文件,任何你喜欢的东西。并使用GitHub (Git)或Bitbucket (Mercurial)来管理存储库本身。这两种工具都是免费的,所以不会增加成本,而且现在每种工具都发挥了自己的优势。

在Dropbox上运行Git/Mercurial除了风险什么都不会增加。不要这样做。