0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

github 最初のコマンド

Last updated at Posted at 2023-08-30

初めに

自分のコードをgithubにrepositoryをアップロード(push)する時のコマンドの説明です。

典型的なコマンド

git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/<あなたのgithub repository.git>
git push -u origin main

コマンドの意味

git init

現在のディレクトリを新しいGitリポジトリとして初期化します。
このコマンドは.gitという隠れディレクトリを作成し、リポジトリのメタデータを格納します。

git add README.md

README.mdファイルをGitのステージングエリアに追加します。ステージングエリアは、次のコミットに含める変更を一時的に保存する場所です。

git commit -m "first commit"

ステージングエリアの変更をリポジトリにコミット(保存)します。コミットメッセージとして"first commit"を使用します。 コミットはリポジトリの歴史に永続的に保存されるスナップショットです。

git branch -M main

現在のブランチの名前をmainに変更します。
-Mオプションは、ブランチがすでに存在する場合でも強制的に名前を変更します。

git remote add origin https://github.com/<あなたのgithub repository.git>

新しいリモートリポジトリを追加し、それをoriginという名前で参照します。
originはリモートリポジトリ(この場合はGitHub上のリポジトリ)のショートカット名です。

git push -u origin main

ローカルのmainブランチをoriginリモートリポジトリにプッシュ(アップロード)します。
-uオプションは、今後git pushやgit pullを実行する際にリモートリポジトリとブランチ名を指定しなくても済むように、追跡関係を設定します。

覚える内容

  • branch名 : 前はdefaultとしてmasterでしたが、master-slaveという差別的要素があることで、最近はmainにする傾向があります。

  • main: これはローカルPCにあるブランチの名前です。このブランチには、あなたが作業している変更が含まれています。ローカルブランチはあなたのコンピュータ内に存在し、その変更はリモートリポジトリにプッシュするまで他の人には見えません。

  • origin: これはリモートリポジトリ(この場合はGitHub上のリポジトリ)のショートカット(エイリアス)名です。git remote add コマンドで設定されます。originは通常、リモートリポジトリのデフォルト名とされます。

  • git push -u origin main というコマンドは、**ローカルの main ブランチの内容を origin として設定されたリモートリポジトリにプッシュ(アップロード)**します。この操作により、ローカルとリモートの両方が同期されます。

  • ローカルの変更を強制的にリモートに適用したいときは、--force オプションを使用します。

git push -u origin main --force
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?