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 repositoriesgit remote -v - List remote repositories with URLsgit remote add origin https://github.com/username/repo.git - Add a remote repository named origingit remote remove origin - Remove the origin remotegit remote rename origin upstream - Rename the origin remote to upstreamgit remote show origin - Show detailed information about the origin remote
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 remotegit push -u origin feature - Push feature branch and set it as upstreamgit push - Push current branch to its configured upstreamgit push --force origin main - Force push main branch (use with caution)git push --force-with-lease origin main - Safer force pushgit push origin --delete feature - Delete the feature branch from the remote
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 branchgit pull origin main - Fetch and merge changes from the main branch of the origin remotegit pull --rebase - Fetch remote changes and rebase local changesgit pull --ff-only - Pull only if fast-forward is possible (don't create a merge commit)
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 remotegit fetch --all - Fetch changes from all remotesgit fetch --prune - Fetch changes and remove references to deleted remote branchesgit fetch origin main - Fetch only the main branch from the origin remote
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.
| 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) |
How git fetch works:
refs/remotes/ (e.g., refs/remotes/origin/main)refs/heads/main).git/objects directoryHow git pull works:
git fetch to retrieve remote changesgit merge (or git rebase if the --rebase option is specified)git fetchを使うべき場合:
git pullを使うべき場合:
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を実行するワークフローを好みます。これにより、リモートの変更を統合する前に、その内容を理解し、潜在的な問題を特定する機会が得られます。