Starting with Git

Dr Andres Baravalle

Starting with Git

  • Introduction to Git
  • Creating a repository
  • Downloading a repository
  • Adding, committing and pushing files
  • Resolving collision

Introduction to Git

Etymology and meaning

Etymology: From Middle English get ("offspring", especially "illegitimate offspring"). A southern variant of Scots get ("illegitimate child, brat"), related to beget. (from Online Etymology Dictionary)

Meaning (Britain, slang, pejorative): 1) A contemptible person 2) A silly, incompetent, stupid, annoying or childish person.

What is Git?

Seriously speaking, Git is a powerful version control system, Developed by Linus Torvalds in 2005 and introduced to the Open Source community as "the information manager from hell" (Linus Torvalds).

As Linux, it's named after his creator:

“I’m an egotistical bastard, and I name all my projects after myself. First Linux, now git.”
PHP logo

Command line Git

We are going to use Git from the command line. Without any arguments, Git (git.exe) lists its options and the most common subcommands.

To manage our web applications, we are going to use the following subcommands:
add Add file contents to the index
branch List, create, or delete branches
checkout Checkout and switch to a branch
clone Clone a repository into a new directory
commit Record changes to the repository

 

Command line Git (2)

diff Show changes between commits, the commit and working trees, etc
log Show commit logs
init Create an empty git repository or reinitialize an existing one
merge Join two or more development histories
mv Move or rename a file, a directory, or a symlink
pull Fetch from and merge with another repository or a local branch
push Update remote refs along with associated objects
rm Remove files from the working tree and from the index
show Show various types of objects
status Show the working tree status

You do not need to understand these commands now, but we will be using them shortly for our lab activities.

Before we start

As you may immagine, there is more than one way to use Git. The following slides are an example of one way in which you can use Git and assume that you are using the git command from the shell.

On-line help

Documentation for each git subcommand is available using any of the following commands:

git help subcommand
git --help subcommand
git subcommand --help 

Downloading projects

git clone git://github.com/Project/project.git

Activity 1

We will download the phpDocumentor package (we will also use the package later on this semester).

  1. All the steps for the lecture run within the web server shell (command line)
  2. Briefly read the section "How to use phpDocumentor? " in the documentation and run
    phpdoc run -h 
                           

    (just to test that the package has been downloaded correctly)

Creating an initial repository

To create a Git repository, you can run git init in the root folder of your project:

git init

This creates a .git subfolder with the information Git needs to operate.

Activity 2: joining the repository

For the aims of this course, each team has an allocated space on GitHub.

Register on GitHub here: https://education.github.com/pack, using your institutional email address and then ask your lecturer (in the lab) to add your group members to your group's github private repository.

Once you have been added to the repository, your repository name will be the same as your group numer. E.g. if you are "Group1" your repository URL is https://github.com/uel-webapps/group1.git

You do not need to create an initial repository, but read the getting started with GitHub guide and explore the web interface for GitHub by opening your repository pages.

Setting up and using your key pair

Authentication in git is done using a public/private key combination.

You can use your own key pair or create a new one:

ssh-keygen -t rsa -b 4096 -C "a.baravalle@uel.ac.uk"

After creating the keypair, you will use ssh-agent to keep the key pair in memory. This is needed for authentication.

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

You will also need to upload your keypair on GitHub, here: https://github.com/settings/ssh

Activity 3: creating and uploading your key pair

Create your keypair and run ssh-agent to load the keypair.

Follow the step-by-step instructions here and upload the public key to GitHub.

Configuring the author

Beside setting your public/private key, before you start using Git you need to set up your username, and your email. This will be the default value used for all your commits.

git config --global user.name "Andres Baravalle"
git config --global user.email "a.baravalle@uel.ac.uk"

This will save the info in $HOME/.gitconfig - you need to run this operation only once.

Activity 4: getting started

Set up the username, email (on the Linux shell) and download your initial (empty) repository from GitHub.

git clone git@github.com:uel-webapps/group1.git 

(replace the address as needed)

Adding content to the stage

All repositories are empty by default. You must explicitly add content to your repository - using git add and git commit.

When you add content to the repository, it will be tracked.

echo "<html><head><title>Hello world</title></head><body><p>Hello world</p></body></html>" >hello.php
git add hello.php
echo "<html><head><title>Hello world</title></head><body><p>Hello world</p></body></html>" >hello2.php
git add hello2.php

git add puts the file into the stage; Git now knows that the file will have to be uploaded into the repository during the next commit.

Committing content

git commit -m "Initial commit"

You can see the log of your commits using:

git log

To see more details about a particular commit, use git show with a commit id (or just git show for the last one).

Pushing content

After adding (git add) and committing (git commit) operations, the code has not been yet uploaded to the repository.

You need to do that with:

git push            

Git add, commit and push

  1. git add adds your modified files to the queue to be committed later. Files are not committed
  2. git commit commits the files that have been added and creates a new revision with a log. If you do not add any files, git will not commit anything. You can combine both actions with git commit -a
  3. git push pushes your changes to the remote repository.

(from http://stackoverflow.com/questions/6143285/git-add-vs-push-vs-commit)

Activity 5: add, commit and push

Create a hello word PHP file, add it to the repository, commit and push.

Collision?

If you are working as part of a team, another team member could have changed one or more files in the project that you also have changed.

This is normally signalled by this type of error:

error: failed to push some refs to 'git@github.com:uel-webapps/andres.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.
              

When this happens, pull the repository (or branch of the repository):

git pull

Your file(s) will be updated and will include both versions; fix and push, using --force.

Viewing differences

To see the differences between the two revisions of index.html, recall both full commit ID names (use git history to get the ids) and run git diff:

git diff 9da581d910c9c4ac93557ca4859e767f5caf5169 ec232cddfb94e0dfd5b5855af8ded7f5eb5c90d6

More commands

To remove (delete) files from the repository, use git rm:

git rm hello.php
git commit -m "Removed the first Hello World file"

Brances and tags

Git includes the concept of branch (a separate line of development within a project) and tag (a static pointer to a specific commit).

For example, if you want to identify a specific release of your project, you use a tag (v3.2.1). If you want to identify a development vs a stable line of development, you will use a branch.

Using git tags

To tag a git commit, use the command git tag just after the commit:

git tag 0.1

Will tag the current commit as 0.1.

To do this ex post, identify the commit that you want to tag, using:

git log --pretty=oneline

and then run:

git tag -a v1.2 <commit id> 

(replace <commit it> with the id of the commit)

Completely lost?

Try using TortoiseGit; the interface is fairly user friendly.

You will recognise all the commands from the slides.

This work

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License

Creative Commons License