有效地使用Git和Dropbox?

时间:2009-12-25 08:41:52

标签: git version-control cloud dropbox

如何有效地使用GitDropbox

19 个答案:

答案 0 :(得分:1371)

我认为Dropbox上的Git很棒。我一直都在使用它。我有多台计算机(两台在家,一台在工作),我使用Dropbox作为中央裸存储库。由于我不想在公共服务上托管它,并且我无法访问我总是可以访问的服务器,因此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帐户关联的~/Dropbox/git/project.git(或者与人共享此目录),您可以执行所有正常的Git操作,它们将与您的所有其他操作同步机器自动。

我写了一篇博文, On Version Control ,(old link dead )关于我的推理以及我如何设置我的环境,它是基于在我的Ruby on Rails开发经验中,它可以应用于任何事情,真的。

答案 1 :(得分:112)

正确的方法是使用git-remote-dropbox:https://github.com/anishathalye/git-remote-dropbox

在Dropbox中创建自己的裸仓库会导致很多问题。 Anish(图书馆的创建者)explains it best

  

这些问题的根本原因是Dropbox桌面   客户端设计用于同步文件,而不是Git存储库。没有   对Git存储库的特殊处理,它不保持相同   保证为Git。远程存储库上的操作不再存在   原子,并发操作或不幸的时机   同步可能导致存储库损坏。

     

传统的Git遥控器在服务器端运行代码以使其工作   没错,但我们做不到。

     

解决方案:可以正确解决此问题。可以使用   使用Dropbox的Git具有相同的安全性和一致性保证   作为传统的Git遥控器,即使有多个用户和   并发操作!

     

对于用户来说,它就像使用git-remote-dropbox一样简单   作为Git和。之间透明双向桥梁的助手   Dropbox并保持传统Git遥控器的所有保证。   它甚至可以安全地与共享文件夹一起使用,因此它可以用于   合作(无限制的无限私人回购   合作者!)。

     

使用远程助手,可以将Dropbox用作Git遥控器   并继续使用所有常规Git命令,如git clone,git   拉,git push,一切都会按预期工作。

答案 2 :(得分:87)

这个答案基于Mercurial经验,而不是Git,但是这种经验表明,如果甚至有可能会从不同的基础上更新相同的基于Dropbox的存储库,那么以这种方式使用Dropbox会要求提供损坏的存储库不同时间的机器(Mac,Unix,Windows)。

我没有可能出错的完整列表,但这里有一个特定的例子。每台机器都有自己的行尾字符概念,以及如何在文件名中处理大写/小写字符。 Dropbox和Git / Mercurial对此略有不同(我不记得确切的差异)。如果Dropbox更新Git / Mercurial后面的存储库,那么presto就会破坏存储库。这种情况立即发生,并且不可见,所以在您尝试从中恢复之前,您甚至不知道您的存储库已损坏。

在从一个乱糟糟的东西中挖掘出这种方式后,我一直在使用以下配方并取得了巨大的成功而没有任何问题的迹象。只需将您的存储库移出Dropbox即可。将Dropbox用于其他一切;文档,JAR files,您喜欢的任何内容。并使用GitHub(Git)或Bitbucket(Mercurial)来管理存储库本身。两者都是免费的,所以这不会增加成本,现在每个工具都发挥其优势。

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

答案 3 :(得分:16)

我不想将我的所有项目都放在一个Git存储库中,也不想进入并为每个项目运行此代码,因此我创建了一个Bash脚本来自动化该过程。你可以在一个或多个目录上使用它 - 所以它可以为你做这篇文章中的代码,或者它可以一次在多个项目上完成。

#!/bin/sh
# Script by Eli Delventhal
# Creates Git projects for file folders by making the origin Dropbox. You will need to install Dropbox for this to work.

# Not enough parameters, show help.
if [ $# -lt 1 ] ; then

cat<<HELP
projects_to_git.sh -- Takes a project folder and creates a Git repository for it on Dropbox

USAGE:
    ./projects_to_git.sh file1 file2 ..

EXAMPLES:
    ./projects_to_git.sh path/to/MyProjectDir
        Creates a git project called MyProjectDir on Dropbox

    ./projects_to_git.sh path/to/workspace/*
        Creates a git project on Dropbox for every folder contained within the workspace directory, where the project name matches the folder name

HELP
    exit 0
fi

# We have enough parameters, so let's actually do this thing.

START_DIR=$(pwd)

# Make sure we have a connection to Dropbox
cd ~
if [ -s 'Dropbox' ] ; then
    echo "Found Dropbox directory."
    cd Dropbox
    if [ -s 'git' ] ; then
        echo "    Dropbox Git directory found."
    else
        echo "    Dropbox Git directory created."
        mkdir git
    fi
else
    echo "You do not have a Dropbox folder at ~/Dropbox! Install Dropbox. Aborting..."
    exit 0
fi

# Process all directories matching the passed parameters.
echo "Starting processing for all files..."
for PROJ in $*
do
    if [ -d $PROJ ] ; then
        PROJNAME=$(basename $PROJ)
        echo "  Processing $PROJNAME..."

        # Enable Git with this project.
        cd $PROJ
        if [ -s '.git' ] ; then
            echo "    $PROJNAME is already a Git repository, ignoring..."
        else
            echo "    Initializing Git for $PROJNAME..."
            git init -q
            git add .
            git commit -m "Initial creation of project." -q

            # Make the origin Dropbox.

            cd ~/Dropbox/git
            if [ -s $PROJNAME ] ; then
                echo "    Warning! $PROJNAME already exists in Git! Ignoring..."
            else
                echo "    Putting $PROJNAME project on Dropbox..."
                mkdir $PROJNAME
                cd $PROJNAME
                git init -q --bare
            fi

            # Link the project to the origin
            echo "    Copying local $PROJNAME to Dropbox..."
            cd $PROJ
            git remote add origin "~/Dropbox/git/$PROJNAME"
            git push -q origin master
            git branch --set-upstream master origin/master
        fi
    fi
done

echo "Done processing all files."
cd $START_DIR

答案 4 :(得分:16)

关于使用Dropbox的小团队:

如果每个开发人员在Dropbox上都有自己的可写裸存储库,只拉给其他开发人员,那么这有助于代码共享而不会有腐败风险!

然后,如果您想要一个集中的“主线”,您可以让一个开发人员通过他们自己的仓库管理所有推送。

答案 5 :(得分:15)

我不认为使用Git和Dropbox是可行的方法......只要想想两者的特点:

GIT中:

  • 允许您拥有中央存储库
  • 允许您拥有自己的存储库并拥有自己的更改
  • 允许您从中央存储库发送和接收更改
  • 允许多人更改相同的文件并将它们合并或要求您合并它们如果无法合并
  • 允许Web和桌面客户端访问中央存储库

收存箱:

  • 将所有内容保存在中央存储库中
  • 允许您在服务器中拥有自己的文件版本
  • 强制您从中央存储库发送和接收更改
  • 如果多个人更改了相同的文件,则提交的第一个文件将替换为以后的提交,并且不会发生合并,这很麻烦(当然也是最大的缺点)
  • 允许Web和桌面客户端访问中央存储库。

如果您担心共享某些文件,为什么不加密它们呢?然后你可以获得Dropbox给Git的最大优势,就是拥有公共和私人文件......

答案 6 :(得分:15)

现在是2015年,截至三天前,已创建基于new toolDropbox API v2以安全地在Dropbox上使用git。它适用于API,而不是使用桌面客户​​端,并正确处理多个同时推送到共享文件夹中托管的存储库。

配置好后,就可以像任何其他git遥控器一样设置一个git遥控器。

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

答案 7 :(得分:9)

我使用Mercurial(或Git)+ TrueCrypt + Dropbox进行加密远程备份

最酷的是,如果您修改了一小部分代码,Dropbox不会同步整个TrueCrypt容器。同步时间大致与更改量成比例。即使它是加密的,TrueCrypt + Dropbox的组合也可以很好地利用分组密码+块级同步。

其次,单片加密容器不仅增加了安全性,还减少了存储库corruption的机会。

警告:但是,在Dropbox运行时,您必须非常小心未安装容器。如果2个不同的客户端为容器签入不同的版本,解决冲突也是一种痛苦。因此,它仅适用于使用它进行备份的单个人,而不适用于团队。

设定:

  • 创建一个Truecrypt容器(多个技嘉就可以了)
  • 在Truecrypt偏好设置下,取消选中preserve modification timestamp *。
  • 如上所述由Dan(https://stackoverflow.com/a/1961515/781695
  • 创建一个回购

用法:

  • 退出Dropbox
  • 装载容器,推送更改,卸载
  • 运行dropbox

P.S。取消选中preserve modification timestamp会告知保管箱文件已被修改且应该同步。请注意,即使您不更改其中的任何文件,安装容器也会修改时间戳。如果您不希望发生这种情况,只需将卷安装为read-only

即可

答案 8 :(得分:6)

我一直以推荐的方式使用Mercurial,并敦促您保持谨慎,特别是如果任何机器不同的话。 Dropbox论坛充满了对自发出现的神秘文件名案例问题的抱怨。 Hg(我认为Git)在常规签到期间不会注意到或抱怨,并且当你试图将它用于真实时它会抱怨腐败的回购时你只能听到腐败。坏消息。希望我能更具体地解决这个问题及其解决方法;我还在努力从这个烂摊子中挖掘出来。

答案 9 :(得分:6)

我喜欢Dan McNevin的回答!我现在也在一起使用Git和Dropbox,我在.bash_profile中使用了几个别名,所以我的工作流程如下:

~/project $ git init
~/project $ git add .
~/project $ gcam "first commit"
~/project $ git-dropbox

这些是我的别名:

alias gcam='git commit -a -m'
alias gpom='git push origin master'
alias gra='git remote add origin'
alias git-dropbox='TMPGP=~/Dropbox/git/$(pwd | awk -F/ '\''{print $NF}'\'').git;mkdir -p $TMPGP && (cd $TMPGP; git init --bare) && gra $TMPGP && gpom'

答案 10 :(得分:5)

还有一个开源项目(跨平台[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和手册以获取完整的命令参考和教程。

答案 11 :(得分:5)

我们在共享文件夹上使用此方法(在Dropbox中创建裸存储库)。

一小组开发人员可以从该同步的存储库中获取并创建一个本地克隆。一旦工作单元完成,我们就会回到原点。

我遗漏的一件事是,一旦推送到原点,就可以通过变更集信息发送电子邮件。我们正在使用Google Wave手动跟踪更改。

答案 12 :(得分:4)

我将我的非Github回购存储在Dropbox上。我遇到的一个警告是在重新安装后同步。 Dropbox将首先下载最小的文件,然后再转移到较大的文件。如果你晚上开始并在周末之后回来,这不是问题: - )

我的主题 - http://forums.dropbox.com/topic.php?id=29984&replies=6

答案 13 :(得分:3)

我喜欢Dan McNevin的最高投票回答。我最终做了很多次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 master分支。如果给出了多个项目名称,则将使用最右侧的项目名称参数。

(可选)-r命令参数指定将推送到原始主服务器的远程分支。也可以使用-m参数指定项目原点主文件的位置。默认的.gitignore文件也放在远程分支目录中。目录和.gitignore文件默认值在脚本中指定。

答案 14 :(得分:3)

现在在2014年,我一直在使用Git和Dropbox大约一年半没有问题。 但有些观点:

  • 我使用Dropbox的所有机器都在Windows上,不同版本(7到8)+ 1个mac。
  • 我不与其他人共享存储库,所以我是唯一一个修改它的人。
  • git push推送到远程存储库,如果它被破坏,我可以轻松恢复它。
  • 我必须在C:\Users中使用mklink /D link target创建别名,因为有些库指向绝对位置。

答案 15 :(得分:2)

对于我2美分的Dropbox,只有在您不想打扰获得中央回购主机的情况下才能进行个人使用。对于任何专业开发,你可能会产生比你更难解决的问题,正如已经多次在线程中提到的那样,Dropbox不是为这个用例而设计的。也就是说,在没有任何第三方插件或工具的情况下在Dropbox上转储存储库的一种非常安全的方法是使用bundle。我在.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

答案 16 :(得分:1)

另一种方法:

到目前为止,所有答案,包括最受欢迎的@Dan answer,都解决了使用Dropbox集中共享存储库而不是使用专注于git的服务的想法,如github,bitbucket等。

但是,由于原始问题没有具体说明使用&#34; Git和Dropbox一起有效使用&#34;真的意味着,让我们采用另一种方法: &#34;使用Dropbox仅同步工作树。&#34;

该方法有以下步骤:

  1. 在项目目录中,创建一个空的.git目录(例如mkdir -p myproject/.git

  2. 取消同步Dropbox中的.git目录。如果使用Dropbox应用程序:转到首选项,同步和&#34;选择要同步的文件夹&#34;,其中.git目录需要取消标记。这将删除.git目录。

  3. 在项目目录中运行git init

  4. 如果.git已经存在,它也有效,然后只执行第2步.Dropbox会保留网站中git文件的副本。

    第2步将导致Dropbox不同步git系统结构,这是此方法的理想结果。

    为什么会使用这种方法?

    • 尚未推送的更改将包含Dropbox备份,并且它们将在设备之间同步。

    • 如果在设备之间同步时Dropbox会将某些内容搞砸,git statusgit diff可以很方便地解决问题。

    • 它可以节省Dropbox帐户的空间(整个历史记录不会存储在那里)

    • 它避免了@dubek和@Ates在@ Dan的答案评论中提出的问题,以及@clu在another answer中的担忧。

    在其他地方存在遥控器(github等),这种方法可以正常工作。

    在不同的分支机构工作会带来一些问题,需要注意:

    • 一个潜在的问题是Dropbox(不必要地?)在检出不同的分支时同步可能很多的文件。

    • 如果两个或多个Dropbox同步设备签出了不同的分支,则两个设备的未提交更改可能会丢失,

    解决这些问题的一种方法是使用git worktree将分支机构检查保存在不同的目录中。

答案 17 :(得分:0)

不使用第三方集成工具,我可以稍微改善一下条件,并使用DropBox和其他类似的云盘服务,如SpiderOak和Git。

目标是避免在这些文件修改过程中进行同步,因为它可以上传部分状态,然后将其下载,完全破坏你的git状态。

为了避免这个问题,我做了:

  1. 使用git bundle create my_repo.git --all将我的git索引捆绑在一个文件中。
  2. 设置文件监控的延迟,例如5分钟,而不是瞬时。这降低了DropBox在更改过程中同步部分状态的几率。在动态修改云盘上的文件时(例如使用即时保存笔记应用程序),它也会有很大帮助。
  3. 这并不完美,因为无法保证它不会再次搞乱git状态,但它有所帮助,目前我没有遇到任何问题。

答案 18 :(得分:0)

在MacOS上,您也可以停止Dropbox,进行更改,然后重新启动Dropbox。 我正在使用以下组合,对此感到非常满意:

在两个目录(您本地的git托管项目目录和位于Dropbox上的远程git存储库)中,运行以下命令来禁用自动打包(这是Dropbox同步的主要问题)

git config --global gc.auto 0

然后不时地在禁用保管箱的情况下压缩存储库。例如,每当我发布新版应用程序时,我都会在bash-build-script中执行以下操作。

osascript -e "tell application \"Dropbox\" to quit"

# Compress local
git gc --prune=now; git repack -a -d

# Compress remote
REPOS_DIR_REMOTE=`git remote get-url --push origin`
cd "${REPOS_DIR_REMOTE}"
git gc --prune=now; git repack -a -d

osascript -e "tell application \"Dropbox\" to launch"
osascript -e "display notification with title \"Compress Done\""