Git Remote Repository Operations Commands Guide

Remote Repository Operations

git remote - Manage Remote Repositories

Adds, removes, and displays remote repositories.

Option/Subcommand Description
add <name> <URL> Add a new remote repository
remove <name> Remove a remote repository
rename <old-name> <new-name> Rename a remote repository
-v, --verbose Display remote repository names and URLs
show <name> Display detailed information about the specified remote repository

Examples:

git remote - List remote repositories
git remote -v - List remote repositories with URLs
git remote add origin https://github.com/username/repo.git - Add a remote repository named origin
git remote remove origin - Remove the origin remote
git remote rename origin upstream - Rename the origin remote to upstream
git remote show origin - Show detailed information about the origin remote

git push - Send Local Changes to Remote Repository

Sends changes from local branches to a remote repository.

Option Description
-u, --set-upstream Set upstream branch (allows future pushes without arguments)
--force, -f Force overwrite of remote branch (caution: rewrites history)
--force-with-lease Force push only if the remote branch hasn't been unexpectedly modified
--tags Push all tags
--delete Delete a remote branch

Examples:

git push origin main - Push local main branch to origin remote
git push -u origin feature - Push feature branch and set it as upstream
git push - Push current branch to its configured upstream
git push --force origin main - Force push main branch (use with caution)
git push --force-with-lease origin main - Safer force push
git push origin --delete feature - Delete the feature branch from the remote

git pull - Incorporate Remote Changes into Local Repository

Incorporates changes from a remote branch into the local branch (fetch + merge).

Option Description
--rebase Use rebase instead of merge
--no-commit Stage changes without automatically committing
--ff-only Allow only fast-forward (don't create a merge commit)
--prune Remove references to remote branches that have been deleted

Examples:

git pull - Fetch and merge changes from the remote branch corresponding to the current branch
git pull origin main - Fetch and merge changes from the main branch of the origin remote
git pull --rebase - Fetch remote changes and rebase local changes
git pull --ff-only - Pull only if fast-forward is possible (don't create a merge commit)

git fetch - Get Remote Changes Without Merging

Retrieves changes from a remote repository but does not merge them into local branches.

Option Description
--all Fetch from all remotes
--prune Remove references to remote branches that have been deleted
--tags Fetch all tags
--depth=<depth> Fetch only history up to the specified depth

Examples:

git fetch - Fetch changes from the default remote (usually origin)
git fetch origin - Fetch changes from the origin remote
git fetch --all - Fetch changes from all remotes
git fetch --prune - Fetch changes and remove references to deleted remote branches
git fetch origin main - Fetch only the main branch from the origin remote

Differences Between git pull and git fetch - Detailed Comparison

Both git pull and git fetch are commands that retrieve data from a remote repository, but there are important differences in their behavior and use cases.

Basic Differences

git fetch git pull
Only retrieves data from the remote repository without modifying local working branches Retrieves data from the remote repository and automatically merges (or rebases if specified) into the local working branch
Safe operation: does not affect your current work May cause conflicts during the merge process
First step of a two-step process (for manual merging later) One-step operation (automatically executes fetch and merge)

Internal Operation Differences

How git fetch works:

  1. Downloads the latest commit history from the remote repository
  2. Updates local refs/remotes/ (e.g., refs/remotes/origin/main)
  3. Does not modify local working branches (e.g., refs/heads/main)
  4. Downloaded commits are stored in the local .git/objects directory

How git pull works:

  1. Executes git fetch to retrieve remote changes
  2. Then automatically executes git merge (or git rebase if the --rebase option is specified)
  3. Updates the local working branch

使い分けのポイント

git fetchを使うべき場合:

  • リモートの変更を確認してから統合したい場合
  • マージする前に変更内容を詳しく調査したい場合
  • 現在の作業を中断せずにリモートの最新状態を知りたい場合
  • 複数のリモートブランチの変更を選択的に統合したい場合

git pullを使うべき場合:

  • リモートの変更をすぐに統合したい場合
  • リモートの変更が自分の作業と競合しないことが分かっている場合
  • 迅速に最新の状態に更新したい場合

fetchした後の変更確認とマージ

fetchした後、以下のコマンドでリモートとの差分を確認できます:

git diff main origin/main - ローカルのmainブランチとリモートのmainブランチの差分を表示
git log main..origin/main - リモートにあって、ローカルにないコミットを表示
git log -p main..origin/main - 差分のあるコミットの詳細な変更内容も表示

確認後、マージする場合は:

git merge origin/main - リモートの変更をローカルブランチにマージ
git rebase origin/main - リモートの変更をベースにローカルの変更を再適用

ワークフロー例: 多くの開発者は、git fetchを使って変更を確認してから、明示的にgit mergeまたはgit rebaseを実行するワークフローを好みます。これにより、リモートの変更を統合する前に、その内容を理解し、潜在的な問題を特定する機会が得られます。