Git and GitHub for Beginners I(Tutorial)

PJ Wang
CS Note
Published in
5 min readNov 6, 2017

What is Git?

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for source code management in software development,[8] but it can be used to keep track of changes in any set of files. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows.[1]

Why Git?

Version control is the only reasonable way to keep track of changes in code, manuscripts, presentations, and data analysis projects. I used to make numbered tar.gz files for a project. But exploring the differences is difficult, to say the least. And if you use git properly, you’ll have annotated each small change.

Merging collaborators’ changes made easy. Have you ever had to deal with a collaborator sending you modifications distributed across many files, or had to deal with two people having made changes to the same file at the same time? Painful. git mergeis the answer. [3]

Install Git

Ubuntu

Open terminal and code the scripts

$ sudo apt-get install git-all

Mac OS

Download link: Click

Windows

Download link: Click

Git GUI

SourceTree: Click

Tutorial

Step 1: Make the directory where you want to use Git

Desktop user$ mkdir gitDemo
Desktop user$ cd gitDemo

Step 2: Setup “config”

gitDemo user$ git config --global user.name "daniel wang"
gitDemo user$ git config --global user.email "daniel820710@gmail.com"

Check your config

gitDemo user$ git config --list

Step 3: Initial the Git

gitDemo user$ git init

Generate a hidden directory “.git” in your directory

.DS_Store is a file that stores custom attributes of its containing folder, such as the position of icons or the choice of a background image.

How to show hidden files on Mac

Type the command:

user$ defaults write com.apple.finder AppleShowAllFiles TRUE;\killall Finder

However, if you want to hide hidden files

user$ defaults write com.apple.finder AppleShowAllFiles FALSE;\killall Finder

Step 4: Add / Clone file and commit file

Introduction

Work Flow

Workspace: What you see in your editor and where you make your changes.

Staging Area: A snapshot of your working tree at a particular point in development.

Local Repository: Your copy of a project, initialized as a Git repository.

Remote Repository: The shared copy of the repo that lives on a remote server.

In workspace

Clone files from github

gitDemo user$ git clone https://github.com/libgit2/libgit2

Or add files from local

gitDemo user$ echo test123 > testFile.txt
gitDemo user$ git status

New file is “Untracked files” before execute “git add”.

gitDemo user$ git add testFile.txt
gitDemo user$ git status

In Staging Area

After the first time you add file, you could remove file and back to “workspace” by following instruction.

gitDemo user$ git reset HEAD testFile.txt

Or you could commit it with message.

gitDemo user$ git commit -m "Add testFile.txt"
gitDemo user$ git status

In Local Repository

You could amend the message about last commit.

gitDemo user$ git commit --amend

If you regret to commit, you could go back to previous version.

gitDemo user$ git reset --hard HEAD^

The last three commits (HEAD, HEAD^, and HEAD~2)

Other command

Revert

This command creates a new commit that undoes the changes from a previous commit. This command adds new history to the project (it doesn’t modify existing history).

Add and commit the file named “testFile2.txt”, and execute the command.

gitDemo user$ git revert HEAD
gitDemo user$ git log

Reset

This command is back to previous version including log.

Add and commit the file named “testFile2.txt”.

gitDemo user$ git log

Type the command, and the file can not recover.

gitDemo user$ git reset --hard HAED^
gitDemo user$ git log

If you don’t want to delete the file, type other command:

gitDemo user$ git reset --soft HEAD^
gitDemo user$ git status
gitDemo user$ git reset --mixed HEAD^
gitDemo user$ git status

Tag

Tag the last commit version.

gitDemo user$ git tag -a v1.0 -m "version 1.0"
gitDemo user$ git show v1.0

Tag any version you want

First, check which version you want to tag.

gitDemo user$ git log --pretty=oneline

Second, type the command:

gitDemo user$ git tag -a v1.0 6c32c24 -m "version 1.0"
gitDemo user$ git log --pretty=oneline

If you want to delete tag, type the command:

gitDemo user$ git tag -d v1.0

How to use branch?

Git and GitHub for Beginners II(Tutorial)

How to use GitHub?

Git and GitHub for Beginners III(Tutorial)

--

--

PJ Wang
CS Note

台大資工所碩畢 / 設計思考教練 / 系統思考顧問 / 資料科學家 / 新創 / 科技 + 商業 + 使用者