The Gitfetchcommand lets you look before you leap.

What Is git fetch, and What Does It Do?

Your local files remain untouched.

Linux laptop showing a terminal window with a globe pattern inthe background and a binary watermark

fatmawati achmad zaenuri/Shutterstock

Why is that important?

You have to keep your wits about you when you’re working as part of a development team.

Or, to put it another way, thegit pullcommand is like agit fetchfollowed immediately by agit merge.

Using the git fetch command on the default remote repository

By default, the first remote repository is called “origin.”

How to Fetch a Single Branch

Fetching information about a single branch is easy.

Addthe name of the branchto the command line to tellfetchyou only need to know about that one branch.

Looking at the branches that exist on the default remote

Here, we’re tellingfetchto retrieve the branch “mary-feature” from the remote repository “origin.”

This won’t over-write any existing files if it’s the first time you’ve used this branch.

Note that the local and remote branches have two periods “..” separating them.

Using the git tag command to list the tags in the local repository

The–onelineoption shows thecommitidentifier and the commit message.

The one-line display is useful if a branch contains a great many changes.

To see a little more information, omit the–onelineoption.

Using the –dry-run option so see the changes the fetch command would retrieve

We’ll checkout the branch to check that we’re working it is our current, working branch..

The branch is checked out for us, and we’re told that it is behind the remote version.

We can usegit pullto update it, thengit statusto check our status.

Using git fetch to retrieve a single remote branch

We need to tell Git which remote branch we want to merge.

Aneditoropens to allow us to provide a commit message.

We can accept the suggested message, or add our own.

Safely checking out a retrieved remote branch to a new local branch

The editor is yourdefault editorunless Git has been configured to use a different editor.

back up your changes when you are ready to proceed.

The merge carries on automatically when the editor is closed.

Using git to fetch the remote changes and git log to show the changes

Our merge was successful because there were no conflicts.

Conflicts arise when the same lines of code are changed by two or more developers.

If that is the case,Git marks up the conflicts in the affected file.

You’ll need to review them in turn and choose which change to keep.

If you actually want what the puppy has shown you, you’ll need topull.

Related:Git rebase: Everything you better Know