Git tutorial

2018 Program Design 2

CSIE 梁祐承

Git ?

GitLab ?

GitHub ?

Git

  • Version control system
  • Trace code changelog
  • Work together
  • open source
  • Use with other tools ...

Without Git

With Git

  • hw1.cpp
  • hw1_old.cpp
  • hw1_new.cpp
  • hw1_test.cpp
  • hw1_final.cpp
  • hw1_true_final.cpp
  • hw1_classmate.cpp
  • hw1_from_senpai.cpp

Don't lost anything

GitHub

  • The world's largest software developing platform
  • Many famous repos (e.g. linux kernel)

 

GitLab

  • Build your own git server
  • open-sourced software

 

 

Environment setup

OS & Softwares

  • Ubuntu 16.04 LTS
  • Qt 5.9.2
  • git

You can install by yourself

but remember to test your code

with VM image provided by TA

Download VM image

Install VirtualBox

Select the host OS you're using

Add new VM

Select OS type

Set memory limit

Adjust by your computer spec!

Choose disk image

Use the image provided by TA

Start the VM

username & password are both 'pd2vm'

Overview

Installation

$ sudo apt install git

Pre-installed in VM provided by TA

If you already have linux installed...

If you're using windows...
Search from Google and choose the one you like

Basic

Repository (repo)

Commit

Basic unit in git.

In most case, you will call your project using git as repository.

Record you code change in git repository, you can view difference between commits.

Local & Remote

Working space

Staging area

Local repo

add

commit

Remote repo

(Github/Gitlab)

push

pull/clone

Git commands

Config

  • Let git know who you are
  • This should be done before you use git
$ git config --global user.email "your@email.com"
$ git config --global user.name "your_git_account"
$ git config --global core.editor "vim"

$ git config -l 
user.email=your@email.com
user.name=your_git_account
core.editor=vim

Clone

  • Get remote repo to your computer
  • You can bypass username and password input by setup SSH Key
$ git clone https://pd2b.imslab.org/gitlab/HMKRL/test.git
Cloning into 'test'...
Username for 'https://pd2b.imslab.org': <your username>
Password for 'https://hmkrl@pd2b.imslab.org': <your password>
remote: Counting objects: 35, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 35 (delta 10), reused 35 (delta 10)
Unpacking objects: 100% (35/35), done.

Init

  • Initialize your git repository
# Run command in your repo folder
$ git init 
Initialized empty Git repository in /home/user/my_proj/.git/

# ... start to write your code

Status

  • See current repo status
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

$ vim hw1.c

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	hw1.c

nothing added to commit but untracked files present (use "git add" to track)

Add

  • Add file to staging space
$ git status
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	hw1.c

nothing added to commit but untracked files present (use "git add" to track)

$ git add hw1.c
$ git status

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   hw1.c

Commit

  • Record staged change into repo
$ git status
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   hw1.c

$ git commit

# Edit commit message in vim
[master 52c637e] commit example
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hw1.c

Log

  • See commit log
$ git log

commit 52c637efecc9969cef9a8a753e59097143928b31
Author: Yu-Cheng Liang <a135246a135246@gmail.com>
Date:   Sat Mar 10 00:11:53 2018 +0800

    commit example

commit 613a350b0ddeaa4f586981404eae41704ef69c99
Author: You-Cheng Liang <liangyc39@gmail.com>
Date:   Sat Feb 10 13:55:46 2018 +0800

    sleep

Push

  • upload local repo to remote server
# set default push repo
$ git push -u origin master          

Username for 'https://pd2b.imslab.org': <your username>
Password for 'https://hmkrl@pd2b.imslab.org': <your password>
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 271 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://pd2b.imslab.org/gitlab/HMKRL/test.git
   613a350..52c637e  master -> master
Branch master set up to track remote branch master from origin.

# Push directly
$ git push

Pull

  • Sync local change from remote server
$ git pull
Username for 'https://pd2b.imslab.org': <your username>
Password for 'https://hmkrl@pd2b.imslab.org': <your password>
remote: Counting objects: 2, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From https://pd2b.imslab.org/gitlab/HMKRL/test
   52c637e..8f0b454  master     -> origin/master
Updating 52c637e..8f0b454
Fast-forward
 new_file | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new_file

Other advanced command

  • Checkout
  • Branch
  • Merge
  • Reset
  • diff
  • ...

Feel free to try if you're interested!

More git tutorials

Use GitLab

Confirm account

  • Goto your school mailbox
  • Click password reset link
  • Set your password
  • Confirm your e-mail
  • Login with your student ID

If password reset link expired or you forgot your password, click here and enter your <stuID>@mail.ncku.edu.tw

Create remote repo

Set repo name

Follow instruction 

Setup SSH Key

Generate RSA keypair

Get RSA public key

Copy your public key

Upload to GitLab

Paste your key

Try to login with SSH

Use SSH git url

Git Tutorial

By Liang Yu-Cheng

Git Tutorial

  • 4,381