脳汁portal

アメリカ在住(だった)新米エンジニアがその日学んだIT知識を書き綴るブログ

ローカルに既に存在するプロジェクトを新規にGithubにpushする場合

途中までローカルで作成したプロジェクトをやっぱりgithubで管理したいとなった時の手順
クライアントを使ったり、サーバ内で完結する方法など何個か方法がありますが、(私が)一番簡単だと思う方法を載せます。

手順

1. WebのGithub Pageから対象のリポジトリを作成する

RepositoriesのPageで「New」を選択します

f:id:portaltan:20151106165050p:plain

Repositoryを作成する
  • Initialize this repository wiht a READMEのチェックはoff
  • Add .gitignoreはNoneのまま
  • Add a licenseもNoneのまま

f:id:portaltan:20151106165324p:plain

空のリポジトリが出来るので、以下のマーク内のコードをメモしておく

f:id:portaltan:20151106165703p:plain

2. ローカルでpushの準備をする

まずはgitで使用すると宣言する(gitマーキング)
$cd ${project directory}
$git init
gitのユーザ名とアドレスを登録(設定していない場合)
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
次に管理対象のファイルを指定する
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       index.html
nothing added to commit but untracked files present (use "git add" to track)

index.htmlを管理したい場合は

$ git add index.html
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   index.html
#
commitする
$ git commit -a
1で作成したリポジトリをpush先として登録する

コピーしたコードの1行目を実行

$ git remote add origin https://github.com/nouziru/git_test.git
$ git remote -v
origin  https://github.com/nouziru/git_test.git (fetch)
origin  https://github.com/nouziru/git_test.git (push)

3. 実際にpushする

$ git push origin master
  Username for 'https://github.com': 
  Password for 'https://nouziru@github.com':
  Counting objects: 3, done.
  Writing objects: 100% (3/3), 219 bytes, done.
  Total 3 (delta 0), reused 0 (delta 0)
  To https://github.com/nouziru/git_test.git
   * [new branch]      master -> master
4. 確認

実際に先ほどのGithubのPageに行くと、initial commitがpushされていることを確認できます。
f:id:portaltan:20151106170910p:plain




多分これが一番早いと思います