Saturday, June 18, 2016

Unleashing the Git - part 3 - Working with remote repositories

More often we need to share the files that we modify and make it available for other users. This is typically the case in Software development projects, where several people work on a set of files and all needs to modify and do changes. The solution is to have the repository in a centralised place and each and every one work with there own copies before they commit it to the central place. There are many online git repository providers. Examples are GitHub, BitBucket etc.

Choose, whatever best suites for you and sign up for an account. Most online repositories provide free accounts. I've a created a one in bitbucket and have created a repository named online_workbench.
$ git remote add origin https://shazni@bitbucket.org/shazni/online_workbench.git  // Syntax is git remote add <name> <repository URL>
$ git remote rm <name> // To remove a remote from your local repository
$ git push -u origin master
Password for 'https://shazni@bitbucket.org':
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 88.01 KiB, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://shazni@bitbucket.org/shazni/online_workbench.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

After you entered your password, you'll have your repository online and its local location is your local $HOME/example directory.

'git push -u origin master' is to make the git aware, that all the pull and push operations to default to the master branch. If in case '-u origin master' is not specified, each and every time you issue a 'pull' or 'push' command, you will have to specify the origin.

Ok.. Now if someone needs to get a local copy and work on this remote repository, what he/she needs to do? Only few steps are involved.

First we need to clone the repository into a directory. Navigate or create a new directory where you need the local clone of the repository and issue the following command.
$ git clone <remote repository location> <Path you want the clone to be>
If you omit the <Path you want the clone to be>, it will be in the current directory.

Depending on how the firewall on your computer or local area network (LAN) is configured, you might get an error trying to clone a remote repository over the network. Git uses SSH by default to transfer changes
over the network, but it also uses the Git protocol (signified by having git:// at the beginning of the URI) on port 9418. Check with your local network administrator to make sure communication on ports 22—the port SSH communicates on—and 9418 are open if you have trouble communicating with a remote repository.
$ git clone --depth 50 <remote repository location>  // Create a shallow clone with the last fifty commits

Then, if we do the modification to the files or added files, next we need to stage it for commits. Issue the following commands.
$ git add *            // Stages and add all your changes, This is similar to git add -A 
// If you specify git add -u will only previously added and updated files will be staged. -p parameter will present you with sections so that you are given option to include a change or not.
$ git commit –m 'My commit comment'     // Commit the changes to the local repository
$ git pull        // checks whether there are any unsinced updates in the remote location and if exist syncs local and the server
$ git push        // Add the changes made by you to the server

No comments:

Post a Comment