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工作树将分支签出保存在单独的目录中。

其他回答

我喜欢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'

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

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

我喜欢丹·麦克内文投票最多的回答。我最后做了太多次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文件默认值在脚本中指定。

正确的方法是使用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.

现在是2014年,我已经使用Git和Dropbox大约一年半了,没有任何问题。 以下是几点:

我所有使用Dropbox的电脑都是Windows操作系统,不同版本(7到8)+ 1个mac。 我不与其他人共享存储库,所以我是唯一一个修改它的人。 git push推送到一个远程存储库,这样如果它损坏了,我可以很容易地恢复它。 我必须在C:\Users中使用mklink /D链接目标创建别名,因为一些库指向绝对位置。