1
0

More than 3 years have passed since last update.

Linuxサーバからリモートリポジトリにpush & リモートリポジトリからローカルPCにclone

Last updated at Posted at 2020-01-07
目標
  • linuxサーバ上で運用されているrailsアプリのソースコードをGitlab.comのリモートリポジトリにプッシュする。
  • リモートリポジトリからローカルPCのローカルリポジトリにクローンする。
環境
  • linux(GCE, Debian)
  • Gitlab.com
  • Mac(OSX 10.15.2)
準備:git install (linuxサーバ、ローカルPCにGitをインストール)

下記サイトなどを参考にしてください。
https://git-scm.com/book/ja/v2/%E4%BD%BF%E3%81%84%E5%A7%8B%E3%82%81%E3%82%8B-Git%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB

1. linuxの操作
git config(USER_NAMEとEMAILは環境による)
ターミナル
git config --list
git config --global user.name "USER_NAME"
git config --global user.email "EMAIL"
ファイル権限(USERNAMEとUSERGROUPは環境による)

権限を間違えるとgit push時に権限エラーになります。

ターミナル
sudo chown -R USERNAME:USERGROUP ./
sudo find ./ -type d -exec chmod 755 {} +
sudo find ./ -type f -exec chmod 644 {} +
SSH鍵(gitlab_rsaは好きな名前をつける)
  • 秘密鍵は~/.ssh/に置いてください。
  • .pubの内容をGitlab.comのユーザーのssh鍵に貼り付けてください。
ターミナル
ssh-keygen -t rsa -f gitlab_rsa

mv ./gitlab_rsa ~/.ssh/
mv ./gitlab_rsa.pub ~/.ssh/
cat gitlab_rsa.pub 
=>.pubの内容をGitlab.comに貼り付け

eval $(ssh-agent -s)

ssh-add ~/.ssh/gitlab_rsa

ssh -T git@gitlab.com
git init ~ git pushまで(:repoは環境による)

gitコマンドの詳細の説明は省略します。
サーバのローカルリポジトリを初期化+初期コミットして、リモートリポジトリにプッシュしています。

ターミナル
git init

vi .gitignore 
=>rails用のテンプレートから持ってくるとラク

git add .

git commit .

git remote add origin git@gitlab.com:repo

git push origin master
2. ローカルPC操作
プロジェクト用ディレクトリ作成(好きな名前のディレクトリを作成する)
ターミナル
mkdir Gitlab
SSH鍵(gitlab_rsa_userは好きな名前をつける)
  • 秘密鍵は~/.ssh/に置いてください。
  • .pubの内容をGitlab.comのユーザーのssh鍵に貼り付けてください。
ターミナル
ssh-keygen -t rsa -f gitlab_rsa_user

mv gitlab_rsa_user ~/.ssh/

mv gitlab_rsa_user.pub  ~/.ssh/

cat gitlab_rsa_user.pub
=>Gitlabに貼り付け

eval $(ssh-agent -s)

ssh-add ~/.ssh/gitlab_rsa_user

ssh -T git@gitlab.com
git config(USERNAMEとEMAILは環境による)

リモートリポジトリからローカルPCのローカルリポジトリにクローンしています。

ターミナル
git config --global user.name "USERNAME"
git config --global user.email EMAIL
git clone(/repo.gitは環境による)
ターミナル
git clone https://gitlab.com/repo.git
bundle ~ rails sまで

ローカルPCにクローンしてきたディレクトリでbundleとrails sが実行可能か確認する。
サーバ環境と同じrubyバージョン、railsバージョンで実行すると環境依存のエラーを回避できる。
rbenvを使用するとプロジェクトディレクトリ毎にrubyバージョンを指定できるのでラク。
windowsでrbenvを使用する場合は、wslを有効化してubuntuターミナル上で作業する。
参考:
https://qiita.com/chimame/items/8130aa2c07a152a865b1

エラーが出たらエラーメッセージを読んで不足するファイルをサーバから直接持ってくる。
(.gitignoreで指定したファイルはgit管理から外れるため。)

ターミナル
bundle

rails s
1
0
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
1
0