gitの基本的なコマンド (add, branch, switch, merge, commit, push)

gitの基本的なコマンド

初めにやる操作

git init
git add -A
git status
git commit -m "initinal commit"

git remote add <name> <url>
git branch -M main
git push -u origin main

git init: gitレポジトリを初期化
git add -A: ファイルの変更(追加、編集、削除) をステージングする
git status: gitレポジトリの状態を確認
git commit -m "initinal commit": コメント付きでプッシュ

git remote add origin <url>: local な git repository を、github の remote repository に追加して、origin と名付ける。
git branch -M main: current repository を main に renameする。
git push -u origin main: remote repository の origin に、local repository の main branch を push する。-u をつけると、以降はこのコマンドを git push と省略可能。

mainブランチで大元のコードができたら

git switch -c <branch-name>
git branch

# 変更を加える

git status
git add -A
git status
git commit -m "modify some codes"

git switch main
git merge <branch-name>
git branch -d <branch-name> (任意)

git push

git switch -c <branch-name>: <branch-name> という branch を作成して移動。
git branch: branch 一覧と、現在の branch を確認。

git merge <branch-name>: 現在の branch と、<branch-name>を merge する。
git branch -d <branch-name>: <branch-name> を削除(任意)

git push: git push -u origin main の省略形

まとめ

gitの操作は間違えるとかなり重大なので、自分なりの方法を覚え書きとしてまとめました。自分は正直適当なのであまり参考にしないほうがいいと思います。