A Git Fetch Example in Eclipse

This post continues our series of performing Git functions in Eclipse. Last time we stepped through a branching example where we left a branch we were working to address a hot fix, then switched back to the branch with the updated source.

Today we're going to exit a branch we're working in, do a fetch from the remote repository, merge it into master, go into our branch to complete our work, merge it with the updated source from the fetch, then push all of our updates back up to the remote repository.

Our Code State Before the Fetch

We're working with our Git Work project where you can see we have updates to GitworkLauncher.java staged. Shown in brackets on the project folder is our comment-work branch. Also notice in our common package we have a single GitworkUI.java file. That will change with the updates from the repository.

Fetch and Branching

We'll see how easy it is to fetch the remote updates and get back to our work by simple branching. Here's the status of GitworkLauncher.java before the fetch.

We did a $ git fetch from our remote repository. Our working directory, which includes the GitworkLauncher.java file above is untouched and unchanged. The code we fetched is in a branch called origin/master.

We now checkout the origin/master branch and see it listed in the Project Explorer. From the repository we have a new GitTempWork.java file in common and if we were to look, our GitworkLauncher.java file would not include the code in our comment-work branch.

Merging the Fetched updates

Everything looks good in origin/master so we checkout master and merge the branch. With $ git status we see that everything is up-to-date in master.

Merging Our Local Work

Back to our comment-work branch.

As you can see from the Project Explorer below, comment-work doesn't know anything about the updates we fetched, now merged in master. Our working directory is in the exact same state we were in prior to fetching from the remote repository.

We'll complete our work in the comment-work branch.

Let's save and commit our work in the comment-work branch.

Switch to Master and Push Updates to Remote Repository

With our coding in comment-work complete, we're going to do a $ git checkout master (which includes the fetched updates we merged earlier), merge the comment-work branch and push our updates to the remote repository.

As we typically do in these examples, time to check the remote repository to confirm it contains our comment-work updates as well as the source code we fetched and merged with our master branch earlier.

Posted November 27, 2015 06:28 PM EST

More Like This Post