Git Tutorial: Pull
git pull is a simple and useful way to synchronize local branches with a remote repository. It combines the steps of **fetch** and **merge** and helps integrate the latest changes into the local repository. In cases where more control is desired, `git pull --rebase` or a separate execution of `git fetch` and `git merge` can be useful.
Bernd Fischer
12. Oct 2024 · 3 min read
What is git pull?
git pull is a Git command that combines two operations:
1. Fetch – Git fetches the latest changes from a remote repository.
2. Merge – Git integrates the changes into the current branch of the local repository.
In other words: git pull retrieves the latest commits from a remote branch and attempts to automatically merge them with the current branch.
Syntax of git pull
The basic syntax for the command git pull is:
git pull <Remote-Repository> <Branch>
- Remote repository: The name of the remote repository from which to fetch changes (e.g.,
origin). - Branch: The branch from which to fetch changes (e.g.,
main).
Example: Fetch current changes from the main branch
git pull origin main
In this example, the latest changes from the main branch of the remote repository origin are fetched and merged into the current branch.
1. Fetch and Merge – What happens during a git pull?
As already mentioned, git pull performs two steps: fetch and merge. These steps can also be executed separately to have more control over the process.
Fetch: Retrieve changes
Using git fetch, changes from the remote repository are downloaded into the local repository but are not yet integrated into the current branch.
git fetch origin main
This command fetches changes from the main branch but does not integrate them. These changes are stored in a special area called FETCH_HEAD.
Merge: Integrate changes
After fetching the changes with git fetch, the changes can be merged manually:
git merge origin/main
This command merges the downloaded changes from the main branch into the current branch.
2. Automatic merge conflict
If there are no conflicts, git pull will automatically merge the changes. However, merge conflicts may occur if local changes do not align with changes in the remote repository. In this case, the conflict must be resolved manually.
Example: Handling a merge conflict
Suppose a conflict arises after running git pull. Git will usually display an error message and mark the affected files. Here's how to resolve a conflict:
- Open affected files: Git adds markers like
<<<<<<<,=======und>>>>>>>within conflict files to indicate conflict areas. - Resolve conflict manually: The marked sections in the file must be edited manually to resolve the conflict.
- Confirm changes: After resolving the conflict, add the changes and create a commit.
git add <DateienMitKonflikt>
git commit -m "Konflikt gelöst"
3. Quick update with git pull without conflicts
If there are no local changes or the local changes fully match the remote changes, git pull executes smoothly and the changes are merged. This is the most common case in daily Git usage.
Example: A simple git pull
git pull
If no specific remote repository or branch is specified, git pull fetches changes from the default remote repository (usually origin) and the current branch.
4. git pull --rebase
An alternative to the standard merge is to use git pull --rebase. Instead of merging changes, local commits are "rebased" onto the latest version of the remote branch.
Example: git pull --rebase
git pull --rebase origin main
- Rebase: Local commits are applied onto the latest version of the
mainbranch, avoiding additional merge commits and leading to a "cleaner" commit history.
Rebase is especially useful for avoiding merge commit clutter and maintaining a linear history.
5. Difference between git pull and git fetch
git fetch: Retrieves changes from the remote repository without integrating them into the current branch.git pull: Retrieves changes and merges them directly (Fetch + Merge).
Using git fetch provides more control, as it allows executing the merge step manually and reviewing changes before merging.
Example: git fetch and then merge
git fetch origin main # Änderungen abrufen
git log FETCH_HEAD # Änderungen prüfen
git merge origin/main # Änderungen zusammenführen