Skip to main content

Section 5.4 Fetching Upstream

Subsection 5.4.1 First, Fetching

To answer the question in Section 5.3, we are not quite done. See, even though your fork and the parent repository are connected, updates in one are not automatically reflected in the other. So if you just contributed to a repository and the pull request was accepted, you should be fine. But if someone else’s pull request also got approved, your fork will not have those changes. You have to tell GitHub to grab those changes and update your fork.
The act of grabbing changes from the parent repository is called fetching, aptly named since that is exactly what you are doing: fetching changes. Technically, this is distinct from a related merge. First, you fetch the changes, then you merge the changes into your fork.
You might remember from previous sections using git pull to fetch changes. In essence, using this command will fetch your changes. But it also merges the changes into your fork. In most cases, using git pull is fine and is a great shortcut. But as you get more experienced in the git world, you may want to fetch changes and then merge them later. We will explore pulling changes more in future sections.
Before continuing, you must wait until I accept your pull request from Section 5.3.

Subsection 5.4.2 Second, Upstream

Another important concept is that of upstream. Remember from Subsection 4.3.4 when we talked about remotes? Back there, I mentioned that there were two types of remotes (origin and upstream) and that we would talk about upstream later. Well, now it’s later! This idea will become important again when we use Git to collaborate (see if you can figure out why).
Any repository you own or that you clone will have the remote identifier of “origin”, for you. Any repo you do not own and that you fork will have the remote identifier of “upstream”. Remember that a remote is like a connection between repositories; it’s the definition of the link between them. When you fork a repo, you are creating a remote connection between the parent repo (upstream) and your newly-created fork (origin). As we will explore in Chapter 6, when you clone a repo, you are creating a remote connection between your fork (origin) and your personal computer (local) with the future ability to be connected to the authoritative repo (upstream).

Subsection 5.4.3 Third, Fetching Upstream

So in order to have changes from the parent repository reflected in your own fork, you have to fetch upstream. GitHub makes this easy! Remember that dialog box from Activity 5.3? Back then, we used the “Contribute” button to start a pull request. Here, we will use the option right next to it: “Fetch upstream”.
First, though, go back to the homepage of your fork. You might notice that a slightly different box has appeared. Now it might say something like “This branch is 1 commit behind ian-curtis:main” (you could have a different number). Earlier you were ahead of ian-curtis:main. What’s going on here?

Ahead vs. Behind.

GitHub uses ahead and behind to tell you how your fork currently sits compared to the repo you forked from. Any commit that you have made to your fork that has not been merged into the parent repo ticks up the number of commits ahead you are. Any commit that has been made to the parent repo that is not reflected in your fork ticks up the number of commits behind you are.
As an example, you might have a message like “This branch is 2 commits ahead, 3 commits behind <parent-repo>:main”. This means you have made 2 commits that are not on the parent repo and that the parent repo has 3 commits that you have not fetched.

Warning 5.4.3.1. Fetch Upstream Often.

In my opinion, you want to keep the least amount of commits behind. If you are working on someone else’s work, you want to stay up to date. If some big change happens, you should make sure you have that change on your fork. Why? Well, you don’t want to overwrite anything someone else just did. You also don’t want to work on adding a new feature that is already incorporated. Maybe the new change helps you do your work better/faster. You also want to avoid potential merge conflicts because someone else changed the same files you are working on.
Therefore, it is usually recommended practice to fetch upstream as often as you can. At the very least, daily, especially if the project is popular and updated frequently. It is possible that there is no new upstream content to fetch but hey, at least you checked!
This warning will repeat in Chapter 6 when we talk about pulling and pushing.
You may be wondering why you are even any commits behind! If your addition to my repository was accepted, then our repos should match, right? Not quite. Actually, that act of accepting your pull request and merging into my main branch was a commit in itself which is where the mystery commit comes from.
Enough talk, let’s fetch that upstream!
(a)
Find the “Fetch upstream” dialog box on the home page of your fork.
(b)
Notice that GitHub will fetch and merge the upstream changes in one step (even though it is technically two steps). Great!
It also emphasizes Warning 5.4.3.1 through a somewhat passive-aggressive reminder: “Keep your fork up-to-date with the upstream repository”.
To make the magic happen, click on “Fetch and merge”.
(c)
Bam. Done. But do make sure your fork now says
This branch is up to date with ian-curtis/aboutme:main
(in general, this would be the name of whatever repo you forked from). Yours may also say, depending on when you fetched, that your branch is a number of commits ahead (but none behind).
And there you go! Now you are ready to keep on editing or to go about your day. If you are interested in some other GitHub features related to collaborating, continue to Appendix B. Otherwise, go to Chapter 6 to see how to use Git commands and local edits to collaborate.