Git checkout remote branch: how it works and when it is good to use. Should we know about checkout remote branch?

Written by Akshay Gorad. Posted in Git No Comments

Git checkout remote branch: how it works and when it is good to use. Should we know about checkout remote branch?

Git is a fantastic tool that many developers use for version control of their projects. Although there are many other version control systems – such as Subversion (SVN) and Competitor Versioning System (CVS) – git is by far the most commonly used. A good reason for this is the emphasis on distributed development and ease of use of branches. Let’s take a look at branches in git and what git checkout remote branch actually means.

What is a branch?

Git branch is a simple way to deviate from the main development pipeline. It is commonly used to develop a new feature or fix in a branch. This way, you encapsulanrate the changes and keep your main or main branch clean. The branches in git are very useful because they are easy to create and relatively inexpensive. Unlike other version control systems, in git, a branch is not a copy of your code but simply a pointer to the original node from which the branch originates.

How do I checkout a branch?

If you already have a branch on your local machine, you can simply check out or switch to that branch using the command git checkout <branch name>.

When you want to create a new branch from your main branch with the name “dev”, for example, use git branch dev—this only creates the branch. If you want to work in this branch and commit to it, you need to check out this branch just like before using git checkout dev.

Note: When you extract a branch from your local machine, all validations will be on the new branch and not on the main branch. Knowing this, you can also create a branch from a branch recursively. This may sound strange, but imagine that you are creating a new feature in a new branch and you want to experiment a little. It makes perfect sense to do this in a separate level branch that comes from your features branch.

How do I checkout a remote branch?

A remote branch is the best way to share your development work with other people on your team. It is good to mention that git checkout remote branch is not an actual existing command. If you want to check out a remote branch someone published, you first have to use git fetch. This command downloads references from your remote repository to your local machine, including reference to the remote branch. Now all you need to do is use git checkout <remote branch name>

How do I create a local branch from a remote branch?

After a fetch, you can extract the remote branch as mentioned above. This means that a local copy of the branch is available on your machine. If you want to extract a remote branch but name it differently on your local machine, you can run:

git checkout -b myLocalName origin/remoteName

Your local branch name, myLocalName will be connected to the remote branch remoteName. Note that origin is the standard reference to the original remote repository my project was cloned from. This can be different, for instance, when you are working with multiple remotes.

How do I turn my local branch into a remote branch?

If you’re on a local branch myNewFeature and want to share this branch remotely you have to set the upstream to make it a remote branch.

When I want to push my changes, first I have to use -u or –set-upstream like this:

git push -u origin myNewFeature

Now the local branch also has a remote counterpart. The next time I want to push changes I can just use git push without any parameters.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments