Git how to get remote branch

Git how to get remote branch

Remote Branches

Remote branches are references (pointers) to the state of branches in your remote repositories. They’re local branches that you can’t move; they’re moved automatically for you whenever you do any network communication. Remote branches act as bookmarks to remind you where the branches on your remote repositories were the last time you connected to them.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

If you do some work on your local master branch, and, in the meantime, someone else pushes to git.ourcompany.com and updates its master branch, then your histories move forward differently. Also, as long as you stay out of contact with your origin server, your origin/master pointer doesn’t move.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

To synchronize your work, you run a git fetch origin command. This command looks up which server “origin” is (in this case, it’s git.ourcompany.com ), fetches any data from it that you don’t yet have, and updates your local database, moving your origin/master pointer to its new, more up-to-date position.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Now, you can run git fetch teamone to fetch everything the remote teamone server has that you don’t have yet. Because that server has a subset of the data your origin server has right now, Git fetches no data but sets a remote branch called teamone/master to point to the commit that teamone has as its master branch.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Pushing

When you want to share a branch with the world, you need to push it up to a remote that you have write access to. Your local branches aren’t automatically synchronized to the remotes you write to – you have to explicitly push the branches you want to share. That way, you can use private branches for work you don’t want to share, and push up only the topic branches you want to collaborate on.

If you have a branch named serverfix that you want to work on with others, you can push it up the same way you pushed your first branch. Run git push (remote) (branch) :

If you’re using an HTTPS URL to push over, the Git server will ask you for your username and password for authentication. By default it will prompt you on the terminal for this information so the server can tell if you’re allowed to push.

For more information on the various credential caching options available, see Credential Storage.

The next time one of your collaborators fetches from the server, they will get a reference to where the server’s version of serverfix is under the remote branch origin/serverfix :

It’s important to note that when you do a fetch that brings down new remote branches, you don’t automatically have local, editable copies of them. In other words, in this case, you don’t have a new serverfix branch – you only have an origin/serverfix pointer that you can’t modify.

This gives you a local branch that you can work on that starts where origin/serverfix is.

Tracking Branches

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

So here we can see that our iss53 branch is tracking origin/iss53 and is “ahead” by two, meaning that we have two commits locally that are not pushed to the server. We can also see that our master branch is tracking origin/master and is up to date. Next we can see that our serverfix branch is tracking the server-fix-good branch on our teamone server and is ahead by three and behind by one, meaning that there is one commit on the server we haven’t merged in yet and three commits locally that we haven’t pushed. Finally we can see that our testing branch is not tracking any remote branch.

Pulling

While the git fetch command will fetch down all the changes on the server that you don’t have yet, it will not modify your working directory at all. It will simply get the data for you and let you merge it yourself. However, there is a command called git pull which is essentially a git fetch immediately followed by a git merge in most cases. If you have a tracking branch set up as demonstrated in the last section, either by explicitly setting it or by having it created for you by the clone or checkout commands, git pull will look up what server and branch your current branch is tracking, fetch from that server and then try to merge in that remote branch.

Generally it’s better to simply use the fetch and merge commands explicitly as the magic of git pull can often be confusing.

Deleting Remote Branches

Basically all this does is remove the pointer from the server. The Git server will generally keep the data there for a while until a garbage collection runs, so if it was accidentally deleted, it’s often easy to recover.

Git Checkout Remote Branch Tutorial

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Git is a version control tool that allows you to maintain and view different versions of your application. When a new update breaks your app, Git lets you revert those changes to the previous version.

In addition to versioning, Git allows you to work in multiple environments at the same time. Multiple environments in this context means branches.

Why you need branches

When you’re working with git, you’ll have a master (also called main) environment (branch). This particular branch holds the source code that gets deployed when your app is ready for production.

When you want to update your app, you can also add more commits (changes) to this branch. For minor changes, this may not be a big deal, but for big changes, doing this is not ideal. And that’s why other branches exist.

To create and use a new branch, you use the following command in your terminal in the project directory:

On this new branch, you can create the new changes. Then when you’re done, you can merge them with the master branch.

Another benefit of branches is that they allow multiple developers to work on the same project simultaneously. If you have multiple developers working on the same master branch, it can be disastrous. You have too many changes between each developer’s code, and this usually ends in merge conflicts.

With Git, you can jump on another branch (another environment) and make changes there, while work goes on in other branches.

What does Git Checkout Remote Branch mean?

When you begin a project with Git, you get two environments: the local master branch (which exists in your computer), and the remote master branch (which exists in a Git-supported platform like GitHub).

You can push changes from the local master branch to the remote master branch and also pull changes from the remote branch.

When you create a branch locally, it exists only locally until it is pushed to GitHub where it becomes the remote branch. This is shown in the following example:

From the example above, origin new-branch becomes the remote branch. As you may have noticed, we created a new branch and committed a change on it before pushing to the new remote branch.

But what if the remote branch already existed, and we wanted to pull the branch and all of its changes to our local environment?

That’s where we «Git Checkout Remote Branch».

How to Git Checkout Remote Branch

Let’s say there’s a remote branch created by another developer, and you want to pull that branch. Here’s how you go about it:

1. Fetch all remote branches

2. List the branches available for checkout

To see the branches available for checkout, run the following:

3. Pull changes from a remote branch

What this does is:

And now you have a copy of that remote branch. Also, you can push commits to that remote branch. For example, you make push a new commit like so:

Conclusion

Git branching makes it very easy to collaborate during application development.

With branches, different developers can easily work on different parts of the application simultaneously.

With checkout remote branch, collaboration even becomes more seamless as developers can also copy remote branches locally on their systems, make changes, and push to the remote branches.

Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I teach JavaScript / ReactJS / NodeJS / React Frameworks / TypeScript / et al

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Remote Branches

Remote-tracking branches are references to the state of remote branches. They’re local references that you can’t move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository. Think of them as bookmarks, to remind you where the branches in your remote repositories were the last time you connected to them.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

If you do some work on your local master branch, and, in the meantime, someone else pushes to git.ourcompany.com and updates its master branch, then your histories move forward differently. Also, as long as you stay out of contact with your origin server, your origin/master pointer doesn’t move.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

To synchronize your work with a given remote, you run a git fetch command (in our case, git fetch origin ). This command looks up which server “origin” is (in this case, it’s git.ourcompany.com ), fetches any data from it that you don’t yet have, and updates your local database, moving your origin/master pointer to its new, more up-to-date position.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Now, you can run git fetch teamone to fetch everything the remote teamone server has that you don’t have yet. Because that server has a subset of the data your origin server has right now, Git fetches no data but sets a remote-tracking branch called teamone/master to point to the commit that teamone has as its master branch.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Pushing

When you want to share a branch with the world, you need to push it up to a remote to which you have write access. Your local branches aren’t automatically synchronized to the remotes you write to — you have to explicitly push the branches you want to share. That way, you can use private branches for work you don’t want to share, and push up only the topic branches you want to collaborate on.

If you have a branch named serverfix that you want to work on with others, you can push it up the same way you pushed your first branch. Run git push
:

If you’re using an HTTPS URL to push over, the Git server will ask you for your username and password for authentication. By default it will prompt you on the terminal for this information so the server can tell if you’re allowed to push.

For more information on the various credential caching options available, see Credential Storage.

The next time one of your collaborators fetches from the server, they will get a reference to where the server’s version of serverfix is under the remote branch origin/serverfix :

It’s important to note that when you do a fetch that brings down new remote-tracking branches, you don’t automatically have local, editable copies of them. In other words, in this case, you don’t have a new serverfix branch — you have only an origin/serverfix pointer that you can’t modify.

This gives you a local branch that you can work on that starts where origin/serverfix is.

Tracking Branches

In fact, this is so common that there’s even a shortcut for that shortcut. If the branch name you’re trying to checkout (a) doesn’t exist and (b) exactly matches a name on only one remote, Git will create a tracking branch for you:

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

So here we can see that our iss53 branch is tracking origin/iss53 and is “ahead” by two, meaning that we have two commits locally that are not pushed to the server. We can also see that our master branch is tracking origin/master and is up to date. Next we can see that our serverfix branch is tracking the server-fix-good branch on our teamone server and is ahead by three and behind by one, meaning that there is one commit on the server we haven’t merged in yet and three commits locally that we haven’t pushed. Finally we can see that our testing branch is not tracking any remote branch.

It’s important to note that these numbers are only since the last time you fetched from each server. This command does not reach out to the servers, it’s telling you about what it has cached from these servers locally. If you want totally up to date ahead and behind numbers, you’ll need to fetch from all your remotes right before running this. You could do that like this:

Pulling

While the git fetch command will fetch all the changes on the server that you don’t have yet, it will not modify your working directory at all. It will simply get the data for you and let you merge it yourself. However, there is a command called git pull which is essentially a git fetch immediately followed by a git merge in most cases. If you have a tracking branch set up as demonstrated in the last section, either by explicitly setting it or by having it created for you by the clone or checkout commands, git pull will look up what server and branch your current branch is tracking, fetch from that server and then try to merge in that remote branch.

Generally it’s better to simply use the fetch and merge commands explicitly as the magic of git pull can often be confusing.

Deleting Remote Branches

Basically all this does is remove the pointer from the server. The Git server will generally keep the data there for a while until a garbage collection runs, so if it was accidentally deleted, it’s often easy to recover.

Easily Perform Git Checkout Remote Branch [Step-by-Step]

Read more tutorials by Devin Rich!

Table of Contents

Have you been newly onboarded to a team and told to checkout a remote branch of the Git code repository? Used by teams large and small, remote Git branches unlock powerful code collaboration abilities. In this article, learn and understand how to retrieve the code from a remote Git repository!

Table of Contents

Prerequisites

Other potential installation methods for Windows are Chocolatey and Git4Win.

What is a Remote Branch?

A remote branch is a branch that exists on a remote Git repository. These remote repositories, referred to as “remotes,” are where your local Git repository will look for external commit updates once configured.

Find out what branches are available with the Git branch command. The starred ( * ) branch is the currently active branch. The branches shown below are only local branches as no remote branches exist yet.

What is Git Checkout Remote Branch?

Say you have a local Git repository and need to start collaborating with others. To do that, leverage the Git checkout command to retrieve the latest commits from a remote branch on a remote Git repository.

With a configured remote branch, Git will instead mirror a remote branch instead of a local-only branch. An example of the command git checkout remote
is shown below.

Clone the Remote Git Repository

As an existing repository exists, you are ready to get started. Follow along below to clone the remote repository to a local folder.

1. Cloning a remote repository may be easiest via the HTTPS URL rather than SSH to avoid certificate and firewall issues.

2. Once you’ve copied the HTTPS URL to your clipboard, open a command-line using your favorite CLI, such as Windows Command Prompt or the Windows Terminal.

3. Navigate to a folder where you want the cloned the repository to reside. This tutorial uses the git folder in the home directory as you will see in the screenshots. Run the following command to clone the repository, which creates a sub-folder containing the remote repository.

Run the following status command to verify that the repository has been successfully created. The Git status command displays differences between the local branch and remote branch, useful to determine if your branch is connected and up to date! The default Git remote is named origin as prefixed to the main remote branch. git status

What is a Git Remote and How to Track Branches

Cloning a repository automatically creates a reference to a remote source, known as a remote. This reference is used to check for new commits or push new changes to the remote repository.

A Git repository itself may have multiple remotes, but a branch can only reference a single remote.

Tracking a remote branch creates a relationship to a local branch. This relationship allows you to easily push or pull commits the remote branch to the local branch. In addition, a tracked branch determines how far ahead or behind in commits the local branch is from the remote.

Listing a Git Repositories Remotes

To see more detail about the configured remotes, add the v switch to display the full Git remote URI.

Viewing Available Remote Branches

Display all available branches, both local and remote, in your repository with the branch command. To show the remote branches as well, append the all switch, as shown below.

In the below screenshot, you can see that the remote repository origin that the tutorial is using is shown as expected. However, this screenshot is missing all branches from the remote named Remote2.

If you know that a branch is on your remote, but it isn’t showing on the branch list, you must update your list of available remote branches as covered in the next section.

Updating the Available Remote Branches

Sometimes a remote repository may add or change branches. To make sure you are working with the most up to date branches, you may need to run the git fetch command.

The fetch command will connect to the remote repository and retrieve a list of all available branches. View your currently tracked branches after running the git fetch command to see that the remote branch is now visible, as demonstrated below.

You can see below that the branch remotes/origin/RemoteBranch is now showing in the list of available remote branches.

Fetch itself only checks for updates from the remote that is currently tracked in the current branch. To have Git check for updates from all remotes in the repo, regardless of tracking status, add the all parameter.

Viewing Each Branch’s Remote Tracking

Changing a Branch’s Remote Tracking

You may need to change your local branch to track a different remote branch. To set your current branch to a specific remote branch, track the remote branch with the u parameter, as shown below.

Checking out Git Remote Branches

With newly created branches on the remote Git repository, you may need to checkout a different branch. Checking out a remote repository branch is done through the checkout command.

To perform the checkout, use the git checkout command and type the name of the remote branch as seen below.

If your local repository already contains a local branch with the same name, add the b parameter to specify an alternate local branch name before the remote branch to checkout.

If your local repository has multiple remotes with the same branch name, specify which remote and branch to track local commits against.

Future versions of Git may possibly prefer that you use the git switch command for changing and creating branches. The Git switch command is only for branches while the Git checkout command has many abilities. Irshad S. wrote a nice primer on the rationale if you’d like to read more about it.

How to Push a Local Branch to a Remote

If you create a local branch that doesn’t have any remote branch for tracking, you can push the branch to the remote via the git push command. The simplest method to create a new branch on your desired remote is with the u parameter.

There are times when the current local branch does not match the remote branch’s name. To use a different name, specify the desired branch name after the source is joined by a colon, as seen below.

Next Steps and Additional Resources

You’ve now reviewed the basics of Git, how to manage remotes, and how to track remote branches. You’ve also learned several ways that you can check out a remote branch and some best practices when using Git. This knowledge will help save you from frustration in the future.

If you need to read up on some other aspects of managing a Git repository, be sure to check out these good Git resources that are available:

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

More from ATA Learning & Partners

Recommended Resources!

Recommended Resources for Training, Information Security, Automation, and more!

Get Paid to Write!

ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

ATA Learning Guidebooks

ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

Remote Branches

Remote-tracking branches are references to the state of remote branches. They’re local references that you can’t move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository. Think of them as bookmarks, to remind you where the branches in your remote repositories were the last time you connected to them.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

If you do some work on your local master branch, and, in the meantime, someone else pushes to git.ourcompany.com and updates its master branch, then your histories move forward differently. Also, as long as you stay out of contact with your origin server, your origin/master pointer doesn’t move.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

To synchronize your work with a given remote, you run a git fetch command (in our case, git fetch origin ). This command looks up which server “origin” is (in this case, it’s git.ourcompany.com ), fetches any data from it that you don’t yet have, and updates your local database, moving your origin/master pointer to its new, more up-to-date position.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Now, you can run git fetch teamone to fetch everything the remote teamone server has that you don’t have yet. Because that server has a subset of the data your origin server has right now, Git fetches no data but sets a remote-tracking branch called teamone/master to point to the commit that teamone has as its master branch.

Git how to get remote branch. Смотреть фото Git how to get remote branch. Смотреть картинку Git how to get remote branch. Картинка про Git how to get remote branch. Фото Git how to get remote branch

Pushing

When you want to share a branch with the world, you need to push it up to a remote to which you have write access. Your local branches aren’t automatically synchronized to the remotes you write to — you have to explicitly push the branches you want to share. That way, you can use private branches for work you don’t want to share, and push up only the topic branches you want to collaborate on.

If you have a branch named serverfix that you want to work on with others, you can push it up the same way you pushed your first branch. Run git push
:

If you’re using an HTTPS URL to push over, the Git server will ask you for your username and password for authentication. By default it will prompt you on the terminal for this information so the server can tell if you’re allowed to push.

For more information on the various credential caching options available, see Credential Storage.

The next time one of your collaborators fetches from the server, they will get a reference to where the server’s version of serverfix is under the remote branch origin/serverfix :

It’s important to note that when you do a fetch that brings down new remote-tracking branches, you don’t automatically have local, editable copies of them. In other words, in this case, you don’t have a new serverfix branch — you have only an origin/serverfix pointer that you can’t modify.

This gives you a local branch that you can work on that starts where origin/serverfix is.

Tracking Branches

In fact, this is so common that there’s even a shortcut for that shortcut. If the branch name you’re trying to checkout (a) doesn’t exist and (b) exactly matches a name on only one remote, Git will create a tracking branch for you:

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

So here we can see that our iss53 branch is tracking origin/iss53 and is “ahead” by two, meaning that we have two commits locally that are not pushed to the server. We can also see that our master branch is tracking origin/master and is up to date. Next we can see that our serverfix branch is tracking the server-fix-good branch on our teamone server and is ahead by three and behind by one, meaning that there is one commit on the server we haven’t merged in yet and three commits locally that we haven’t pushed. Finally we can see that our testing branch is not tracking any remote branch.

It’s important to note that these numbers are only since the last time you fetched from each server. This command does not reach out to the servers, it’s telling you about what it has cached from these servers locally. If you want totally up to date ahead and behind numbers, you’ll need to fetch from all your remotes right before running this. You could do that like this:

Pulling

While the git fetch command will fetch all the changes on the server that you don’t have yet, it will not modify your working directory at all. It will simply get the data for you and let you merge it yourself. However, there is a command called git pull which is essentially a git fetch immediately followed by a git merge in most cases. If you have a tracking branch set up as demonstrated in the last section, either by explicitly setting it or by having it created for you by the clone or checkout commands, git pull will look up what server and branch your current branch is tracking, fetch from that server and then try to merge in that remote branch.

Generally it’s better to simply use the fetch and merge commands explicitly as the magic of git pull can often be confusing.

Deleting Remote Branches

Basically all this does is remove the pointer from the server. The Git server will generally keep the data there for a while until a garbage collection runs, so if it was accidentally deleted, it’s often easy to recover.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *