Skip to main content

Command Palette

Search for a command to run...

The simplest method for fetching a remote branch in Git.

Published
2 min readView as Markdown
The simplest method for fetching a remote branch in Git.
D

My journey as a self-taught developer began with a burning curiosity. Growing up in Uganda, where resources were scarce, I couldn't afford a formal education. Instead, I relied on handwritten notes from friends and online tutorials. As time went on, I got good at building websites for local businesses, each project proving my self-taught skills. But my dream of joining a major tech company seemed unreachable. Rejections piled up, all due to my academic background and self-doubt crept in. Then, a turning point. A friend I had shared my knowledge with landed a job at a promising startup in Kampala. He believed in me and recommended me. For five years now, I've been working remotely for that very company. They saw the potential in passionate, self-taught talent. But my heart is saddened by the millions in Uganda and the world facing the same challenges. That's why I am sharing my knowledge on this platform and other social platforms. My expertise is in PHP, JavaScript, WordPress, Technical Writing and business leadership. If you want to learn from me or collaborate, consider to follow or send me a on X.com/davidofug

Assume you're a team member and a developer sends you a Pull Request to merge.

The developer creates their own branches to ensure proper code tracking and code quality.

These branches will generate Pull Requests, which must be merged into the main branch.

You don't want to merge code if you're not sure if it works. To test the code, run it in an environment that is not accessible to your product's users.

Apart from being a Control Version System, git has some amazing features, one of which is the Fetch. That is precisely the purpose of this resource. I'll show you the simplest methods for fetching branches from remote git.

Let's dive in.

The git switch command is a new command to Git. The command, as the name implies, will switch to a specified local branch. Continue reading to find out more.

Syntax

git switch [branch_name]

Example

Assuming the local branch is bar, you would

git switch bar

Something to note about the git switch is that it will fetch updates of the remote branch and save you a lot of typing time.

However, if the branch does not exist in the local repository, make use of two commands.

git fetch
git switch bar

An alternative and largely trusted method of fetching a specific branch from the remote repo is.

  1. git fetch [remote_repo_name] [remote_branch]:[local_branch]
  2. git checkout [local_branch]

Example

Assuming;-

  • Remote repository alias is origin
  • Remote branch name is foo
  • Local branch name is bar

The procedure will be like the one below.

git fetch origin foo:bar
git checkout bar

Let me know what you think about this tip. Share your thoughts in the comment section below.

More from this blog

David Wampamba

30 posts

David is a passionate teens and youths trainer, STEAM evangelist, employment advocate, entrepreneur and Acumen Fellow whose mission is empower young generations through tech, creation of jobs and enhancing education.