Table of contents
1. Command difference
2. Impact on current work
Fetch command to understand
Gitfetch and Git pull areGit version controlTwo commonly used commands in the system are related to getting the latest code from a remote repository, but there are obvious differences in how and how they operate. Here are the main differences between Git fetch and Git pull
1. Command difference
-
Git fetch:
- The Git fetch command is used to get the latest code from a remote repository and save it to the local repository, but does not automatically merge or modify the current working directory.
- It downloads the latest commit record of the remote repository to a separate branch of the local repository (usually a remote tracking branch, e.g.
origin/master
)middle. - With Git fetch, you can decide whether to merge by comparing the differences between local and remote tracking branches.
-
Git pull:
- The Git pull command is actually a combination of Git fetch and Git merge.
- It first gets the latest code from the remote repository (same as Git fetch) and then automatically merges the current branch of the local repository with the corresponding branch on the remote repository.
- If a conflict occurs during the merge process, Git pull stops the merge and prompts the user to resolve the conflict.
2. Impact on current work
- Git fetch: Git fetch does not make any changes to files in the working directory, it only updates the remote tracking branch in the local repository. This means you can safely use Git fetch to view the latest status of the remote repository without affecting your current work.
- Git pull: Git pull automatically merges changes from the remote repository to the current working branch, which may modify files in the working directory. If the local branch has uncommitted changes and these changes conflict with the changes from the remote branch, Git pull will not be merged and prompt the user to resolve the conflict.
Fetch command to understand
Note: git fetch only updates the version of the remote tracking branch, without changing the contents of the local branch or workspace. Here is an example to illustrate
Suppose you have the following environment settings:
- You have a remote repository with a branch called feature whose latest commit is version A (we assume it is a simple hash value, like a123456).
- You have cloned this remote repository locally, and your local repository has a corresponding remote tracking branch origin/feature, and a local branch feature. Your local feature branch is currently on version A (like a123456), and you have made some uncommitted changes on this branch (the changes are only present in the workspace and have not been temporarily stored or committed yet).
- Now that there are new commits on the feature branch of the remote repository (version B, commit ID is b123456), you want to know about the changes, but you don't want to merge them into your local feature branch yet.
You execute the git fetch command:
git fetch
This command will contact the remote repository, download the latest commit information, and update the remote tracking branch in your local repository. In this example,origin/feature will be updated to the latest commit to the remote feature branch (version B, i.e. b123456).
However, your local feature branches and workspace will not be affected in any way. Your local feature branch still points to version A (a123456), and changes in the workspace (uncommitted changes you made on version A basis) still exist.
To verify this, you can use gitlogTo view the commit history of the local feature branch, it will display the commit until version A. You can then use git log origin/feature to view the commit history of the remote tracking branch origin/feature, which will display the commit until version B.
Additionally, you can use git status to view the status of the workspace, which will show you that you have uncommitted changes (these changes are based on version A).
This example clearly shows how git fetch can only update the version of the remote tracking branch without changing the content of the local branch or workspace. If you want to merge remote changes into your local branch, you need to do gitmergeorigin/feature (if you have checked out the local feature branch) or other merge/rebase operations.
end