Tuesday, 10 September 2013

Get started with Git: Cloning

Git is a software version control system.  It is good because you can use it in a distributed way keeping local and remote repositories kept in sync, with multiple developers strong project history and conflict resolution.

This post describes how to 'git going' with an existing project assuming you have already Set Up Git.  We create a local version of a project on github, make a small change and bounce that back to the github master.


You will make a copy of the remote project using Git to create a local repository.  Then you will make and commit a small change to the code and sync it with the remote project.

Step 1: Clone the git repository

Find the url of the remote repository.  If hosted on github.com it will be something like https://github.com/<username>/<projectname>.git 

In terminal navigate to the folder you want to keep your local repository in and type:
$ git clone https://github.com/<username>/<projectname>.git

This will create a local copy of the project 
Cloning into 'ProjectName'...
remote: Counting objects: 147, done.
remote: Compressing objects: 100% (96/96), done.
remote: Total 147 (delta 54), reused 132 (delta 45)
Receiving objects: 100% (147/147), 1.05 MiB | 496 KiB/s, done.
Resolving deltas: 100% (54/54), done.
$ git status
# On branch master
nothing to commit (working directory clean) 

Step 2:  Change local repository

Make a change to the code, here I just added something minor to README.md.  This type of file is a 'markdown' file that github displays in a specific way as documentation for the project.

Git tracks these changes as you make them locally and you can view the current state with the command git status:
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
# modified:   README.md
no changes added to commit (use "git add" and/or "git commit -a")

If you add a new file to the directory that you want to track it Git must be made aware of it using
git add <filename>

To formally add your changes to your local repository you must commit them as follows:
$ git commit -a -m "change README.md in a very minor way to test MM's git"
[master 718e4f0] Change README.md in a very minor way
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
nothing to commit (working directory clean)

Step 3: Commit changes to remote

When you cloned the remote repository, git also set up aliases for it.  You can see these as follows
$ git remote -v
origin https://github.com/<username>/<repo>.git (fetch)
origin https://github.com/<username>/<repo>.git (push)

This aliases are used for fetching the latest version of the remote files (fetch) and for making your local changes to the remote (push).  The syntax is git push|fetch <alias> <branch> where in this case we're going to push our locally committed change to origin (the github repository I cloned) and the master branch (use the command git branch to see that's the only one at the moment).

$ git push origin master
Username for 'https://github.com': <yourusername>
Password for 'https://<yourusername>@github.com': <your password> 
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 313 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
To https://github.com/<username>/<repo>.git
   1f206be..718e4f0  master -> master

The cool thing about the aliases is that you can set up a git project on a number of remote sites and push your updates to each one at different times.  This is great if you have a test website, a staging website and a live website because you can set up an alias for each and push your code to different targets as required by using git push <target alias> <branch>.  There are also lots of cool things you can do with Git branches.