LoginSignup
6
10

More than 5 years have passed since last update.

boxにgitリポジトリを作る

Posted at

はじめに

勤め先でbitbucketが使えるんですが、一人での作業も多いのでbitbucketにリポジトリを作るほどでもないプロジェクトもまあまああります。
それで、そのプロジェクトのバックアップをとろうとした時に社用のboxにプロジェクト丸ごとあげたらnode_modulesのサイズがでか過ぎてアップロード終わらねぇ!なんてことがありました。

色々模索した結果、Dropboxにgitリポジトリを作成する方法があったので、それを参考に社用のboxの同期フォルダーにgitリポジトリを作成するコマンドを作成してみました。

環境

  • macOS High Sierra
  • bash
  • fish

Winの方は適宜読み替えてください🙇‍

やり方

ローカルのリポジトリを作成してプロジェクトでリモートを追加するのは結構簡単で、

box側で

# /Users/<USER_NAME>/Box Sync/
mkdir -p git/<PROJECT_NAME>
cd git/<PROJECT_NAME>
git init --bare --shared

を実行し、プロジェクト側で

git remote add box "/Users/<USER_NAME>/Box Sync/git/<PROJECT_NAME>"

これで、boxをリポジトリとして使えるのですが、毎回これをやるのも面倒なんでコマンド化します

create-box-git () {

    echo "start"

    PROJECT_DIR=$PWD
    NAME=`basename $PWD`
    BOX_GIT_PATH="/Users/<USER_NAME>/Box Sync/git"
    GIT_PATH="$BOX_GIT_PATH/$NAME"

    cd $BOX_GIT_PATH
    mkdir $NAME
    cd $NAME
    git init --bare --shared
    cd $PROJECT_DIR

    if [ -d ./.git ]; then
        echo "git あるよ"
    else
        echo "git ないよ"
        git init
    fi

    git remote add box $GIT_PATH
    touch .gitignore
    echo "sucsess!"
}

これを~/.bash_profileに書き加えます。

fishの場合はこちら
function create-box-git
    echo "start"

    set project_dir $PWD
    set name (basename $PWD)
    set box_git_path "/Users/<USER_NAME>/Box Sync/git"
    set git_path "$box_git_path/$name"

    cd $box_git_path
    mkdir $name
    cd $name
    git init --bare --shared
    cd $project_dir

    if test -d ./.git
        echo "git あるよ"
    else
        echo "git ないよ"
        git init
    end

    git remote add box $git_path
    touch .gitignore
    echo "sucsess!"
end

これを.config/fish/config.fishに書き加えます

まとめ

これでまめにプロジェクトのバックアップができるので、社用のmacがおかしくなっても安心して作業できます!

どっちかというと、gitの勉強というより、bashやfishのスクリプトの勉強の方が時間かかった気がしますが...😅

参考

6
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10