Git Checkout Branch Again From Remote

Remote Branches

Remote branches are references (pointers) to the country of branches in your remote repositories. They're local branches that you tin can't motility; they're moved automatically for you lot whenever you practise whatsoever network advice. Remote branches act as bookmarks to remind you where the branches on your remote repositories were the last time y'all continued to them.

They take the class (remote)/(branch). For instance, if yous wanted to encounter what the master branch on your origin remote looked like every bit of the last fourth dimension you communicated with it, you would check the origin/master branch. If you lot were working on an issue with a partner and they pushed up an iss53 branch, you might accept your own local iss53 co-operative; but the branch on the server would betoken to the commit at origin/iss53.

This may exist a bit confusing, so let's look at an example. Let's say yous have a Git server on your network at git.ourcompany.com. If you clone from this, Git's clone command automatically names it origin for yous, pulls down all its data, creates a pointer to where its master branch is, and names it origin/master locally. Git likewise gives yous your own local principal branch starting at the same place as origin's chief co-operative, then you have something to work from.

Annotation

"origin" is non special

Just like the branch proper name "master" does not have any special meaning in Git, neither does "origin". While "master" is the default proper noun for a starting co-operative when you run git init which is the merely reason information technology'due south widely used, "origin" is the default name for a remote when yous run git clone. If you lot run git clone -o booyah instead, and so you will have booyah/chief equally your default remote branch.

Server and local repositories after cloning.

Figure 30. Server and local repositories after cloning

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

Local and remote work can diverge.

Effigy 31. Local and remote piece of work tin diverge

To synchronize your piece of 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 information from information technology that you don't however accept, and updates your local database, moving your origin/master arrow to its new, more upwards-to-date position.

`git fetch` updates your remote references.

Figure 32. git fetch updates your remote references

To demonstrate having multiple remote servers and what remote branches for those remote projects wait like, let's assume you have another internal Git server that is used but for development by ane of your sprint teams. This server is at git.team1.ourcompany.com. Y'all can add it as a new remote reference to the project you're currently working on by running the git remote add command as we covered in Git Basics. Name this remote teamone, which will be your shortname for that whole URL.

Adding another server as a remote.

Figure 33. Adding some other server as a remote

Now, you lot tin run git fetch teamone to fetch everything the remote teamone server has that you lot don't have yet. Because that server has a subset of the information your origin server has correct now, Git fetches no data but sets a remote branch chosen teamone/master to indicate to the commit that teamone has as its chief branch.

Remote tracking branch for `teamone/master`.

Figure 34. Remote tracking co-operative for teamone/master

Pushing

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

If you have a branch named serverfix that you want to work on with others, yous can push information technology upward the same way you lot pushed your first co-operative. Run git button (remote) (branch):

              $ git push button origin serverfix Counting objects: 24, done. Delta compression using up to 8 threads. Compressing objects: 100% (xv/15), done. Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done. Total 24 (delta ii), reused 0 (delta 0) To https://github.com/schacon/simplegit  * [new branch]      serverfix -> serverfix            

This is a fleck of a shortcut. Git automatically expands the serverfix branchname out to refs/heads/serverfix:refs/heads/serverfix, which means, "Take my serverfix local branch and push it to update the remote's serverfix branch." Nosotros'll get over the refs/heads/ part in particular in Git Internals, only you can generally get out it off. You tin also exercise git push button origin serverfix:serverfix, which does the same affair – it says, "Take my serverfix and make information technology the remote'south serverfix." You lot can use this format to push a local branch into a remote branch that is named differently. If you didn't want it to be called serverfix on the remote, you could instead run git push origin serverfix:awesomebranch to push your local serverfix branch to the awesomebranch branch on the remote project.

Note

Don't type your password every time

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

If you lot don't want to type it every sinlge fourth dimension you push, you can prepare a "credential cache". The simplest is just to keep it in memory for a few mintues, which you tin easily prepare by running git config --global credential.helper enshroud.

For more information on the various credential caching options bachelor, run across Credential Storage.

The next time one of your collaborators fetches from the server, they volition get a reference to where the server'southward version of serverfix is nether the remote branch origin/serverfix:

              $ git fetch origin remote: Counting objects: 7, washed. remote: Compressing objects: 100% (2/two), done. remote: Total iii (delta 0), reused 3 (delta 0) Unpacking objects: 100% (iii/3), washed. From https://github.com/schacon/simplegit  * [new branch]      serverfix    -> origin/serverfix            

It'southward important to note that when you do a fetch that brings downwardly new remote branches, you don't automatically have local, editable copies of them. In other words, in this case, you don't take a new serverfix branch – you only take an origin/serverfix pointer that you tin't alter.

To merge this work into your current working branch, you can run git merge origin/serverfix. If yous want your ain serverfix branch that you can work on, y'all can base of operations information technology off your remote branch:

              $ git checkout -b serverfix origin/serverfix Branch serverfix set to track remote co-operative serverfix from origin. Switched to a new branch 'serverfix'            

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

Tracking Branches

Checking out a local branch from a remote branch automatically creates what is called a "tracking branch" (or sometimes an "upstream branch"). Tracking branches are local branches that accept a direct relationship to a remote branch. If y'all're on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into.

When y'all clone a repository, it generally automatically creates a principal branch that tracks origin/master. Even so, you can set up other tracking branches if you wish – ones that rails branches on other remotes, or don't track the master branch. The uncomplicated case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. This is a common plenty operation that git provides the --track shorthand:

              $ git checkout --runway origin/serverfix Branch serverfix set to track remote branch serverfix from origin. Switched to a new branch 'serverfix'            

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

              $ git checkout -b sf origin/serverfix Branch sf fix to track remote branch serverfix from origin. Switched to a new co-operative 'sf'            

Now, your local branch sf will automatically pull from origin/serverfix.

If yous already have a local co-operative and desire to set it to a remote co-operative you simply pulled down, or want to alter the upstream branch yous're tracking, you tin can use the -u or --gear up-upstream-to option to git branch to explicitly set it at any time.

              $ git branch -u origin/serverfix Branch serverfix prepare to track remote branch serverfix from origin.            

Note

Upstream shorthand

When you have an tracking branch prepare, yous can reference it with the @{upstream} or @{u} autograph. And so if you lot're on the master branch and information technology's tracking origin/primary, you tin can say something like git merge @{u} instead of git merge origin/chief if you lot wish.

If you want to see what tracking branches you have ready upwardly, you can use the -vv option to git branch. This will list out your local branches with more information including what each branch is tracking and if your local branch is ahead, behind or both.

              $ git branch -vv   iss53     7e424c3 [origin/iss53: alee 2] forgot the brackets   chief    1ae2a45 [origin/master] deploying alphabetize fix * serverfix f8674d9 [teamone/server-fix-good: alee iii, behind 1] this should do information technology   testing   5ea463a trying something new            

And so here we tin can come across that our iss53 branch is tracking origin/iss53 and is "ahead" by 2, significant that we have two commits locally that are non pushed to the server. We can also see that our principal branch is tracking origin/main and is up to date. Next we can see that our serverfix branch is tracking the server-set up-good branch on our teamone server and is ahead past three and behind by one, meaning that in that location is ane commit on the server nosotros oasis't merged in yet and three commits locally that nosotros 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 just since the last time y'all fetched from each server. This command does not reach out to the servers, it's telling you lot about what information technology has cached from these servers locally. If yous want totally upwards to date ahead and behind numbers, you'll need to fetch from all your remotes right before running this. You could practice that like this: $ git fetch --all; git branch -vv

Pulling

While the git fetch command will fetch down all the changes on the server that you don't have yet, information technology volition 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 chosen git pull which is substantially a git fetch immediately followed by a git merge in most cases. If you lot have a tracking branch set up upward every bit 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 await up what server and branch your current co-operative is tracking, fetch from that server and then try to merge in that remote co-operative.

Generally it's better to simply utilise the fetch and merge commands explicitly as the magic of git pull tin frequently be confusing.

Deleting Remote Branches

Suppose you're done with a remote branch – say you and your collaborators are finished with a characteristic and take merged information technology into your remote's principal branch (or whatever branch your stable codeline is in). Y'all tin can delete a remote co-operative using the --delete choice to git push button. If you desire to delete your serverfix branch from the server, you run the following:

              $ git push button origin --delete serverfix To https://github.com/schacon/simplegit  - [deleted]         serverfix            

Basically all this does is remove the pointer from the server. The Git server will more often than not proceed the information at that place for a while until a garbage drove runs, and so if it was accidentally deleted, it's oftentimes piece of cake to recover.

huberoverve.blogspot.com

Source: https://git-scm.com/book/id/v2/Git-Branching-Remote-Branches

0 Response to "Git Checkout Branch Again From Remote"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel