Fetching

One thing to always keep in mind when working with GitHub is that your local repository and the remote repository are not connected and thus, don't sync up automatically when something changes on either of them.


Suppose a situation like the one described in the diagram below. We start with both the local and remote repositories synced-up. They both only have one commit (commit A). Next, we make a commit on the remote repository. Now we need to tell our local Git repository to sync up with the remote GitHub repository.

As an example, let's head on to GitHub and edit the README.md file. To do so, click on the pencil icon on the top-right corner of the file. This will open the file on edit mode. Now, edit the file. Then, scroll to the bottom of the page and click on the button. If we click on the Code tab, we'll see that next to the README.md on the list of files, the last commit time has changed to now.

Fetching 9

Now we are in the situation described in frame 2 of the diagram. Our remote repository has one more commit than our local repository. You can verify this by using the git log command.


To bring the changes into our local repository we run

git fetch remote branch

where remote is the name of the remote from where we want to fetch and branch is the name of the branch in that remote that we want to fetch. If we omit the branch option, Git will fetch all branches, and if we omit the remote option, Git will assume we want to fetch the origin remote.


Now if we look at the log again, we'll see that origin/main and origin/HEAD have both moved one commit ahead of the local HEAD and main. We can also check this by running

git branch -vv

Git is now telling us that our local main is behind the remote by 1 commit. To bring our main branch forward we merge the remote tracking branch onto the local by running

git merge origin/main

Now, main and origin/main are pointing to the same commit.