Y's note

Web技術・プロダクトマネジメント・そして経営について

本ブログの更新を停止しており、今後は下記Noteに記載していきます。
https://note.com/yutakikuchi/

MacUserのためのGitHub登録手順のまとめ

0 手順

GitHub登録方法を載せておきます。

  1. 準備
  2. Gitユーザ登録
    1. 公開鍵登録
    2. レポジトリ作成
    3. Commit
  3. git設定ファイル

1 準備

git-coreのinstallをします。

sudo port install git-core

git-hubに登録する公開鍵をローカルマシーンにて作成しておきます。

cd ~/.ssh
ssh-keygen
less id_rsa.pub ← ファイルの内容を後でGitHub側に登録します。

2登録

ユーザ登録

翻訳機能で日本語表示できるので迷うことなく登録できると思います。

https://github.com/
公開鍵登録

以下のページのSSH公開鍵登録より先ほど作成した公開鍵をコピペします。

https://github.com/account
レポジトリ作成

GitHubのWeb上で新しいレポジトリを作成できます。

https://github.com/repositories/new
Commit

Git用のディレクトリの下に各プロジェクト用のディレクトリを作成しておくと良いと思います。※PHPというプロジェクトのレポジトリを作成してみました。

mkdir -p git/PHP
cd git
git config --global user.name "自分の名前"
git config --global user.email メールアドレス
cd PHP
git init //初期化
touch README //ファイル作成
git add README //ファイルをadd
git commit -m 'test commit' README //commit
git remote add origin git@github.com:yutakikuchi/PHP.git  //ファイルをremoteに登録
git push origin master //masterに登録

3 git設定ファイル

デフォルトだと使いづらいということもあるので、git設定ファイルを$HOMEディレクトリに用意しておくと良いでしょう。color,aliasなどを設定しておくと便利だと思います。
.gitconfig : git用の設定ファイル
.gitignore : gitで扱いを無視するファイル

※ .gitconfig
[user]
  name = yutakikuchi
  email = address
[color]
  status = auto
  diff = auto
  branch = auto
  interactive = auto
  grep = auto
[push]
  default = tracking 
[core]
  excludesfile = ~/.gitignore  
  autocrlf = input
[alias]
  co = checkout
  ci = commit -a
  st = status
  up = pull --rebase
[github]
  user = yutakikuchi
  email = address
※ .gitignore
#無視ファイル
 *.swp
 *.bak

GitHubに登録できない場合

commit先のディレクトリとローカルのディレクトリの整合性が取れていない場合は以下のようなエラーが出ます。

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:yutakikuchi/PHP.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

上のようなエラーが出てしまった場合はgit pullでリポジトリのファイルを一度取得すると直ります。

$ git pull git@github.com:yutakikuchi/PHP.git
$ git push origin master

あまり良くない対処法だと思いますがpushに--forceオプションを付けて強制的に行う事も可能です。

$ git push origin master --force