Run git fetch --prune to Update
In this step, we'll learn how to update our local Git repository with the latest changes from a remote repository. Imagine you're working on a project with others, and they've made changes and pushed them to a central server. You need a way to get those changes into your local copy.
The command we use for this is git fetch
. This command downloads commits, files, and refs from a remote repository into your local repository. It's like getting the latest updates from the server without actually merging them into your current work.
We'll also use the --prune
option. This option removes any remote-tracking branches that no longer exist on the remote. This helps keep your list of remote branches clean and up-to-date.
Let's assume you have a remote repository configured (we'll cover how to add remotes in a future lab). For now, we'll simulate fetching from a remote.
Open your terminal, make sure you are in your ~/project/my-time-machine
directory, and run the following command:
cd ~/project/my-time-machine
git fetch --prune origin
You might see output similar to this (the exact output depends on the remote repository):
From origin
* [new branch] feature/new-feature -> origin/feature/new-feature
- [deleted] (none) -> origin/old-branch
This output shows that Git has fetched new branches (feature/new-feature
) and removed branches that no longer exist on the remote (old-branch
).
Running git fetch --prune
is a good practice to keep your local view of the remote repository accurate. It allows you to see what changes have been made by others before you decide to integrate them into your own work.