COMP 1531 Tutorial 02, Week 02

Opening an account

People generally resort to either Bitbucket or Github in order to integrate version control and code hosting in their projects.

Github

Github provides unlimited free public repository with the basic plan, but if you plan on hosting your assignments, that is probably not a very good idea, because it is too easy for people to take a look at your assignment and copy it down. But if you are a student, you are in luck because Github provides unlimited free public and private repositories (until you graduate) if you sign up with a student account. Jump on to https://education.github.com/pack/ to open an account with your student email and verify.

Once you are done, let’s set up git on our machine.

Setting up git in your local machine

Linux

Git generally comes pre-installed with Linux. You can find out by typing

git status

If you get a ‘fatal: Not a git repository (or any of the parent directories): .git’, you have git.

If you get something like ‘bash: git: command not recognized’ then go to

https://git-scm.com/download/linux

And follow the relevant instructions to set up git.

Mac

Install homebrew first, and then type

brew install git

Authenticating

Open the terminal, and type in (with the quotes)

git config --global user.name "Your Name"
git config --global user.email "email@example.com"
cd ~/.ssh

If you do not have an SSH key, “No such file or directory” will pop up. If so, type in

ssh-keygen -t rsa -C "your_email@example.com" and accept the defaults.

Next, type in

cat ~/.ssh/id_rsa.pub

You will see a very large hash key. That is your SSH key and one way git can verify that it is indeed you.

Go to your git account > Settings > SSH and GPG Keys > New SSH Key

Paste that ssh key and give it a title that tells you which machine it is, for example “CSE Machine”, “personal-mac”, etc.

Start using git

Git knows what you did last summer.

Git, as you may have heard, is a version control system.

In the company that you work for, the software engineers (and that includes yourself) have worked hard to get a minimum viable product running, and it was a huge success. After tasting success in version 1.0 of your product, you now want to add more features to it when version 2.0 comes out. And thus you delve into your mugs of coffee and work even harder to add the features. Version 2.0 is now ready and you release it.

As it turns out, version 2.0 has some serious bugs in it that somehow passed your tests and is now running in production. You guys now need to roll back to version 1.0, which is a stable version and is yet to produce any serious bugs.

Such a scenario is quite common in any place where software is used, and thanks to git, we can roll back to a stable version of our choice and keep working on fixing the bugs before releasing it.

Initialize repository

Create a new repository in your remote git account (the online github/bitbucket account) . For now, let's go to github.com and login. Then from your dashboard, locate the button "New Repository". Fill in the name and description fields and create the repository as private. Then, initialize a git repository in your local machine.

In order to initialize an empty git repository in your local machine, cd to the root of your project and do

cd project_dir
git init

You should see a message saying

Initialized empty Git repository in /home/path/to/project/.git/
Set remote repository to track local repository

Now, we need to tell our local repo where it should push its code to.

cd to your project root.

In your remote project, you will see a button “Clone or download”. Click on it, click on “Use SSH”.

Copy that url. It will typically look like this:

git@github.com:Your_id/Repository_name.git

git remote add origin remote_repository_url
git remote -v (to verify)

Let's add a ReadMe file so we have something to push. Open a ReadMe file and write "Starter ReadMe File"

git add .
git commit -m "Added sample Readme file"
git push -u origin master

And you have made your first push. Now let us add some code to it so that we can see the actual functionalities of git.

Cloning

Cloning an existing repository is fairly straightforward. Go to the website, and find the green ‘Clone or Download’ button. Click on it and copy the clone url, which looks like https://github.com/username/repository.git

After that, open up a terminal and type

git clone <url address>

The repository will be downloaded into your local machine.

Committing and Adding

Let’s add a very simple minimal Hello World application. For this, create a file named “hello_word.py” and inside, write

print("Hello, World!")

Save the file and do the following

git add . (Stage the directory)

git commit -m “Added a minimal application”(Add a commit message)
git push (Push to remote)

If you look into your remote git account, you should be able to see the new file added

Branching out

If you are to collaborate on a project, or you want to add a new feature but don’t want to mess up the old features, you would want to create a new branch and make your changes.

To create a branch, type in

git checkout -b new-branch-name

Note that the -b flag is needed only when creating a branch. After it is created, you can just switch into it by typing in

git checkout new-branch-name

Now, make some changes to your file. For instance, add a comment like “A change exclusive to this branch only”.

git add .
git commit -m “Added a comment in this branch”
git push

Notice that it will show that everything is up to date, even though you had made changes. That is because your new branch exists only locally, and not remotely. Not yet anyway. So by default git tried to push local master to remote master. In order to tackle that, do

git push --set-upstream origin new-branch-name

or

git push -u origin new_branch_name

(Note that this is only needed when you are pushing for the first time from a branch. After that, git push will suffice).

To notice the differences, type in

git diff master

At any point, to check which branch you are on, type in

git branch

To check changes that you are yet to commit, type in

git status

Merge conflict

A git pull a day keeps the conflicts away.

Merge conflicts happen when two people try to push together (not necessarily at the same time, but after the same pull) and they have different code in certain parts of the code, then git doesn’t know which change to accept, so it throws merge conflicts. The larger the project is, the harder it is to fix. Let us look at an example.

By default, all git repositories have the branch ‘master’. Let us make another branch called ‘dev’.

Before making the branch dev, open a hello-world.py file and put in

print(“This is a git branch”)

Add, commit and push.

Then run

git checkout -b dev
Now, while in dev, underneath line 1, add the following line

“This is branch dev”

Add, commit and push.

Go back to master and just as before, add the line

“This is branch master”

Add, commit and push.

Now, type in the following commands

git fetch --all
git merge master (while in dev)

In order to fix your merge conflicts, go to your file and locate the ‘<<<<<<<’ and ‘>>>>>>>’ and pick the one that you think is appropriate.

There are several other ways to get merge conflicts.

In order to avoid merge conflicts, make sure you don’t leave uncommited changes at the end of the day and most importantly, at the start of the day, do a git pull.

Exam style questions

I am involved in the implementation of a mission-critical application, where the requirements are very clear and the customer has assured that the requirements are stable. They would like the project to be implemented in a reasonable amount of time, and they have adequate resources available? Discuss, which software methodology might be suitable for this project and why?

As a consultant I am involved in a project, where the enterprise CEO has said to me:

We have a moderate understanding of our requirements, yet we would like to see what the product looks like after some of the features of the product have been implemented. We don’t need detailed documentation, yet we would like to see visual model of the business process and your proposed architecture. Select a suitable development methodology for this project. For your chosen methodology, characterise the key features and phases.