📅  最后修改于: 2023-12-03 15:00:55.726000             🧑  作者: Mango
As a programmer, Git is one of the most popular version control systems that you might use on a daily basis. One common command that you might use is git fetch
, which allows you to retrieve the latest changes from a remote repository. In this article, we'll discuss git fetch upstream
, which is a command that is specifically used to fetch changes from an upstream repository.
An upstream repository refers to the original repository from which a given repository was forked. When you create a fork, Git creates a new repository that contains all the code and history from the original repository. You can then work on this new repository and make changes, which are stored locally. When you want to pull in any changes from the original repository, you can use git fetch upstream
.
To use git fetch upstream
, you'll first need to add the upstream repository as a remote. You can do this using the following command:
git remote add upstream <upstream-repo-url>
Replace <upstream-repo-url>
with the URL of the upstream repository that you want to fetch changes from.
Once you've added the upstream repository as a remote, you can use git fetch upstream
to retrieve any changes that have been made to the upstream repository since you last fetched. This command will not merge any of these changes into your local repository - it will simply download them so that they are available for you to merge or rebase.
Once you have fetched changes from upstream, you can merge them into your local repository using the following command:
git merge upstream/master
This command will merge the changes from the upstream repository's master branch into your local repository's master branch. You can replace master
with the name of any other branch that you want to merge changes into.
Alternatively, you can rebase your local changes on top of the changes from upstream using the following command:
git rebase upstream/master
This command will apply your local changes on top of the changes from the upstream repository's master branch. This can be useful if you want to keep a clean and linear commit history.
In this article, we've discussed git fetch upstream
, which is a command that is used to retrieve changes from an upstream repository. This command can be useful if you're working on a forked repository and want to keep your code up-to-date with the original repository. We've also discussed how to merge or rebase changes from upstream into your local repository.