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?

【初心者向け#4】Gitの基本操作入門|ローカルからGitHubへpushするまで(2026)

0
Last updated at Posted at 2026-05-01

0. はじめに

Gitの環境構築については、以下の記事にまとめています。
【初心者#2】バージョン管理 Git&Git hubの導入
今回は、構築した環境を使って、GitとGitHubを用いた基本的なファイル管理手順について解説します。

1. Gitリポジトリの設定

① Gitで管理する場所を初期化する

Gitリポジトリを新規作成します。リポジトリとは、ファイルやディレクトリの変更履歴を記録するための領域です。

ローカル(PC上)に存在する「ローカルリポジトリ」と、GitHub上にある「リモートリポジトリ」に分かれます。本記事では、まずローカルリポジトリの作成を行います。

管理したいディレクトリに移動し、以下のコマンドを実行します。

% git init

② Gitの管理から除外するファイルを指定

Gitの管理する必要のないファイルやフォルダを .gitignore に記述することで追跡対象から除外できます。.gitignoreファイルを作成して、一括して除外したいファイルを記述します。

  • 一時ファイル(*~ など)
  • 実行ファイル(.exe など)
  • 仮想環境(.venv など)
  • データファイル(.csv など)
  • ビルド生成物やキャッシュ

.gitignore の例を以下に示します。

# .gitignore(for Python environment)
# Ignore files
*~
*.exe
*.csv
*.txt
bin/
.venv

作成した.gitignoreをリポジトリに追加します。

% git add .gitignore 
% git status --ignored

これで、.gitignoreで記載したファイルは管理対象外になりました。

③ Gitで管理するファイルを指定

次に、Gitで管理したいファイルやフォルダを追加します。ファイル名、もしくは、ディレクトリ名で管理したい対象を指定します。

% git add .
% git add <file1>
% git add <directory1>

git statusコマンドを使って、追加された内容を確認しておきます。

% git status

出力例:

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   .gitignore
    new file:  <file1>
	new file:  <directory1>

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	<directory2>/
	<directory3>/

指定した.gitignore、file1、directory1は管理対象内になっていることがわかります。一方、Gitリポジトリを作成したディレクトリ直下にあっても、指定をしていないファイル(directory2、directory3)は対象外になっています。

この状態を保存します。保存するためにはgit commitコマンドを使います。オプションとして-mの後に、変更内容がわかるようなコメントを入れておきます。今回は、初回のコミットなので、"First Commit"としておくこととします。

git commit -m "First Commit"

これで管理対象の登録が完了しました。

2. GitとGitHubの同期

① GitからGitHubへのアップロード

リモート側の接続URLを設定します。その後、git pushというコマンドで、コミット(登録)したファイルをアップロードします。

% git remote set-url origin git@github.com:**.git
% git push

以上で、ローカルの変更をGitHubへ同期できます。
GitHub上でファイルが反映されていることを確認してください。

次の記事では、共同開発において使うコマンドやブランチ運用について説明します。

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?