ローカルのディレクトリをGitHubに登録してみる

ローカルにある既存のディレクトリのファイルを
GitHubに登録してみる。

リポジトリに登録するディレクトリを用意する

今回GitHubリポジトリに登録するディレクトリ(ファイル)を用意する。

$ mkdir sample_dir
$ cd sample_dir
$ vi hello.txt
Hello, world!

GitHubに空のリポジトリを作成する

GitHubリポジトリを作成する。
「Initialize this repository with a README」にはチェックを入れない。

ちなみに、リポジトリを作成した後に、GitHubでこの後の登録の仕方が
表示されるので、基本これに従えばいい。

GitHubリポジトリを登録

sample_dirにて、git initでリポジトリを作成する。

$ git init

ファイルをaddしてcommitする。

$ git add hello.txt
$ git commit -m 'first commit'

GitHubリポジトリをリモートリポジトリして追加する。

$ git remote add origin https://github.com/yk5656/sample.git

pushで反映する。

$ git push -u origin master

GitHubに反映されているのが確認できる。

(補足)初回のpushは「-u origin master」が必要

初回、単に「git push」とするとエラーになる。

$ 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

なんか紐付いてないため(らしい)

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = https://github.com/yk5656/sample.git
        fetch = +refs/heads/*:refs/remotes/origin/*

一度「-u origin master」をつけてpushすると、

$ git push -u origin master

紐づく。

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = https://github.com/yk5656/sample.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

これで、次回以降は「git push」でいける。