Git Fetching, Pulling from Remote repository and Pushing to Remote repository

Hi, I am Malathi Boggavarapu and welcome to my blog.

This tutorial describes how to fetch from remote repository, pulling from and pushing to remote repository. I have a seperate post "Git Fundamentals" in my blog which explains everything about Git but i had broken them down to smaller sections to make it easy for everyone to follow. This post will only introduce you to Git.

So let's get started.

Fetching from a Remote

The remote repository is automatically added when we clone a repository for example from GITHUB but if i have local repo and i want to add a remote destination to it, i can use the below command to do it.

git remote add origin https://github.com/....

origin can be anything, i had chosen some arbitrary name here. So if someone sends a pull request, you can add their public repository and then pull their changes into your working local copy to examine. So you can have multiple remotes and it is done commonly in git inorder to evaluate patches or pull requests that have been made to your project. Now that remote has been added. I can do the following command

git fetch - It will pull down any changes from that remote repo. We can run as many times as we want. If we have multiple remotes, i can specify from which remote to fetch from.
Example :  git fetch origin

git log origin/master - origin/master is the name of the remote branch. It shows all the changes that were committed into the repository.

If we have some new commits in the repo and does not exist in your local working copy, then do a merge operation. See below.

git merge origin/master

We will discuss more about merge later.

Pulling from a Remote


If i do git branch -r, i can see that remote branch origin/master that i just merged from (as said above)

git fetch; git merge origin/master - We can specify multiple commands at the same time. This command will fetch from remote repo and do merge the changes to local repo.

git branch --set-upstream master origin/master - Setting up upstream branch is introduced in Git 1,7. Upstream tracking branch is basically what branch remotely does my local branch nearer. master is my master branch (local one) and origin/master is the branch of remote repo. Once i set this remote tracking branch i can do the following command to pull out the changes from the remote repo to local repo.

git pull - It is the shortest form of command git fetch; git merge origin/master. It pulls any changes down to local repo. If i do not want to use upstream branch as said above, i can simply use git pull origin master where origin master is the remote branch.

Pushing to a Remote



If we look at first command git commit -am "Sharing is easy", we are committing changes of a file to local repository and we want to push the changes to the remote repository. Now when we do git push, it ask for username and password to access github. Instead of doing this way, we are going to follow a slightly different approach here.

We do remove remote origin using git remote rm origin and add it using the ssh version of the url. The advantage of the ssh version is that it is going to use my ssh key to authenticate with github. Now if i do git push, it is not going to prompt for the password but it just push the changes to the github.

So if you are pushing the changes back up to the github repository, it is easier to use ssh url rather than http url. http require username and password whereas the ssh version can use your ssh key to do the authentication for you.

So we came to the end of the session. Hope it is helpful. Please post your comments and let me know your questions and valuable inputs.

Follow my other posts on Git which were mentioned below

Brief History of Version Control Systems, DVCS Advantages and Git Installation
Configuring Git in your computer
Working locally with Git
How to Git clone your remote repository
Git - Creating, Verifying tags and Pushing to Remote repository
Branching, Merging and Rebasing with Git
Git - Branching, Merging, Rebasing and Cherry-picking

Happy Learning!!!


Comments

Popular posts from this blog

Bash - Execute Pl/Sql script from Shell script

How to get client Ip Address using Java HttpServletRequest

How to install Portable JDK in Windows without Admin rights