Friday, May 23, 2014

Learning git remote

Hello everybody! Today, we will take a look into git remote. So why git remote? Ever wonder why everytime when you push, you only have one command to push to? What if you want to push to a few servers? But before we push into a few servers, let's take a look what is git remote actually?

From git remote documentation

git-remote - Manage set of tracked repositories

So let's explain using examples. Below is my git repository, we check what we have in our current project.
$ git remote -v
origin https://github.com/jasonwee/videoOnCloud.git (fetch)
origin https://github.com/jasonwee/videoOnCloud.git (push)

Okay, so we have a remote repository named origin and its url for fetch and push, all clear, we are tracing the remote repository on github. As you may notice, origin didn't explain much though, other than said, oh yea, this is where it begin. What if you want to use a more descriptive term?
$ git remote rename origin github
$ git remote -v
github https://github.com/jasonwee/videoOnCloud.git (fetch)
github https://github.com/jasonwee/videoOnCloud.git (push)

okay, so now it is very descriptive, our remote repository is github. Now, what if I would like to push to another remote server? what then? Can I do it?
$ git remote add production https://production.com/jasonwee/videoOnCloud.git
$ git remote -v
github https://github.com/jasonwee/videoOnCloud.git (fetch)
github https://github.com/jasonwee/videoOnCloud.git (push)
production https://production.com/jasonwee/videoOnCloud.git (fetch)
production https://production.com/jasonwee/videoOnCloud.git (push)

That's looks pretty easy. But over time, you may forget where is the branches set to.
$ git branch -r
github/master

So it is currently pointing to github/master. If you want to remove local branches which in  remote branches has been removed, you can use git remote prune. Note that, following --dry-run won't actually remove but just show you which is going to be removed. If you are sure, just remove the parameter --dry-run.
$ git remote prune --dry-run github
$

To fetch updates for a named set of remote in the repository, execute using remote update
$ git remote -v update github
Fetching github
From https://github.com/jasonwee/videoOnCloud
= [up to date] master -> github/master

To change a push url for a remote repository, and without --push, fetch url is changed.
$ git remote set-url --push production https://production1.com/jasonwee/videoOnCloud.git
$ git remote -v
github https://github.com/jasonwee/videoOnCloud.git (fetch)
github https://github.com/jasonwee/videoOnCloud.git (push)
production https://production.com/jasonwee/videoOnCloud.git (fetch)
production https://production1.com/jasonwee/videoOnCloud.git (push)

$ git remote set-url production https://production2.com/jasonwee/videoOnCloud.git
$ git remote -v
github https://github.com/jasonwee/videoOnCloud.git (fetch)
github https://github.com/jasonwee/videoOnCloud.git (push)
production https://production2.com/jasonwee/videoOnCloud.git (fetch)
production https://production1.com/jasonwee/videoOnCloud.git (push)

Though you can add more url for remote repositories, you can use set-url --add. If you notice, it won't be showing but you can check in .git/config to look at the url.
$ git remote set-url --add production https://production3.com/jasonwee/videoOnCloud.git
$ git remote -v
github https://github.com/jasonwee/videoOnCloud.git (fetch)
github https://github.com/jasonwee/videoOnCloud.git (push)
production https://production2.com/jasonwee/videoOnCloud.git (fetch)
production https://production1.com/jasonwee/videoOnCloud.git (push)
$ git remote set-url --delete production https://production3.com/jasonwee/videoOnCloud.git
$ git remote -v
github https://github.com/jasonwee/videoOnCloud.git (fetch)
github https://github.com/jasonwee/videoOnCloud.git (push)
production https://production2.com/jasonwee/videoOnCloud.git (fetch)
production https://production1.com/jasonwee/videoOnCloud.git (push)

Okay, because I don't host any remote production server, I will remove it.
$ git remote remove production
$ git remote -v
github https://github.com/jasonwee/videoOnCloud.git (fetch)
github https://github.com/jasonwee/videoOnCloud.git (push)

 

No comments:

Post a Comment