In an earlier tutorial, I showed you how to connect a VPS to Github and move files from Github to the VPS. In that tutorial, we saw how to use “git clone” to initiate a clone of a remote project that you’re starting from scratch. If you already have a repository and have been working on it for some time, you can sync the changes from the remote branch to your local branch using “git pull”. However, what if you simply want to take a look at the changes that other people have made in remote branches without necessarily overwriting your local copy?
In such a situation, “git fetch” is the command for you. In this tutorial, I’ll show you how to use “git fetch” along with a few examples.
Using Git Fetch to Obtain Remote Changes
Let’s say you’ve already cloned or pulled from a remote repository, and have been working on it for a while. Now you just want to see what other changes have been made. However, unlike a pull request, you want to review the changes and don’t necessarily want to overwrite what you have on your local machine. This is when you use git.
To use “git fetch”, the following command works:
git fetch origin
This will download the changes and you can follow the log to see what’s being downloaded. Once that’s done, you can use the following command to see the various commits that have occurred on the remote branch:
git log --oneline main..origin/main
The above two commands will generate an output like this:
You can see from the git log command, that two commits on the remote branch have been downloaded to the local branch. If you want to see more detail on the changes that were made, you can use the following code:
git log -p main..origin/main
The above command, using the “-p” flag, will show you the exact lines that were added and removed in each commit.
The Difference Between Git Fetch, Git Pull and Git Clone
All of the above three commands are used to transfer files and other changes from the remote repository to the local repository. However, they all do things slightly differently and have very different use-case scenarios. Here’s a brief explanation of the differences between each of them.
Git Clone
Git clone is used when you currently don’t have any trace of the project on your local machine. Not even a repository to start with. So git clone is used at the very beginning of the setup of a project on your local machine. Because it clones the remote repo, it also takes the greatest amount of time and data, which can be significant for large projects. You also need to run it just one time.
Git Pull
Git pull is used when you’ve already finished cloning the project, and have been working on it for some time. Maybe you’ve already committed some changes, and maybe it’s been a while since you last worked on the project, and want to catch up with the latest state. In such situations, git pull brings your local machine up to date with what’s happening on the remote repository. It not only downloads all the changes, but it also updates the copy of the project on your local system so that you have the latest state.
Unlike git clone, git pull requires a lot less data and is much faster – especially for larger projects, since it’s only downloading the changes.
Git Fetch
Get fetch is almost the same as git pull but with one major difference. It too, downloads the changes that have been made to the remote repository that are different from what you have on your local machine, but it skips the crucial step of updating your local machine with the changes.
Instead, git fetch gives you the option of monitoring the changes that you receive from the remote branch, before deciding to merge them. You can do this on an individual commit basis.
How to Merge the Changes from Git Fetch
Let’s say that you’ve used git fetch to obtain the list of changes from the remote branch and have reviewed the individual commits one by one, and that everything is satisfactory and you want to merge all the commits on the remote branch into your local copy. To do this, we simply use the “merge” command like this:
git merge origin/main
This assumes that you’re on the branch that you want to update – “main” or “master” are the usual options.
Using Git Pull to Fetch and Merge at Once
As you might have guessed, fetching and merging using the “git” command is equivalent to simply using the “pull” command to update your local repository with the remote changes. Using the pull command, however, denies you the opportunity to review the changes beforehand. If you already know that you’re going to merge all the changes you receive from fetch, you might as well use the pull command since it’s faster, and there’s a lower chance of something going wrong.
Handling Merge Conflicts
Sometimes, you might try and merge a commit that you received from git fetch into your local branch, that conflicts with a file that you’re currently working on. If this is the case, git will pause the merge for the conflicted files. It will continue working on all the other files to which the merge command applies, and which don’t require manual intervention. After this, the status of the repository indicates that a merge is in progress and that manual action is required before the status can be settled.
To resolve the conflict, you need to open the files that git told you were conflicted, examine the conflict markers, and put the file into a “final” state. Then you need to add and commit the changes like you would with any other file.
If you refuse to fix the conflicts, git won’t allow you to proceed with certain operations that require a clean working state. For example, you can no longer checkout other branches, or start new merges. One option is to simply abort the merge using the command:
git merge --abort
This will restore your working directory to the state that existed before the merge. This gives you time to perhaps collaborate with other people involved in the editing of the file and sort out the changes that need to be made to remove the conflicts from the fetch and merge commands.
Git Fetch is Useful for a Multi-Branch Environment
One of the uses for git fetch, other than simply reviewing the changes before merging them, is for a user to pull the changes from all other branches in the project to get an idea of what others are working on and get a feel for the progress of the project. This allows a project manager to plan future merges or rebases and understand the overall project status.
Conclusion
Git fetch is very similar to git pull, with the only difference being that it doesn’t automatically merge the changes into your local working directory. This allows you to examine the commits before merging them, and it’s very useful in many scenarios, especially those involving projects where you work with multiple branches.
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply