🍣

git で新規作成の方法

2024/05/06に公開

概要

個人開発や自身の自己研鑽をする時に、GitHubと連携させて、開発することがあると思います。その際に、言語やフレームワーク関係なくGitで新規アプリ開発時に使う手順を書いていきます。

手順

1.RailsでもLaravel、何でもいいので、新規に作成
2.GitHubでリポジトリを作成

以下はGitHubに出てくる手順です。

git init
git add README.md
git commit -m "first commit"
git branch -M master
git remote add origin <SSHのurl>
git push -u origin master

自分はこんなトラブルがあった

pushができない時に、以下のようなエラーになりました。

git push -u origin master
To github.com:Hashimoto-Noriaki/frontend-training.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'github.com:Hashimoto-Noriaki/frontend-training.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.

リモートリポジトリにローカルにない変更があるので、git pushが拒否されたというエラーです。これは、他のユーザーが同じブランチに変更をプッシュした時に発生します。

エラーメッセージのヒントに従い、まずはローカルのリポジトリを最新の状態に更新するためにgit pullを実行してみました。pullをしたのですが解決しませんでした。そこでrebaseをやってみました

% git pull origin master --rebase

From github.com:Hashimoto-Noriaki/frontend-training
 * branch            master     -> FETCH_HEAD
Successfully rebased and updated refs/heads/master.
% git push -u origin master

こうしてpushができました。

Discussion