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?

Git 初心者備忘録まとめ

Last updated at Posted at 2025-08-14

Git 初心者備忘録まとめ

  • 初めての投稿です.
  • 今回はLinux(特にUbuntu)のgitの使い方を備忘録として自分用に残す.
  • 私は独学でAIなどで聞きながら行っているので正確性には欠けるかもしれないのでご注意ください.

目次

手順 テーマ
1 Gitの準備
2 SSH接続での接続(おすすめ)
3 新規プロジェクトの作成
4 2回目以降の作業の流れ
5 参考サイト

1. Gitの準備

  • Git インストール
bash
# インストール
sudo apt install git

# gitのインストールを確認
git --version
  • Git にユーザー情報を設定する
bash
# ユーザー名
git config --global user.name "UserName"
# メールアドレス
git config --global user.email "user@example.com"

# 設定内容の確認
git config -l

# その他の設定(任意)
# 新規リポジトリ作成時のデフォルトブランチ名を main にする
git config --global init.defaultBranch main

# 見やすいログのエイリアス
git config --global alias.lg "log --oneline --graph --decorate --all"
  • GitHub アカウント作成
    このサイトなどを参考に作成する.

2. SSH接続での接続(おすすめ)

Gitの操作(コマンドライン)

  • パスワード入力で困らないように設定しておくのがいい
bash
# 1) SSH鍵を作成(メールはGitHubに登録済みのもの)
ssh-keygen -t ed25519 -C "user@example.com"
  • 出力
bash
# 保存する場所を変更したければ入力(デフォルトで良い場合はEnter)
Enter file in which to save the key (/home/Users/.ssh/id_ed25519):

# そのままEnterキーを押す
Enter passphrase (empty for no passphrase):

# そのままEnterキーを押す
Enter same passphrase again: 
出力結果
bash
Your identification has been saved in /home/User/.ssh/id_ed25519
Your public key has been saved in /home/User/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256: user@example.com
The key's randomart image is:
  • 入力
bash
# デフォルトの保存場所の場合はこのまま(変更した場合はそのパスを入力)
# 2) 公開鍵を表示してコピー
cat ~/.ssh/id_ed25519.pub
  • 表示された内容を全てコピー

GitHubでの操作(ウェブ上)

  • GitHubのサイトにアクセスする
  • ログインしたら右上のアカウントを押して, Settings -> 左側にあるSSH and GPG keys -> New SSH keyの順に進む.
  • Titleを入力して, Keyの部分に先程コピーしたものを貼り付ける.

最後に接続の確認(コマンドライン)

  • 入力
bash
ssh -T git@github.com
  • 出力
bash
# これが出れば接続完了
Hi User! You've successfully authenticated, but GitHub does not provide shell access.

# もし以下のような出力の場合,yesと入力してEnterを押す
The authenticity of host 'github.com (20.27.177.113)' can't be established.

3. 新規プロジェクトの作成

Gitの操作(コマンドライン)

  • git initでこのディレクトリを Git リポジトリにする
bash
# 作業するディレクトリまで移動する
# プロジェクトのフォルダの作成/移動
mkdir new-project-name
cd new-project-name

# Git 初期化
git init

# 確認(任意)
ls -a
  • main.cを作成する(好きなファイルでいい)
bash
# ファイルを作成する(今回はC言語のファイルを使う)
touch main.c

# ファイルを編集する(好きなエディタで編集)
nano main.c
  • ファイルの編集(仮)
main.c
#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}
  • コンパイル&動作確認
bash
# コンパイル
gcc main.c

# 実行
./a.out
bash
# 出力
Hello World
  • .gitignoreを編集
bash
# .gitignoreを作成
touch .gitignore

# .gitignoreを編集する(実行ファイルをアップロードしない)
nano .gitignore

*.out # この行を追記する
  • gitでコミットする
bash
# このフォルダ内の全てのファイルをステージへ
git add .

# コミット(コメントはより適切なものを書く)
git commit -m "一回目のコミット"

GitHubでの操作(ウェブ上)

  • GitHubのサイトにアクセスする
  • Create a New repositoryを押す
  • Repository nameを決め, Create repositoryを押す(このときREADMEや.gitignoreは作らない)

Gitの操作(コマンドライン)

bash
# リモート登録(SSH例)
git remote add origin git@github.com:<GitHubのユーザー名>/new-project-name.git

# ブランチ名を main にして初回プッシュ(-u は追跡設定)
git branch -M main
git push -u origin main
  • これでGitHubにアクセスして同期されているか確認する
  • ここまでの操作でGitHub上にはmain.c.gitignoreだけがあがっているはず

addを取り消したい場合は以下を参考にする

コミットを取り消したい場合は以下を参考にする

4. 2回目以降の作業の流れ

ローカルに作業するプロジェクトがない場合

  • 作業するプロジェクトのGitHubにアクセスして, Codeを押して, SSHを選択して, URLをコピーする
  • 次にコマンドラインで以下の操作をする
bash
git clone コピーしたURL

Gitの操作(コマンドライン)

  • 必ずやること(最新のmainブランチを同期させる)
bash
# mainブランチに切り替え
git switch main

# 最新のバージョンに取り込む
git pull --ff-only
  • 新しいブランチの作成(作業用の部屋を別で作るイメージ)
bash
# ブランチの確認
git branch

# 新しいブランチの作成(名前は自由)
# -c は「create」の意味 → 新しいブランチを作る
git switch -c feature/new-branch-name

# ブランチの確認(現在いるブランチが変更されていることを確認)
git branch

# ファイルに変更を加える(好きなエディタで)
nano main.c
main.c
#include <stdio.h>

int main() {
    int x = 100;
    
    printf("%d\n", x);
    return 0;
}
  • コンパイル&動作確認
bash
# コンパイル
gcc main.c

# 実行
./a.out
  • 出力
bash
100
  • 変更部分の確認/コミット
bash
# どのファイルが変わったか確認
git status
出力結果
bash
On branch feature/new-branch-name
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   main.c

no changes added to commit (use "git add" and/or "git commit -a")
bash
# 具体的な差分を見る
git diff main.c
出力結果
bash
diff --git a/main.c b/main.c
index 744d6ff..6300c99 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 
 int main() {
-    printf("Hello World\n");
+    int x = 100;
+    printf("%d\n", x);
     return 0;
 }
bash
# 変更をステージに載せる
git add main.c

# コミット
git commit -m "2回目のコミット"
出力結果
bash
[feature/new-branch-name 68db9cb] 2回目のコミット
 1 file changed, 2 insertions(+), 1 deletion(-)
bash
# GitHub(リモート)にプッシュ
git push -u origin feature/new-branch-name

GitHubでの操作(ウェブ上)

  • GitHubにアクセスする
  • Compare & pull requestボタンを押す
  • Add a title, Add a descriptionを書く
  • Create pull requestを押す
  • 変更内容を確認し, Merge pull request -> Confirm mergeを押す
  • 最後にDelete branchを押す

Gitの操作(コマンドライン)

  • mainブランチに切り替えて, 作業していたブランチを削除する
# 現在のブランチの確認
git branch

# mainブランチに切り替え
git switch main

# 作業していたブランチを削除
git branch -d feature/new-branch-name

# 作業していたブランチが削除されたか確認
git branch

参考サイト

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?