Lab 2 - Git / SSH - COMP2100/6442 Skip navigation COMP2100/6442 ANU College of Engineering & Computer Science Search query Search ANU web, staff & maps Search COMP2100/6442 Exams menu Search query Search ANU web, staff & maps Search Search COMP2100/6442 labs Overview Lab 1 - Install Lab 2 - Git / SSH Lab 3 - Trees Lab 4 - Parser Lab 5 - Android 1 Lab 6 - Android 2 Lab 7 - Persistent Data Lab 8 - PHP Lab 9 - Lab Test 1 Lab Test 2 related sites Wattle Lab 2 - Git / SSH Task 1 - Git The main objective of this stage is to understand and get some practice with some basic git operations. All the operations in this stage should be done in the terminal. You could always using git help to get instructions of the command or git help [command] for specific command instructions. Setting up your name and E-mail address for the git. For example, your name is Jin Kurosawa and your E-mail address is u48694869@anu.edu.au, you have to execute git config --global user.name "Jin Kurosawa"
git config --global user.email u48694869@anu.edu.au
Setting up a new repository (repo). Create a new directory under your home directory, named as ‘helloworld’. cd to that directory, execute git init. The terminal should shows ‘Initialized empty Git repository in [folder path]’. Now you have successfully create a repository. If you do ls -al under the ‘helloworld’ directory it should have a directory named as ‘.git’. Create a file named as ‘HelloWorld.java’ and implement a simple program which outputs “Hello World!” in terminal. Add the file to the git repo. Execute the command git status. You can see that your created ‘HelloWorld.java’ is red under ‘Untracked files’. Now you have to add the file as a change to the previous state (which is empty). Execute command git add ./helloworld.java. It should add the file to the current changes. Now execute the command git status again, the ‘Hello World.java’ should be green with ‘new file: ‘ in front of it under ‘Changes to be committed:’ Commit the change. Execute the command git commit -m "[Your comments for the commit]", for example, git commit -m "Add HellWorld.java" to commit the changes. Then the terminal should show the branch name with your comments, then following the number of file changes, insertions and deletions. You can also execute git commit and use a text editor for creating a better formatted comments. Check your commits. Using command git log to check your commits. If you done the previous steps correctly, your commit from the last step should appear in the terminal. Make some changes to your ‘HellWorld.java’ file, and create 3 more commits. Check the result by using git log command. Create a repo on Gitlab. Access the gitlab of CECS using the web browser. Log in with your uni-id and password. Click the add button and selecting ‘New project’. Fill in the Project name with HelloWorld and click ‘Create project’. It should bring you to a new page and shows you that the project was successfully created. Add the Gitlab repo as a remote. If you could find ‘SSH’ around your Project ID, then change it into ‘HTTPS’. Copy the URL next to the ‘HTTPS’ (it should be something like ‘https://gitlab.cecs.anu.edu.au/u48694869/helloworld.git’). Now go back to your terminal, execute git remote add origin [url] and replace the part [url] with your https starts URL. Now execute git remote -v it should show you two lines of URL. Both of them should start with ‘origin’ and followed by the URL you just pasted. One of them ended with ‘(fetch)’ and one with ‘(push)’. These two URLs marks the remote URL when git push and fetch the remote repo. Push to the remote repo. Now execute git push origin master. After typing your uni-id and password, it should shows ‘[new branch] master -> master’ for the result. If you refresh the gitlab page, your file should appear in the page. Clone a remote repo. Now cd to a different directory to the ‘helloworld’. The directory should not be inside the ‘helloworld’ directory. Then execute git clone [url] and replace the url with your HTTPS URL address. It should shows ‘Cloning into ‘helloworld’…’ and asking for your uni-id as username and password. After that, a new directory named as ‘helloworld’ should appear. To avoid confusion, we rename this directory to ‘helloworld2’ using the mv helloworld helloworld2 command. Creating a conflict. cd into your ‘helloworld2’ directory, make some changes and create a new commit. Push the commit to the remote repo. Now go back to your original ‘helloworld’ directory, make some changes and create a new commit. Now try to push your commit, you should get an ‘[rejected]’ with ‘(fetch first)’. This means the remote repo contains some changes that your local repo doesn’t contain. You have to pull those changes down to your local repo and make a merge. Pull down and merge the repo. Execute git pull origin master, it should start pulling down the changes. At the end of the terminal result, there should be a line with ‘Auto-merging HelloWorld.java’. Git will try to auto merge the file, but some times it fails and shows as a ‘CONFLICT’. The last line should show you ‘Automatic merge failed; fix conflicts and then commit the result.’. Now open your ‘HelloWorld.java’ file, you can see there are a few lines are surrounded by ‘«««<’, ‘=======’ and ‘»»»>’, which the HEAD shows your local repo version of the part of the file and the one with hex hash shows the remote version of the file. Solve the conflict, commit and update your repo. You have to modify the code and make it work again. Those lines added by git could be removed, it is just used for marking the position where is different. Now commit your changes and try to push it to remote repo. It should now push with no error. Now cd to ‘helloworld2’ directory and try to pull the changes down. It should pull both commits down. Check it by git log command. Task 2 - SSH setup for Git The main objective of this stage is to understand and get some practice with SSH setup and use them for ssh, scp and gitlab. Create your own public private key pair. To generate a key pair you have to execute ssh-keygen. You need to enter the location for saving the key and an optional passphrase for protecting the key or just using the default settings. Find your keys in ‘.ssh’ directory under your home directory. They should named as ‘id_rsa’ for private key (which you never want anybody else know) and ‘id_rsa.pub’ for public key (which you can show to other people). Using your SSH key to log in to partch. Using ssh [your uid]@partch.anu.edu.au login to partch with your uni-id password, open the ‘authorized_keys’ file under ‘.ssh’ directory and paste the content of public key in to a new line of ‘authorized_keys’ (You need to make ‘authorized_keys’ file if you can’t find one). Save and exit the text editor. Log out and then log in again, it should using your SSH keys. If you set a passphrase for the private key, you have to input the passphrase of private key but not the uni-id password. If you didn’t set any passphase, it should directly logged into the partch. Try to copy some file using scp from your local hard drive to partch, it should copy without asking you for your uni-id password. Using your SSH key settings for clone, push and pull for the Gitlab of CECS. First access the gitlab of CECS. You have to login and open your settings. On the left hand side list, there should be one item named as ‘SSH Keys’. You have to paste your public key in to the input box of ‘Key’ area, then click the ‘Add Key’ button. Now when you clone the ‘HelloWorld’ repo using the ‘SSH’ hyperlink (not the ‘HTTPS’ one), it should not asking for your uni-id password for authorized. Task 3 - Makefile The main objective of this stage is to understand and get some practice with Makefile. Create a file named as ‘Makefile’ under your ‘helloworld’ directory. Using text editor to modify the Makefile. You have to implement 3 targets for the Makfile: ‘all’ for compiling your ‘HelloWorld.java’ into ‘HelloWorld.class’, ‘run’ for executing the ‘HelloWorld.class’ with Java and ‘clean’ for removing the ‘HelloWorld.class’ file. Try your Makefile with the make command. Updated: / Responsible Officer: / Page Contact: Contact ANU Copyright Disclaimer Privacy Freedom of Information +61 2 6125 5111 The Australian National University, Canberra CRICOS Provider : 00120C ABN : 52 234 063 906 You appear to be using Internet Explorer 7, or have compatibility view turned on. Your browser is not supported by ANU web styles. » Learn how to fix this » Ignore this warning in future