前回の Subversion から Git へ移行 [wsl] にて、SVN リポジトリから変換して出来た Git リポジトリがローカルにあるので、今回は Git リポジトリを Git サーバーへ上げていきます。
サーバーに空のリポジトリを作る
Git サーバーに空のリポジトリを作成します。
Git が使えるサービスならどこでも大きな違いは無いと思いますが、私が利用している Bitbucket で作っていきます。
Create のドロップダウンを表示して、リポジトリを選択します。

作成するリポジトリの情報を入力して、[リポジトリの作成]をクリックします。
例では、README を含めず、.gitignore を含めるようにしています。

作成されました。

サーバーへ push する
ここからは WSL 上での作業です。
credential.helper を設定に追加する
WSL 上で Bitbucket から git clone したり、Bitbucket へ push したりするには、credential.helper を追加する必要があります。追加しないと Authentication failed になって実行できません。
credential.helper の値に Git for Windows をインストールしたときに入った git-credential-manager-core.exe を指定しますので、git-credential-manager-core.exe が入っている場所を探します。
$ find /mnt/c/Program\ Files -name "git-credential-manager-core.exe"
/mnt/c/Program Files/Git/mingw64/bin/git-credential-manager-core.exe
上のコマンドで発見した git-credential-manager-core.exe のパスを git config に追加します。
$ git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager-core.exe"
設定状況を確認しておきます。
$ git config --global -l
user.name=xxxx
user.email=xxxx@xxxx.com
credential.helper=/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager-core.exe
master を git push する
サーバーの URL を設定に追加
fetch や push が出来るように、サーバー(Bitbucket) の URL を .git/config
に追加します。ここでは、ファイルを直接編集するのではなく、コマンドで追加しています。
$ git remote add origin https://username@bitbucket.org/username/madokae.git
設定されたことを確認します。
$ git remote -v
origin https://username@bitbucket.org/username/madokae.git (fetch)
origin https://username@bitbucket.org/username/madokae.git (push)
git fetch
リモート追跡ブランチの origin/master を取得します。
$ git fetch origin master
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 577 bytes | 24.00 KiB/s, done.
From https://bitbucket.org/username/madokae
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
ここから git fetch → git merge → git push の順に実行していきますが、この順ではなく、git push URL指定をすると Updates were rejected because the remote contains work that you do not have locally. となります。
Bitbucket でリポジトリを作るときに 「Include .gitignore?」を Yes にしたため、リモート側に .gitignore
があって、ローカルに無いという差が出ているためです。
$ git push https://username@bitbucket.org/username/madokae.git
To https://bitbucket.org/username/madokae.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://username@bitbucket.org/username/madokae.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull …') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
確認します。git branch -a で見ると、最後に remotes/origin/master が追加されています。
$ git branch -a
* master
svn/0.9.5
svn/0.9.6
svn/0.9.7
svn/0.9.8
svn/doc
svn/trunk
remotes/origin/master
git merge
fetch した remotes/origin/master
とローカルの master に入っている内容をマージします。
$ git merge --allow-unrelated-histories origin/master
Merge made by the 'recursive' strategy.
.gitignore | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
create mode 100644 .gitignore
--allow-unrealted-histories
祖先が違うので –allow-unrelated-histories オプションを付けます。Bitbucket は 新規に作成したもの、ローカルは SVN リポジトリから移行して新規で作成されたものと祖先が違っているためです。
--allow-unrealted-histories
を付けないで実行すると、refusing to merge unrelated histories
になります。
$ git merge origin/master
fatal: refusing to merge unrelated histories
git push
$ git push --set-upstream origin master
–set-upstream origin master
現在作業している Gitリポジトリは、SVNから変換してローカルで作成したものなので、現在の作業ブランチから見た上流ブランチが存在していません。この上流ブランチを指定して git push してあげる必要があります。
–set-upstream を付けないとエラーになります
$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
全てのタグを git push する
$ git push origin --tags
Enumerating objects: 2142, done.
Counting objects: 100% (1567/1567), done.
Delta compression using up to 12 threads
Compressing objects: 100% (331/331), done.
Writing objects: 100% (1006/1006), 4.81 MiB | 2.98 MiB/s, done.
Total 1006 (delta 911), reused 749 (delta 672)
remote: Resolving deltas: 100% (911/911), completed with 122 local objects.
To https://bitbucket.org/username/madokae.git
* [new tag] 0.9.5.02 -> 0.9.5.02
* [new tag] 0.9.5.03 -> 0.9.5.03
* [new tag] 0.9.6.63 -> 0.9.6.63
* [new tag] 0.9.7.31 -> 0.9.7.31
* [new tag] 0.9.8.37 -> 0.9.8.37
全てのブランチを git push する
$ git push --all origin
Enumerating objects: 1182, done.
Counting objects: 100% (1083/1083), done.
Delta compression using up to 12 threads
Compressing objects: 100% (314/314), done.
Writing objects: 100% (655/655), 14.16 MiB | 1.22 MiB/s, done.
Total 655 (delta 556), reused 431 (delta 338)
remote: Resolving deltas: 100% (556/556), completed with 132 local objects.
To https://bitbucket.org/username/madokae.git
* [new branch] svn/0.9.5 -> svn/0.9.5
* [new branch] svn/0.9.6 -> svn/0.9.6
* [new branch] svn/0.9.7 -> svn/0.9.7
* [new branch] svn/0.9.8 -> svn/0.9.8
* [new branch] svn/doc -> svn/doc
* [new branch] svn/trunk -> svn/trunk
サーバーの状況を確認する
ローカル環境での全ての push が完了したので、サーバーのタグとブランチの反映状況を確認します。
タグを確認する
master のドロップダウンを表示、Tags を表示して、タグが反映されていることを確認します。

ブランチを確認する
master のドロップダウンを表示、Branches を選択して、ブランチが反映されていることを確認します。

完了
以上で Bitbucket への移入は完了です。
コメント