如何同时使用 Git 和 Dropbox?

发布于 2024-08-16 00:38:07 字数 214 浏览 8 评论 0原文

是否可以使用 GitDropbox 一起吗?

Is it possible to use Git and Dropbox together?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

仅此而已 2024-08-23 00:38:07

我认为 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/project.git 目录(无论它是属于您的 Dropbox 帐户还是在多个帐户之间共享)并执行以下操作:所有正常的 Git 操作 - 它们将自动同步到您的所有其他计算机。

我写了一篇博文“关于版本控制”,其中我涵盖我的环境设置背后的推理。它基于我的 Ruby on Rails 开发经验,但它确实可以应用于任何事物。

I think that Git on Dropbox is great. I use it all the time. I have multiple computers (two at home and one at work) on which I use Dropbox as a central bare repository. Since I don’t want to host it on a public service, and I don’t have access to a server that I can always SSH to, Dropbox takes care of this by syncing in the background (very doing so quickly).

Setup is something like this:

~/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

From there, you can just clone that ~/Dropbox/git/project.git directory (regardless of whether it belongs to your Dropbox account or is shared across multiple accounts) and do all the normal Git operations—they will be synchronized to all your other machines automatically.

I wrote a blog post “On Version Control” in which I cover the reasoning behind my environment setup. It’s based on my Ruby on Rails development experience, but it can be applied to anything, really.

许一世地老天荒 2024-08-23 00:38:07

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

在 Dropbox 中创建自己的裸存储库会导致很多问题。 Anish(该库的创建者)解释得最好

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

传统的 Git 遥控器在服务器端运行代码来完成这项工作
正确,但我们不能这样做。

解决方案:可以适当解决这个问题。可以使用
Git 与 Dropbox 具有相同的安全性和一致性保证
作为传统的 Git 远程,即使有多个用户并且
并发操作!

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

通过远程助手,可以将 Dropbox 用作 Git 远程服务器
并继续使用所有常规 Git 命令,例如 git clone、git
pull 和 git push,一切都会按预期工作。

The right way to do this is use git-remote-dropbox: https://github.com/anishathalye/git-remote-dropbox

Creating your own bare repo in Dropbox causes a lot of problems. Anish (the creator of the library) explains it best:

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.

云巢 2024-08-23 00:38:07

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

我没有可能出错的完整列表,但这里有一个让我不舒服的具体例子。每台机器都有自己的行结束字符概念以及如何处理文件名中的大写/小写字符。 Dropbox 和 Git/Mercurial 的处理方式略有不同(我不记得确切的区别)。如果 Dropbox 在 Git/Mercurial 背后更新存储库,很快就会损坏存储库。这种情况会立即发生且不可见,因此您甚至不知道您的存储库已损坏,直到您尝试从中恢复某些内容。

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

在 Dropbox 之上运行 Git/Mercurial 除了风险之外没有任何增加。不要这样做。

This answer is based on Mercurial experience, not Git, but this experience says using Dropbox this way is asking for corrupt repositories if there's even a chance that you'll be updating the same Dropbox-based repository from different machines at various times (Mac, Unix, Windows in my case).

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.

After digging out from one mess doing things this way, I've been using the following recipe with great success and no sign of problems. Simply move your repository out of Dropbox. Use Dropbox for everything else; documentation, JAR files, anything you please. And use GitHub (Git) or Bitbucket (Mercurial) to manage the repository itself. Both are free so this adds nothing to the costs, and each tool now plays to its strengths.

Running Git/Mercurial on top of Dropbox adds nothing except risk. Don't do it.

潦草背影 2024-08-23 00:38:07

我不想将所有项目放在一个 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

I didn't want to put all my projects under one Git repository, nor did I want to go in and run this code for every single project, so I made a Bash script that will automate the process. You can use it on one or multiple directories - so it can do the code in this post for you or it can do it on multiple projects at once.

#!/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
原来是傀儡 2024-08-23 00:38:07

现在已经是 2015 年了,三天前,一个基于 新工具 href="https://blogs.dropbox.com/developers/2015/04/a-preview-of-the-new-dropbox-api-v2/" rel="noreferrer">Dropbox API v2 具有旨在在 Dropbox 上安全地使用 git。它针对 API 而不是使用桌面客户​​端,并正确处理对共享文件夹中托管的存储库的多个同时推送。

配置完成后,它允许人们像任何其他 git 远程一样设置 git 远程。

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

It is now 2015, and as of three days ago, a new tool based on Dropbox API v2 has been created to safely use git on Dropbox. It works against the API rather than using the desktop client, and correctly handles multiple simultaneous pushes to a repository hosted in a shared folder.

Once configured, it allows one to set up a git remote exactly like any other git remote.

git clone "dropbox::/path/to/repo"
git remote add origin "dropbox::/path/to/repo"
遮了一弯 2024-08-23 00:38:07

对于使用 Dropbox 的小团队:

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

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

With regards to small teams using Dropbox:

If each developer has their own writable bare repository on Dropbox, which is pull only to other developers, then this facilitates code sharing with no risk of corruption!

Then if you want a centralized 'mainline', you can have one developer manage all the pushes to it from their own repo.

不及他 2024-08-23 00:38:07

我不认为使用 Git 和 Dropbox 是正确的选择...只需考虑一下两者的功能:

Git:

  • 允许您拥有一个中央存储库
  • 允许您拥有自己的存储库并进行自己的更改
  • 允许您发送并从中央存储库接收更改
  • 允许多人更改相同的文件,他们会合并它们,或者在无法做到的情况下要求您合并它们
  • 具有 Web 和桌面客户端,允许访问中央存储库

Dropbox:

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

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

I don't think that using Git and Dropbox is the way to go... Just think about the features of both:

Git:

  • Allows you to have a central repository
  • Allows you to have your own repository with your own changes
  • Allows you to send and receive changes from the central repository
  • Allows multiple persons to change the same files and them merges them or asks you to merge them if it can't do it
  • Has web and desktop clients to allow access to the central repository

Dropbox:

  • Keeps everything in a central repository
  • Allows you to have your own versions of the files in the server
  • Forces you to send and receive changes from the central repository
  • If multiple persons change the same files, the first file committed is replaced with later commits, and no merge occurs which is troublesome (and definitely its biggest disadvantage)
  • Has web and desktop clients to allow access to the central repository.

And if you're worried with sharing some of your files, why not cipher them? And then you could get the biggest advantage of Dropbox to Git, that is, to have public and private files...

命比纸薄 2024-08-23 00:38:07

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

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

其次,整体加密容器不仅增加了安全性,还减少了存储库 腐败

注意:但是,您必须非常小心,不要在 Dropbox 运行时安装容器。如果两个不同的客户端将不同的版本签入容器,那么解决冲突也可能很痛苦。因此,它仅适用于使用它进行备份的个人,而不适用于团队。

设置:

用法:

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

dropbox取消选中保留修改时间戳会告诉 dropbox 文件已被修改并且应该同步。请注意,即使您没有更改其中的任何文件,安装容器也会修改时间戳。如果您不希望发生这种情况,只需将卷安装为只读

I use Mercurial (or Git) + TrueCrypt + Dropbox for encrypted remote backups.

The coolest thing is that Dropbox does NOT sync the entire TrueCrypt container if you modify a small portion of your code. The sync time is roughly proportional to the amount of changes. Even though it's encrypted, the combination of TrueCrypt + Dropbox makes excellent usage of block cipher + block level sync.

Secondly, a monolithic encrypted container not just adds security, it also reduces chances of repository corruption .

Caution: However you have to be very careful about not having the container mounted while Dropbox is running. It can also be a pain to resolve conflicts if 2 different clients check-in different versions to the container. So, it's practical only for a single person using it for backups, not for a team.

Setup:

  • Create a Truecrypt container (multiple Gigabyte is fine)
  • Under Truecrypt preferences, uncheck preserve modification timestamp*.
  • Create a repo as mentioned above by Dan ( https://stackoverflow.com/a/1961515/781695 )

Usage:

  • Quit Dropbox
  • Mount the container, push your changes, unmount
  • Run dropbox

P.S. Unchecking the preserve modification timestamp tells dropbox that the file has been modified and it should be sync'd. Note that mounting the container modifies the timestamp even if you don't change any file in it. If you don't want that to happen, simply mount the volume as read-only

淡忘如思 2024-08-23 00:38:07

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

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

我缺少的一件事是,一旦发生推送到原点,就可以发送包含更改集信息的电子邮件的好方法。我们正在使用 Google Wave 手动跟踪更改。

We use this method (creating a bare repository in Dropbox) on a share folder.

A small group of developers can pull from that bare synced repository and create a local clone. Once the unit of work is done, we push back to origin.

One thing I'm missing is a good way to have an e-mail sent with the change-set information once a push to origin occurs. We are using Google Wave to manually keep track of changes.

吃素的狼 2024-08-23 00:38:07

我喜欢丹·麦克尼文的回答!我现在也同时使用 Git 和 Dropbox,并且在 .bash_profile< 中使用多个别名/a> 所以我的工作流程如下所示:

~/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'

I love the answer by Dan McNevin! I'm using Git and Dropbox together too now, and I'm using several aliases in my .bash_profile so my workflow looks like this:

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

These are my aliases:

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'
眼藏柔 2024-08-23 00:38:07

我一直按照推荐的方式使用 Mercurial,并敦促您谨慎行事,尤其是在任何机器有所不同的情况下。 Dropbox 论坛上充斥着对自发出现的神秘文件名大小写问题的抱怨。 Hg(我认为 Git)在例行检查期间不会注意到或抱怨,并且当您尝试真正使用它时,当它抱怨损坏的存储库时,您只会听到损坏。坏消息。希望我能更具体地说明该问题及其解决方法;我自己仍在努力摆脱困境。

I've been using Mercurial in the recommended manner and urge that you be cautious, especially if any of the machines differ. The Dropbox fora are full of complaints of mysterious filename case problems turning up spontaneously. Hg (and I presume Git) won't notice or complain during routine checkins and you'll only hear about the corruption when it complains of a corrupt repo when you try to use it for real. Bad news. Wish I could be more specific about the problem and its workarounds; I'm still trying to dig out from this mess myself.

口干舌燥 2024-08-23 00:38:07

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

There's also an open source project (a collection of cross platform [Linux, Mac, Win] scripts) that does all the nitty-gritty details of the repository management with a handful (3-4) of commands.

https://github.com/karalabe/gitbox/wiki

Sample usage is:

$ gitbox create myapp
Creating empty repository...
Initializing new repository...
Repository successfully created.

$ gitbox clone myapp
Cloning repository...
Repository successfully cloned.

After which normal git usage:

$ echo “Some change” > somefile.txt
$ git add somefile.txt
$ git commit –m “Created some file”
$ git push

Check the project wiki and the manuals for full command reference and tutorials.

想你只要分分秒秒 2024-08-23 00:38:07

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

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

I store my non-Github repo's on Dropbox. One caveat I ran into was syncing after a reinstall. Dropbox will download the smallest files first before moving to the larger ones. Not an issue if you start at night and come back after the weekend :-)

My thread - http://forums.dropbox.com/topic.php?id=29984&replies=6

绅士风度i 2024-08-23 00:38:07

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

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

Now in 2014, I have been using Git and Dropbox for about one year and a half without problem.
Some points though:

  • All my machines using Dropbox are on Windows, different versions (7 to 8) + 1 mac.
  • I do not share the repository with someone else, so I am the only one to modify it.
  • git push pushes to a remote repository, so that if it ever gets corrupted, I can easily recover it.
  • I had to create aliases in C:\Users with mklink /D link target because some libraries were pointed to absolute locations.
dawn曙光 2024-08-23 00:38:07

另一种方法:

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

但是,由于最初的问题没有具体说明“有效地使用 Git 和 Dropbox”的真正含义,所以让我们研究另一种方法:
“使用 Dropbox 仅同步工作树。”

操作方法包含以下步骤:

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

  2. 取消同步 .git< Dropbox 中的 /code> 目录。如果使用 Dropbox 应用:转到“首选项”、“同步”,然后“选择要同步的文件夹”,其中 .git 目录需要取消标记。这将删除 .git 目录。

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

如果.git已经存在,它也可以工作存在,则只需执行步骤 2。不过 Dropbox 会在网站中保留 git 文件的副本。

步骤 2 将导致 Dropbox 不同步 git 系统结构,这是此方法的预期结果。

为什么要使用这种方法?

  • 尚未推送的更改将有 Dropbox 备份,并且它们将跨设备同步。

    尚未

  • Dropbox 在设备之间同步时出现问题,git statusgit diff 将很方便地解决问题。

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

  • 它避免了@dubek 和 @Ates 对 @Dan 答案的评论,以及 @clu 在另一个答案中的担忧。

其他地方(github等)的远程存在将适用于这种方法。

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

  • 一个潜在的问题是,当一个人签出不同的分支时,Dropbox(不必要?)可能会同步许多文件。

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

解决这些问题的一种方法是使用 git worktree 将分支签出分开目录。

Another approach:

All the answers so far, including @Dan answer which is the most popular, address the idea of using Dropbox to centralize a shared repository instead of using a service focused on git like github, bitbucket, etc.

But, as the original question does not specify what using "Git and Dropbox together effectively" really means, let's work on another approach:
"Using Dropbox to sync only the worktree."

The how-to has these steps:

  1. inside the project directory, one creates an empty .git directory (e.g. mkdir -p myproject/.git)

  2. un-sync the .git directory in Dropbox. If using the Dropbox App: go to Preferences, Sync, and "choose folders to sync", where the .git directory needs to get unmarked. This will remove the .git directory.

  3. run git init in the project directory

It also works if the .git already exists, then only do the step 2. Dropbox will keep a copy of the git files in the website though.

Step 2 will cause Dropbox not to sync the git system structure, which is the desired outcome for this approach.

Why one would use this approach?

  • The not-yet-pushed changes will have a Dropbox backup, and they would be synced across devices.

  • In case Dropbox screws something up when syncing between devices, git status and git diff will be handy to sort things out.

  • It saves space in the Dropbox account (the whole history will not be stored there)

  • It avoids the concerns raised by @dubek and @Ates in the comments on @Dan's answer, and the concerns by @clu in another answer.

The existence of a remote somewhere else (github, etc.) will work fine with this approach.

Working on different branches brings some issues, that need to be taken care of:

  • One potential problem is having Dropbox (unnecessarily?) syncing potentially many files when one checks out different branches.

  • If two or more Dropbox synced devices have different branches checked out, non committed changes to both devices can be lost,

One way around these issues is to use git worktree to keep branch checkouts in separate directories.

凉月流沐 2024-08-23 00:38:07

我喜欢丹·麦克尼文 (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 文件默认值在脚本中指定。

I like the top-voted answer by Dan McNevin. I ended up doing the sequence of git commands too many times and decided to make a script. So here it is:

#!/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}"

The script only requires a project name. It will generate a git repository in ~/Dropbox/git/ under the specified name and will push the entire contents of the current directory to the newly created origin master branch. If more than one project name is given, the right-most project name argument will be used.

Optionally, the -r command argument specifies the remote branch that will push to the origin master. The location of the project origin master can also be specified with the -m argument. A default .gitignore file is also placed in the remote branch directory. The directory and .gitignore file defaults are specified in the script.

娇妻 2024-08-23 00:38:07

对于我的 2 美分来说,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

For my 2 cents Dropbox only makes sence for personal use where you don't want to bother getting a central repo host. For any professional development you'll probably create more problems than you'll solve, as have been mentioned several times in the thread already, Dropbox isn't designed for this use case. That said, a perfectly safe method to dump repositories on Dropbox without any third-party plugins or tools is to use bundles. I have the following aliases in my .gitconfig to save typing:

[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 #"

Example:

# 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
怎樣才叫好 2024-08-23 00:38:07

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

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

I have faced a similar issue and have created a small script for the same. The idea is to use Dropbox with Git as simply as possible. Currently, I have quickly implemented Ruby code, and I will soon add more.

The script is accessible at https://github.com/nuttylabs/box-git.

缘字诀 2024-08-23 00:38:07

在不使用第三方集成工具的情况下,我可以稍微增强一下条件,并使用 DropBox 和其他类似的云盘服务(例如 SpiderOak 和 Git)。

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

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

  1. 使用 git bundle create my_repo.git --all 将我的 git 索引捆绑在一个文件中。
  2. 设置文件监控的延迟,例如5分钟,而不是瞬时的。这减少了 DropBox 在更改过程中同步部分状态的机会。当即时修改云盘上的文件时(例如即时保存笔记应用程序),它也有很大帮助。

它并不完美,因为不能保证它不会再次弄乱 git 状态,但它有帮助,目前我没有遇到任何问题。

Without using third-party integration tools, I could enhance the condition a bit and use DropBox and other similar cloud disk services such as SpiderOak with Git.

The goal is to avoid the synchronization in the middle of these files modifications, as it can upload a partial state and will then download it back, completely corrupting your git state.

To avoid this issue, I did:

  1. Bundle my git index in one file using git bundle create my_repo.git --all.
  2. Set a delay for the file monitoring, eg 5 minutes, instead of instantaneous. This reduces the chances DropBox synchronizes a partial state in the middle of a change. It also helps greatly when modifying files on the cloud disk on-the-fly (such as with instantaneous saving note-taking apps).

It's not perfect as there is no guarantee it won't mess up the git state again, but it helps and for the moment I did not get any issue.

忆依然 2024-08-23 00:38:07

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

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

git config --global gc.auto 0

然后时不时地在禁用 dropbox 的情况下压缩存储库。例如,每当我发布新版本的应用程序时,我都会在 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\""

On MacOS you may also just stop Dropbox, make your changes and then relaunch Dropbox.
I am using the following combination and am quite happy with it:

In both (your local git managed project directory and your remote git repository located on Dropbox) run the following command to disable auto-packing (which is the main problem with dropbox syncing)

git config --global gc.auto 0

Then from time to time, compress the repositories with dropbox disabled. For example I do the following in my bash-build-script whenever I make new releases of my apps.

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\""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文