Linuxin keskitetty hallinta – Homework 4 – GIT

Homework topic: GIT

Course homepage: http://terokarvinen.com/2012/aikataulu-linuxin-keskitetty-hallinta-ict4tn011-3

Main source for this exercise: http://terokarvinen.com/2012/git-from-offline-to-network

Assignment

-Make a local git repository
-Make multiple nonsensical commits and return your repository to an earlier point with git reset
-Copy your local repository to a server
-Clone your repository to another system and test that changes are synchronized successfully
-Try a git frontend (e.g. giggle)

Enviroment

For this exercise, I’ll be using three virtual machines:

“desktop”, running Debian 6.0.7. (192.168.1.107)

“debianserver”, running Debian 6.0.7 (192.168.1.105)

“palvelin”, running Xubuntu 12.10 (192.168.1.220)

debianserver and palvelin have git installed already.

The exercise

A local git repository

Can’t do much without installing git, so let’s start with that:

human@desktop:~$ sudo apt-get update
human@desktop:~$ sudo apt-get install git

Next, a directory for our project, aptly called “myproject”:

human@desktop:~$ mkdir myproject
human@desktop:~$ cd myproject

Now the first actual git command, git init, which will turn our directory into a repository:

human@desktop:~/myproject$ git init
Initialized empty Git repository in /home/human/myproject/.git/

Changes and commits

Let’s create some content. Namely, a file called “todo” whose contents are “expand this file’s contents”

human@desktop:~/myproject$ nano todo
human@desktop:~/myproject$ cat todo
expand this file's contents

Now that we’ve created a mons… a file, we can add it to the repository. For this, we first apply the contents of the directory and then do a commit:

human@desktop:~/myproject$ git add .
human@desktop:~/myproject$ git commit

Git wants a comment to go with the commit. On Debian (at least) it conveniently fires up the text editor nano, like so:

git-commit

Now that we’ve committed the file, let’s modify the todo file a couple of times, and commit the changes:

human@desktop:~/myproject$ nano todo
human@desktop:~/myproject$ cat todo
expand this file's contents
like so

Add and commit (comment “todo expanded”):

human@desktop:~/myproject$ git add .
human@desktop:~/myproject$ git commit

One more time for the people:

human@desktop:~/myproject$ nano todo
human@desktop:~/myproject$ cat todo
expand this file's contents
like so
wacky shenanigans

Add and commit (comment “todo expanded further”):

human@desktop:~/myproject$ git add .
human@desktop:~/myproject$ git commit

Reverting to a previous version

If we deem our changes useless, we can revert our repository to a previous version. First, let’s take a look at what has been done with git log, colour-coded for convenience:

human@desktop:~/myproject$ git log
commit 08c3e13b5c2096c3b8409caf0a9384cc370aad36
Author: human <human@desktop.vnet.local>
Date: Wed Apr 24 06:51:54 2013 -0400
todo expanded further
commit 2b89953ab4ef7552b73f31c01420c16107889e15
Author: human <human@desktop.vnet.local>
Date: Wed Apr 24 06:46:59 2013 -0400
todo expanded
commit 1379f6b29ae442668132f629ef107a5806c05093
Author: human <human@desktop.vnet.local>
Date: Wed Apr 24 06:38:12 2013 -0400
todo created

The earliest commit’s header is 1379f6b29ae442668132f629ef107a5806c05093. We use that with the git reset command to return to that version:

human@desktop:~/myproject$ git reset --hard 1379f6b29ae442668132f629ef107a5806c05093
HEAD is now at 1379f6b todo created

Copy the local repository to a server

Let’s begin by connecting to the other Debian VM with ssh:

human@desktop:~/myproject$ ssh human@192.168.1.105

Where are we?:

human@debianserver:~$ pwd
/home/human

Time to create a bare repository for future use:

human@debianserver:~$ mkdir myproject.git
human@debianserver:~$ cd myproject.git

…and initialize it:

human@debianserver:~/myproject.git$ git init --bare --shared
Initialized empty shared Git repository in /home/human/myproject.git/

We can now exit the ssh session and return to the “desktop” machine:

human@debianserver:~/myproject.git$ exit
logout
Connection to 192.168.1.105 closed.

Now, to sync the repository with the server. We will issue various configuration commands, which I’ll highlight with red text.

human@desktop:~/myproject$ git remote add origin ssh://human@192.168.1.105/home/human/myproject.git
human@desktop:~/myproject$ git push origin master
The authenticity of host '192.168.1.105 (192.168.1.105)' can't be established.
RSA key fingerprint is :::::::::::::::.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.105' (RSA) to the list of known hosts.
human@desktop:~/myproject$ git push origin master
human@192.168.1.105's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 235 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://human@192.168.1.105/home/human/myproject.git
 * [new branch] master -> master

human@desktop:~/myproject$ git branch --set-upstream master origin/master
Branch master set up to track remote branch master from origin.
human@desktop:~/myproject$ git pull
human@192.168.1.105's password:
Already up-to-date.

The repository now resides on the server.

Cloning the repository

This is where the Xubuntu VM steps in. First off, I’ll create a directory for the project. (We’ll later notice that this step wasn’t necessary.):

master@palvelin:~$ mkdir myproject
master@palvelin:~$ cd myproject/

Git clone is the aptly-named command which clones a remote repository:

master@palvelin:~/myproject$ git clone ssh://human@192.168.1.105/home/human/myproject.git
Cloning into 'myproject'...
The authenticity of host '192.168.1.105 (192.168.1.105)' can't be established.
RSA key fingerprint is 81:b7:30:90:2d:56:6c:05:a4:b8:e0:7c:52:48:5d:f2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.105' (RSA) to the list of known hosts.
human@192.168.1.105's password:
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
master@palvelin:~/myproject$ git pull && git push
fatal: Not a git repository (or any of the parent directories): .git
master@palvelin:~/myproject$ ls
myproject
master@palvelin:~/myproject$ cd myproject/
master@palvelin:~/myproject/myproject$ ls
todo
master@palvelin:~/myproject/myproject$ git pull && git push
human@192.168.1.105's password:
Already up-to-date.

The green hightlight shows why I shouldn’t have created the directory manually. The orange highlight shows how I came to this conclusion and made it work.

Testing if syncing between multiple computers is set up properly

We’re still on the Xubuntu machine. Let’s edit todo:

master@palvelin:~/myproject/myproject$ nano todo
master@palvelin:~/myproject/myproject$ cat todo
expand this file's contents
the newest of edits

…and do a commit (comment “final edit”):

master@palvelin:~/myproject/myproject$ git add .
master@palvelin:~/myproject/myproject$ git commit
[master f042432] final edit

This isn’t enough, we have to do a git push (pull is also part of the workflow) to sync the changes with the server:

master@palvelin:~/myproject/myproject$ git push && git pull
human@192.168.1.105's password:
Counting objects: 5, done.
Writing objects: 100% (3/3), 283 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://human@192.168.1.105/home/human/myproject.git
 1379f6b..f042432 master -> master
human@192.168.1.105's password:
Already up-to-date.

Moving on to the “desktop” machine to do a git pull:

human@desktop:~/myproject$ git pull
human@192.168.1.105's password:
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ssh://192.168.1.105/home/human/myproject
 1379f6b..f042432 master -> origin/master
Updating 1379f6b..f042432
Fast-forward
 todo | 1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

So, did it really work? We can do git log to find out:

human@desktop:~/myproject$ git log
commit f042432539e1a973a84c347bed27edf2a822d04c
Author: master <master@palvelin.(none)>
Date: Wed Apr 24 14:48:30 2013 +0300
final edit
commit 1379f6b29ae442668132f629ef107a5806c05093
Author: human <human@desktop.vnet.local>
Date: Wed Apr 24 06:38:12 2013 -0400
todo created

Looks like our “final edit” commit made its way to desktop’s repository!

A frontend for git

Now that we’ve been up to what have you in the terminal, it might be a good time to try out a graphical frontend for git. I’ll be using a program called “giggle”:

master@palvelin:~/myproject/myproject$ sudo apt-get install giggle

Since we’re already in the project directory, we can just type “giggle” to launch the program, and it’ll visualize our repository like so:

giggle

Advertisements

About a1100320

IT student, musician, gamer. Beep boop.
This entry was posted in Linuxin keskitetty hallinta ICT4TN011-3. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s