LoginSignup
0
0

More than 3 years have passed since last update.

ローカルに作ったJavascriptのプログラムをgithubの新規リポジトリにで管理する為のメモ

Last updated at Posted at 2020-09-16

概要

技術検証用に作成したjavascriptのプログラムがある。色々試した更新を残しておくためにgithubのリポジトリに登録し管理することにしたので、その手順を記す。

手順

大まかな流れとして次のようになる。

  1. githubにリポジトリを作成する
  2. 検証作業を行ったローカルのディレクトリをgitリポジトリ化する
  3. ローカルのリポジトリのリモートリポジトリとしてgithubのリポジトリを設定する
  4. githubのリポジトリとローカルのリポジトリをマージする
  5. ローカルのリポジトリをpushする

githubにリポジトリを作成する

https://github.com/new にアクセスし、新しいリポジトリを作成する。

image.png

今回は個人的な学習用に作ったプログラムなので公開はせずPrivateとした。

  • Initialize this repository with:Add a README fileAdd .gitignore
  • .gitignore template:Nodeを選択した。

ローカルのプロジェクトをgitリポジトリとして初期化する

検証用作業を行っていたディレクトリに移動し、同ディレクトリをgitリポジトリとして初期化する。
ちなみに、今回の検証用のディレクトリはnpm initされたディレクトリで、Node.jsのプロジェクト。

$ git init
# > Initialized empty Git repository in /Users/****/workspace/labo0/.git/

ファイルをコミットする

ローカルのファイル群をコミットする。githubのリポジトリには既に.gitignoreファイルがあるので、同ファイルをローカルのリポジトリにコピーしてからコミットする方が余計なファイルをコミットせずに済むのかも。

リモートリポジトリとしてgithubのリポジトリを設定する

以下のコマンドでリモートを追加出来る。

$ git remote add origin https://github.com/****/labo0.git
$ git ls-remote origin
# > AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA        HEAD
# > AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA        refs/heads/master

このコマンドを見つけるよりも先に以下のコマンドを見つけ試したが失敗した。
「originというリモートは見つからない」とのことで、事前にoriginが存在していないとダメと言うことなんだと認識。

$ git remote set-url origin https://github.com/****/labo0.git
# > fatal: No such remote 'origin'

githubのリポジトリをmergeする

リモートのorigin/masterをローカルのmasterブランチで追跡するように設定し、マージを実施する。

$ git branch --set-upstream-to=origin/master master
# > Branch 'master' set up to track remote branch 'master' from 'origin'.
$ git merge --allow-unrelated-histories
# > Merge made by the 'recursive' strategy.
# >  .gitignore | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# >  README.md  |   1 +
# >  2 files changed, 105 insertions(+)
# >  create mode 100644 .gitignore
# >  create mode 100644 README.md

--set-upstream-to=origin/master master

git pull git push を行う際に、引数(<remote> <branch>)を省略しするために必要。

--allow-unrelated-histories

ローカルのリポジトリとgithubのリポジトリは無関係の独立したリポジトリ同士。それらを強制的にマージするためにはこのオプションが必要ということ?

補足 上記設定を行わずにpullやpushを実施た場合のメッセージ

$ git pull
# > warning: no common commits
# > remote: Enumerating objects: 4, done.
# > remote: Counting objects: 100% (4/4), done.
# > remote: Compressing objects: 100% (3/3), done.
# > remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
# > Unpacking objects: 100% (4/4), done.
# > From https://github.com/****/labo0.git
# >  * [new branch]      master     -> origin/master
# > There is no tracking information for the current branch.
# > Please specify which branch you want to merge with.
# > See git-pull(1) for details.
# > 
# >     git pull <remote> <branch>
# > 
# > If you wish to set tracking information for this branch you can do so with:
# > 
# >     git branch --set-upstream-to=origin/<branch> master
$ git push origin master

# > To https://github.com/****/labo0.git
# >  ! [rejected]        master -> master (non-fast-forward)
# > error: failed to push some refs to 'https://github.com/****/labo0.git'
# > hint: Updates were rejected because the tip of your current branch is behind
# > hint: its remote counterpart. Integrate the remote changes (e.g.
# > hint: 'git pull ...') before pushing again.
# > hint: See the 'Note about fast-forwards' in 'git push --help' for details.

githubのリポジトリにpushする

$ git push
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