0
0

Gitのリモートリポジトリをローカル環境に作成,後からpush先をSSH先(Github, Devops等)に変更

Last updated at Posted at 2024-04-08

経緯

  • 個人開発の場合はcommitだけで正直事足りるが,いつもと同様のルールでバージョン管理したい
  • どうしても生まれる裏作業内容や秘匿性の高いソースコードを個人でバージョン管理したい
  • アジャイル開発に必須だと思うGitの勉強(時間がない時修正箇所にconflictができて迷惑をかけた経験)

Gitでリモートリポジトリをローカル環境に作成

基本は以下(リモートを先に立てて,git clone)を参考したが,
今回は既にローカルが存在することを前提としたい.
remote addする形でリモートリポジトリを作成する.

以下のローカルリポジトリが既に存在するとする.

image.png

空のリモートリポジトリを任意のディレクトリに作成.

mkdir gitserver
cd gitserver
git init --bare --shared ./hoge.git  # gitファイル名はオプション
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty shared Git repository in /.../gitserver/hoge.git/

ローカルリポジトリをリモートリポジトリへ反映する.

git remote add origin /.../gitserver/hoge.git
git config -l
user.name=...
user.email=...
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=/.../gitserver/hoge.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

push

以下の状態でpushを試すと当然developブランチが無いので警告が出るのでOKする.

image.png

image.png

clone

VSCode上でbranchをdevelopに切り替えると,無事cloneできていることがわかった.
しかし,当然developブランチしか紐付かないので,以下のように一直線になった.

mkdir local2 && cd $_
git clone /.../gitserver/hoge.git/
Cloning into '2024_VSCode_Git_Github_test'...
done.
warning: remote HEAD refers to nonexistent ref, unable to checkout.

image.png

やはり最初から計画的にリモートリポジトリを作ってgit cloneする方が自然かも.
以下で解決.

git push --all
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To /.../gitserver/hoge.git
 * [new branch]      main -> main
 * [new branch]      master -> master

image.png

追記:後々調べたら以上までの点で同じようなことを試した方がいらっしゃった.

後からpush先をSSH先(Github, Devops等)に変更

ローカルリポジトリのpush先remote.origin.urlを,上のローカルPC内リモートリポジトリからSSH先(Github, Devops等)に変更したい.

git remote set-url origin (Githubで作ったリモートリポジトリURL)
git push --all

mainだけ後からGithub上につくったせいで順番が逆になったが,master, developはOK

Enumerating objects: 17, done.
Counting objects: 100% (17/17), done.
Delta compression using up to 16 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (17/17), 1.18 KiB | 604.00 KiB/s, done.
Total 17 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/.../test.git
 * [new branch]      develop -> develop
 * [new branch]      master -> master
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/.../test.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and 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.

image.png

所感

リモート作ってからローカル作る方が自然かもしれないが,
今後はローカルである程度commitできたら,ローカルPCにGit用サーバー立てて,
リモートリポジトリでバージョン管理するようにしても良いと思う.

上記の逆はどうするのか?:
開発にOSSを使う目的で,OSS自体の機能追加はしたくない
(例えば,このOSSアルゴリズムを使ったROSの開発をするときに,
 挙動を確認するために評価スクリプトを作った時)
➡そのコードをForkせずcloneしたとき,
ただその開発成果を公知したい場合,元のOSSもフリーだから,
全く別のリポジトリに挙げても問題ないと思うのだが,
その場合もgit remote set-url originすればよいのだろうか?
もちろん開発途中はPrivate repositoryになると思うが,本当のcontributor情報は残したい
あと,.gitが入れ子になったときの挙動等,まだまだわからないことがいっぱい.
とりあえずこういう場合はOSS側の.gitを消すのが良いのか.

ここら辺?↓

0
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
0
0