LoginSignup
23
26

More than 5 years have passed since last update.

githubリポジトリ作成後の初プッシュでちょっとハマったこと

Posted at

githubでリポジトリは作ったものの、コマンドラインからどうやってコミットとかプッシュするんだろうと思ったので、その時の手順もめもがてら含めて投稿します。

動作環境

macOS High Sierra 10.13.3
ターミナル

作業ディレクトリの作成

自分は、Documents配下に作成しました。

-MacBook-Pro:RetirementNotice_rep marimon$ pwd
/Users/marimon/Documents/Other/RetirementNotice_rep

こんな感じ。

ローカルリポジトリの作成

作成した作業ディレクトリをローカルリポジトリとして登録します。
使用するコマンドは、git initです。

marimons-MacBook-Pro:RetirementNotice_rep marimon$ git init

marimons-MacBook-Pro:RetirementNotice_rep marimon$ ls -all
total 24
drwxr-xr-x   8 marimon  staff   256  2 22 10:15 .
drwxr-xr-x  10 marimon  staff   320  2 22 10:14 ..
-rw-r--r--@  1 marimon  staff  6148  2 22 10:15 .DS_Store
drwxr-xr-x   9 marimon  staff   288  2 22 10:14 .git

.gitというサブディレクトリが作成されてますね。
これでOKです。

ローカルリポジトリへコミット

git addgit commitという2種類のコマンドを使用して、コミットを行います。

まず、前者のgit addでgit管理の対象であることを伝えます。

marimons-MacBook-Pro:RetirementNotice_rep marimon$ git add .

作業ディレクトリ配下のファイルは、もれなくgit管理の対象としたのでgit add .としています。
ちなみにgit add *.cssとすれば、対象をCSSファイルだけとすることも可能です。

そして、後者のgit commitでコミットします。

marimons-MacBook-Pro:RetirementNotice_rep marimon$ git commit -m "1st commit"
[master (root-commit) 308e06a] first commit
 23 files changed, 6923 insertions(+)

git commit -m "コミットメッセージ" オプションのmをつけることで、コミットにメッセージをつけています。

コミットログの確認

念のため、ちゃんとコミットできているかを確認してみましょう。
git logというコマンドを使用します。

marimons-MacBook-Pro:RetirementNotice_rep marimon$ git log
commit 308e06a******ca0eaecedd1b6c27 (HEAD -> master)
Author: marimons <*****@yahoo.co.jp>
Date:   Thu Feb 22 1*:3*:12 2018 +0900

    first commit

問題なくできていますね。

リモートリポジトリ(github)へのプッシュ

githubへのプッシュを行います。
すでにローカルリポジトリへのコミットは住んでいますので、このままプッシュの流れにいきます。

まずは、プッシュ先の登録を行います。ここでいうとリモートリポジトリの登録ですね。
git remote add origin プッシュ先URL

marimons-MacBook-Pro:RetirementNotice_rep marimon$ git remote add origin https://github.com/***/***.git

marimons-MacBook-Pro:RetirementNotice_rep marimon$ git remote -v
origin  https://github.com/***/***.git (fetch)
origin  https://github.com/***/***.git (push)

問題なく登録できました。
ちなみに、git remote -vで登録状況の確認ができます。
origin はリモートリポジトリの名前です。

そして、プッシュします。
今しがた登録したリモートリポジトリへプッシュを行います。
git push origin master

しかし、こんなエラーが・・・。

error: failed to push some refs to 'https://github.com/***/***.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first 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.

pullすればいいのかなと思い、pullしたが・・・。

 * [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 branch --set-upstream-to=origin/master master
で設定してもよかったと思うのですが、最新状態をローカルに反映させてたかったので、マージを選択。

git merge --allow-unrelated-histories origin/master

上記コマンドの詳細は下記あたりが、参考になるかなと思います。
2つの Git リポジトリーを合成する
git2.9 から、--allow-unrelated-historiesをオプションにつけないといけなくなったみたい。

marimons-MacBook-Pro:RetirementNotice_rep marimon$ git merge --allow-unrelated-histories origin/master
Merge made by the 'recursive' strategy.
 LICENSE | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 LICENSE

無事マージ完了。

ついでプッシュです。

marimons-MacBook-Pro:RetirementNotice_rep marimon$ git push origin master
Counting objects: 29, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (27/27), done.
Writing objects: 100% (29/29), 83.87 KiB | 7.62 MiB/s, done.
Total 29 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/***/***.git
   75a942e..a57a3e0  master -> master

プッシュも無事成功。
githubを確認するとローカルと同じ状態に!!

23
26
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
23
26