Friday, 30 January 2015

Working with Remotes in Git

First of all must have all the dependencies in your system

1 .sign up at Remote like github.
2. generate ssh key  in your system ,

 SSH: 
 In your home directory that is /Users/Jitendra , there is a directory .ssh which contains two         files named: 
 id_rsa(private key) and 
 id_rsa.pub(public key)

and link this with your remote host e.g. github

3. Now create a blank repository  on github. and clone it in your working directory.

$ git clone git@github.com:Jitu1990/gittest.git

At this point  your remote is set with origin , to see it run:

$ git remote -v
origin: git@github.com:Jitu1990/gittest.git

Working with remotes

Showing your remotes

To see which remote servers you have configured, you can run git remote the command. It lists the shortnames of each remote handle you’ve specified. If you’ve cloned your repository, you should at least see —that is the default name Git gives to the server you cloned from: 

$ git remote
origin
$ git remote -v
origin git@github.com:Jitu1990/gittest.git

Adding New Remote
Note: you can add a new remote Git repository as a short- name you can reference easily, run git remote add [shortname] [url]

$ git remote add pb  https://github.com/Jitu1990/gittest.git

Now track your files and commit changes. If you want to push the changes back to remote host. you can do it with these command .

Inspecting a remote

$git remote show origin


This command shows which branch is automatically pushed when you run
on certain branches. It also shows you which remote branches on the server you don’t yet have, which remote branches you have that have been removed from the server, and multiple branches that are automatically merged when you run


Removing and Renaming a remote
$ git remote rename gitssh gitsshtr

$ git remote remove gitsshtr


Push your commit:
You can push either using https or ssh.
$ git push [remote-name] [branch -name]

eg. $git push origin master


This command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime. If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. You’ll have to pull down their work first and incorporate it into yours before you’ll be allowed to push.  

Note: if you push through https it will ask you remote(github) user name and password:
eg.
   $ git push pb master
Username for 'https://github.com': Username
Password for 'https://jsolanki272@gmail.com@github.com':

That means you can have multiple remote 
$ git remote -v
badkoor git://github.com/bakdoor/tickgit.git
cho45   git://github.com/cho45/grit.git
pb      https://github.com/Jitu1990/gittest.git
origin  git@github.com:Jitu1990/gittest.git

This means I can pull contributions from any of these users pretty easily. But notice that only the remote is an SSH URL, so it’s the only one I can push to  .

Pull Repository:
To pull a repository, run:
$git pull


How pull is different from fetch ?

To get data from your remote projects, you can run

$ 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 origin 

fetches any new work that has been pushed to that server since you cloned (or last fetched from) it. It’s important to note that the command pulls the data to your local repository—it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready. 


you have a branch set up to track a remote branch you can use the  git pull command to automatically fetch and then merge a remote branch into your current branch. This may be an easier or more comfortable workflow for you; and by default, the command automatically sets up your local master branch to track the remote master branch on the server you cloned from (assuming the remote has the master branch)


Running generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you’re currently work- ing on. 






No comments:

Post a Comment