Skip to content

Instantly share code, notes, and snippets.

@dariodaich
Last active August 19, 2016 23:34
Show Gist options
  • Save dariodaich/ad902e01d57320073b68d3f1e077ffed to your computer and use it in GitHub Desktop.
Save dariodaich/ad902e01d57320073b68d3f1e077ffed to your computer and use it in GitHub Desktop.

Git Branching - Remote Branches

Commands

  • git ls-remote: displays references available in a remote repository along with the associated commit IDs.
  • git remote show {remote name}: Manage the set of repositories ("remotes") whose branches you track.
  • git branch | git branch -v | git branch -vv
  • git fetch {remote name}
  • git pull: Fetch from and integrate with another repository or a local branch.
  • git checkout -b {branch} {remote name}/{remote branch name}: checks out a new local branch and sets it up to track remote branch
  • git checkout --track {remote name}/{remote branch name}
  • git checkout {branch name}
  • git fetch: Download objects and refs from another repository.

Definitions

Remote references:

  • Remote references are references (pointers) in your remote repositories, including branches, tags, and so on.

Remote-tracking branches:

  • Remote-tracking branches are references to the state of remote branches. They’re local references that you can’t move.
  • naming: [remote name]/[remote branch name]

Tracking branches:

  • Tracking branches are local branches that have a direct relationship to a remote branch.

Upstream branches:

  • The branch tracking branch tracks is called an “upstream branch”.

What happens when...

  • 'git clone [remote name]' is run? Git’s clone command

    Git’s clone command automatically names it origin for you, pulls down all its data, creates a pointer to where its master branch is, and names it origin/master locally.

  • does the pointer of my remote tracking branches move?

  • when you run 'git fetch [remote name]'
  • I run 'git pull'?

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

  • I run 'git fetch'?

    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.

  • I want to set a tracking branch and I already have a local branch?

    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 remote-name/remote-branch-name

  • I want to delete remote branch?

  • You run 'git push origin --delete {remote branch name}'

2.5. Git Basics - Working with Remotes

Working with Remotes

What are remote repositories?

Remote repositories are versions of your project that are hosted on the Internet or network somewhere.

Showing Your Remotes

How to view which remote servers I have configured?

To see which remote servers you have configured, you can run the git remote command. It lists the shortnames of each remote handle you’ve specified.

When I clone a repo from a server, how is it named locally?

If you’ve cloned your repository, you should at least see origin – that is the default name Git gives to the server you cloned from

How can I view remote repos URLs?

You can also specify -v, which shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote. If you have more than one remote, the command lists them all.

Adding Remote Repositories

How do I add a remote repo?

To add a new remote Git repository as a shortname you can reference easily, run git remote add

How to get the data from a repo I do not yet have locally?

git fetch [remote-name] The command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet. After you do this, you should have references to all the branches from that remote, which you can merge in or inspect at any time.

git fetch will fetch all data pushed to the repository, not only one branch.

How do I write to a remote repo?

When you have your project at a point that you want to share, you have to push it upstream. The command for this is simple: git push [remote-name] [branch-name].

Removing and Renaming Remotes

How do I rename remote repo?

You can run git remote rename to change a remote’s shortname

How do I remove remote repo?

git remote rm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment