0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Git使い方備忘録

Last updated at Posted at 2025-07-31

コマンド記述において、<>は中身の内容を表し、「<」「>」は実際に入力しないことに注意。

remoteリポジトリの設定

新たにremoteリポジトリに設定するリポジトリを指定するためのコマンド。urlは画像の下矢印をクリックすると見つかる。
Qiita.png

git remote add origin <url>

古いリモートリポジトリ設定の削除は以下のコマンドを実行

git remote remove origin

ローカルリポジトリからremoteリポジトリにpush

cdコマンドを使ってローカルリポジトリに移動。ローカルリポジトリ内で以下のコマンドを実行

git add <相対Path>

push対象がリポジトリ内の全てのファイル/フォルダーであれば以下を実行。

git add .

続いて、addしたファイルをコミットする。以下のコマンドを実行。-mで書くコメントはcommitの概要を記述する。

git commit -m "<コメント>"

commitした内容をremoteリポジトリにpushする。pushが完了すると、github上に変更が反映されるので、web上から確認可能になる。初めてpushする場合はブランチができていないので以下のコマンドを実行してpushとともにmainブランチを作る。mainは別の名前でも良い。

git push --set-upstream origin main

二回目以降のpushは、同一ブランチであれば以下のコマンドで良い。

git push

remoteリポジトリのmainブランチからローカルリポジトリへpull

現在のローカルリポジトリにリモートリポジトリからファイルをpull(持ってくる)には、以下のコマンドを実行する。

git fetch orign
git pull origin/main -- <ファイル名>

以下のコードはリポジトリを丸ごと持ってくることができる。

git pull

branch操作

ブランチの確認方法

  • 現在のローカルリポジトリ内のブランチ一覧を取得するには以下のコマンドを実行する
    実行結果で、*がついているブランチが現在いるブランチである。
git branch
  • remoteブランチを含めた全てのブランチ一覧を取得するには以下のコマンドを実行する
git branch -a

ブランチの作成

  • ローカルブランチを作成するには以下のコマンドを実行
git branch <ブランチ名>
  • ローカルブランチを作成して、そのブランチへ移動するには以下のコマンドを実行
git checkout -b <ブランチ名> //旧形式
git switch -c <ブランチ名> //Git2.23以降新形式
  • リモートブランチを作成するには以下のコマンドを実行する。ローカルリポジトリのブランチをpushすることで作成する
git push -u origin <ブランチ名>

ブランチの削除

  • ローカルブランチの削除には以下のコマンドを実行する
git branch -d <ブランチ名>
  • リモートブランチの削除は以下のコマンドを実行する
git push origin --delete <ブランチ名>

ローカルリポジトリ内のブランチ変更

ローカルリポジトリ内の現在ブランチを変更するには以下のコマンドを実行すれば良い。

git checkout <ブランチ名>
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?