Privacy Policy
Snippets index

  Git remotes examples

Remotes listing

To list remote references, and to displays plenty of information about the remote in general and how it relates to your own repository

$ git remote -v show (remote)

for example

$ git remote -v show origin

Remote-tracking branches

Remote-tracking branches are references to the state of remote branches.

They’re local references that you can’t move; they’re moved automatically for you whenever you do any network communication.

Remote-tracking branches act as bookmarks to remind you where the branches in your remote repositories were the last time you connected to them.

Just like the branch name master does not have any special meaning in Git, neither does origin. While "master" is the default name for a starting branch when you run git init which is the only reason it’s widely used, "origin" is the default name for a remote when you run git clone.

If you run:

$ git clone -o booyah

instead, then you will have booyah/master as your default 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.

You can add another Git server (for example git.team1.ourcompany.com) as a new remote reference to the project you’re currently working on by running the $ git remote add command. Here we name this remote teamone, which will be your shortname for that whole URL:

$ git remote add teamone git://git.team1.ourcompany.com

Now, you can run:

$ git fetch teamone

to fetch everything the remote teamone server has that you don’t have yet. If 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.

Pushing to remotes

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):

for example:

$ git push origin serverfix

This is a bit 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."

You can also do:

$ git push origin serverfix:serverfix

which does the same thing – it says:

"Take my serverfix and make it the remote’s serverfix."

You 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.

It’s important to note that when you do a fetch that brings down new remote-tracking branches:

$ git fetch origin

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.

To merge this work into your current working branch, you can run:

$ git merge origin/serverfix

If you want your own serverfix branch that you can work on, you can base it off your remote-tracking branch:

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

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

Tracking Branches

Tracking branches (or "upstream branches") are local branches that have a direct relationship to a remote branch.

Checking out a local branch from a remote-tracking branch automatically creates a "tracking branch".

If you’re on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into.

When you clone a repository, it generally automatically creates a master branch that tracks origin/master. However, you can set up other tracking branches if you wish – ones that track branches on other remotes, or don’t track the master branch.

The simple case is the example you just saw, running:

$ git checkout -b [branch] [remotename]/[branch]

This is a common enough operation that git provides the --track shorthand:

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

In fact, this is so common that there’s even a shortcut for that shortcut.

If the branch name you’re trying to checkout:

    1. doesn’t exist and
    1. exactly matches a name on only one remote,

Git will create a tracking branch for you:

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

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:

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

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

If you already have a local branch and want to set it to a remote branch you just pulled down, or want to change the upstream branch you’re tracking, you can use the -u or --set-upstream-to option to git branch to explicitly set it at any time.

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

If you want to see what tracking branches you have set up, you can use the -vv option to git branch:

$ git branch -vv
  iss53     7e424c3 [origin/iss53: ahead 2] forgot the brackets
  master    1ae2a45 [origin/master] deploying index fix
* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this should do it
  testing   5ea463a trying something new

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:

$ 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, it will not modify your working directory at all.

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, 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.

Deleting Remote Branches

You can delete a remote branch using the --delete option to git push. If you want to delete your serverfix branch from the server, you run the following:

$ git push 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 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.

Credits:

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