Site Navigation

Post navigation

Getting Good with Git: Remotes

Posted: December 14, 2012 Updated: July 4, 2014

Now that we’ve explored the fundamentals of Git, let’s continue getting our Git on and learn a thing our two about remotes. Throughout this post I use the term Remote in descriptions as shorthand for Remote Repository. Remotes are what you will use to collaborate with others.

Viewing, Creating & Removing Remotes

git remote

Similar to git branch except for remotes, this command shows you a listing of all the remote repositories that you have setup your local repository.

git remote add [name_of_remote] [url_where_remote_is_located]

Add a remote repository and give it the alias [name_of_remote]. Aliases are what you will later refer to when running git push. For this example it would be git push [name_of_remote]. The URL is something that will be provided by the remote server after you create the remote repo ~ ie: https://github.com/johndugan/my_repo_name.git

git remote rm [name_of_remote]

Remove the remote server named [name_of_remote].

Note, you can certainly have more than one remote server associated with your local git repo. I personally use both Beanstalk and GitHub. As a side, Beanstalk makes deployment an absolute breeze. It’s well worth the money.

Viewing, Creating & Removing Remote Branches

git branch -r

Displays a list of the remote branches for the repository that exists on our remote server.

git branch -a

Displays a list of all branches both local and remote. Note: to view only your local branches, simply run git branch.

git remote -v

Show remote servers and display which remote is for fetching and which is for pushing. Note: these are typically the same remote server. However, in some cases you will have read only remote for fetching and separate remote where you commit to.

git branch [name_of_branch] [name_of_remote]/[name_of_remote_branch]

Create a new branch [name_of_branch] based off of the remote branch [name_of_remote_branch] that exists on the remote server [name_of_remote]

git push origin --delete [name_of_branch]

Remove the branch named [name_of_branch] from the remote repository. Note: this will not remove [name_of_branch] from our local repository.

Remote Configuration, Diffs & Tracking Branches

cat .git/config

View the Git configuration for this repository. Note: you can view the remote server that you just setup in this file.

git diff [name_of_branch]..[name_of_remote]/[name_of_remote_branch]

View the differences between the [name_of_branch] branch in your local repository with the [name_of_remote_branch] branch that exists on your remote server. Note: in order to make sure you are comparing [name_of_branch] to the most up-to-date version of [name_of_remote_branch], make sure to run git fetch [name_of_remote] because you are actually comparing the [name_of_branch] branch to the [name_of_remote_branch] that exists on your local machine.

git push -u [name_of_remote] [name_of_branch]

Push code from the current local branch to the remote branch [name_of_branch] on the remote server [name_of_remote] and Set [name_of_branch] on [name_of_remote] to be the remote tracking branch. Note: if you cloned the repository, the remote from which it was cloned is automatically setup as the remote tracking branch.