Git: Fetch and Pull
Fetching and pulling are used to get updates from a remote repository. The key difference is how they integrate those changes.
Fetch Changes
Basic Fetch
Fetch changes from remote
git fetch [remote]Downloads commits, files, and refs from a remote repository into your local repository without automatically merging them. This lets you inspect the changes before integrating them.
Fetch and prune
Fetch changes and remove deleted branches
git fetch --prune [remote]This command fetches changes from the remote and removes any remote-tracking references that no longer exist on the remote.
Pull Changes
Pull changes from remote
git pull [remote] [branch]Fetches changes from a remote and immediately merges them into your current branch. Like `git push`, if you don't specify [remote] and [branch], it defaults to your upstream branch.
Pull with rebase
git pull --rebase [remote] [branch]Fetches changes from a remote and rebases your current branch on top of them, instead of merging. This can help maintain a cleaner commit history.
Pull with rebase and autostash
git pull --rebase --autostash [remote] [branch]Similar to `git pull --rebase`, but automatically stashes any local changes before rebasing and applies them afterward. This is useful if you have uncommitted changes that would otherwise block the rebase.
Pull with rebase and autostash from upstream
git pull --rebase --autostash upstream [branch]Fetches changes from the upstream remote and rebases your current branch on top of them, automatically stashing and applying local changes as needed.
Pull with rebase and autostash from origin
git pull --rebase --autostash origin [branch]Fetches changes from the origin remote and rebases your current branch on top of them, automatically stashing and applying local changes as needed.
Pull with fast-forward only
git pull --ff-only [remote] [branch]Fetches changes from a remote and merges them into your current branch only if it can be fast-forwarded. This prevents accidental merge commits.
The `fast-forward only` option ensures that your branch history remains linear and avoids unnecessary merge commits.