Top Banner
Introduction to Git Database Systems DataLab, CS, NTHU Spring, 2020 1
80

Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Jun 13, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Introduction to Git

Database Systems

DataLab, CS, NTHU

Spring, 2020

1

Page 2: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Outline

• Version control system

• Git basics

• Git branch

• Remote repository

2

Page 3: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Outline

• Version control system

• Git basics

• Git branch

• Remote repository

3

Page 4: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Why Version Control ?

4

Page 5: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Students’ VCS

5

Page 6: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

How to work with others?

6

Page 7: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Dropbox VCS in Reality

7

Page 8: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Version Control System

• Store the projects, keep your revision history

• Synchronization between modifications made by

different developers

8

Page 9: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

https://memecrunch.com/meme/8QNIV/now-git

Page 10: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Outline

• Version control system

• Git basics

• Git branch

• Remote repository

10

Page 11: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Git

• Git is a version control system which is

• Fast

• Easy to use

• Distributed

• Able to handle large project

(ex. Linux Kernel 27.8 million lines)

• A git repository is a mini database that tracks your files

11

Page 12: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Installation

• Please check this link

• http://git-scm.com/book/en/Getting-Started-

Installing-Git

12

Page 13: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Configuration

• Modify ~/.gitconfig

• Or, type in following commands

git config --global user.name “your name”git config --global user.email “[email protected]

For more information, please refer this link

13

Page 14: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

• Two ways to create a repository

• Initializing a Repository in an Existing Directory

• Cloning an Existing Repository

• We will talk about it later

• The repository information will be stored in the .git

directory

Creating a new Repository

git init

14

Page 15: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Committing A Version

15

Page 16: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

• Staging (adding) a file

• Staging all files in the current directory

• Committing

Committing A Version

git add [file name]

git add -A

git commit -m “[message]”

16

Page 17: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

• Checking the current status and the current branch

Status

git status

17

Page 18: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

A History Tree

A9D013

B259F1

91DCA1

95165E

13CD8E

HEAD

HEAD is a pointer pointing to

the version you are looking at nowThe versions you committed

form a history tree

18

Page 19: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

• Listing the log

• Listing each log in one line

Logs

git log

git log --oneline

19

Page 20: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Checking Out A Version

A9D013

B259F1

91DCA1

95165E

13CD8EHEAD

git checkout 91DCA1

20

Page 21: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Outline

• Version control system

• Git basics

• Git branch

• Remote repository

21

Page 22: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Branches

Develop

Master

Optimization

A New Menu

Branching Merging

22

Page 23: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

The Master Branch

A9D013

B259F1

91DCA1

95165E

13CD8EHEAD Master

A branch in Git is actually

a label pointing to a version

Master branch is the default branch

created at the start time

23

Page 24: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Branching

91DCA1

95165E

13CD8EHEAD Master Feature

HEAD

24

Page 25: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Branching

91DCA1

95165E

13CD8EMaster

34F19D

Feature

HEAD

25

Page 26: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Branching

91DCA1

95165E

13CD8EMaster

34F19D Feature

HEAD

26

Page 27: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Branching

91DCA1

95165E

13CD8EMaster

34F19D Feature

HEAD

27

Page 28: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Branching

91DCA1

95165E

13CD8E

34F19D Feature

Master

HEAD

1F9D18

28

Page 29: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

• Creating a new branch (label)

• Checking out the branch (move the HEAD)

• Combining the above commands (create & checkout)

Git Branching

git branch [branch name]

git checkout [branch name]

git checkout -b [branch name]

29

Page 30: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Merging

13CD8E

34F19D

Feature

Master

HEAD

1F9D18

84091E

3827FA

30

Page 31: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

• Merging Steps

• Checking out a branch to merge

• Merging another branch

Git Merging

git checkout [branch 1 name]

git merge [branch 2 name]

31

Page 32: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Try Git!

Page 33: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

1. Find current directory

2. Go to that directory

33

Page 34: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

3. Add a new folder

5. Create a new file

4. Go to that folder

6. Put some contents and save

34

Page 35: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

$ git config --global user.name "name"$ git config --global user.email "email"

7. Setup user information

With --global: for all repositories in computer

Without --global: for current repository

35

Page 36: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

$ cd git-demo $ dir$ git init

8. Go to “git-demo”

9. Show the files in “git-demo”

10. Initialize a Git repository

36

Page 37: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

11. Add HelloGit.txt to staging files

12. Commit your changes

$ git add HelloGit.txt

$ git commit -m "version 1"37

Page 38: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

14. Add it and commit again

13. Make some changes and save

38

Page 39: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

15. View your versions

Version ID

Commit messages

$ git log

39

Page 40: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

16. Go to a specific version

$ git checkout {version_id}

40

Page 41: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

$ git log --oneline

15. Show versions with short version ID

Version ID

56% shorter!

41

Page 42: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

16. Go to a specific version

$ git checkout {short_version_id}

42

Page 43: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

17. Close and reopen HelloGit.txt

18. Back to the version 1!

43

Page 44: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Outline

• Version control system

• Git basics

• Git branch

• Remote repository

44

Page 45: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Collaboration with Git

• To work with others using git, you’ll need a server

that store the repository.

• Git is distributed, which means

• Everyone can store a copy of the repository

downloaded from the server

• They can do their jobs independently

45

Page 46: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Collaboration workflow

Local A Local B

Clone Clone

46

Page 47: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Collaboration workflow

Local A Local B

Commit

New

47

Page 48: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Collaboration workflow

Local A Local B

Push

NewNew

48

Page 49: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Collaboration workflow

Local A Local B

Push

New

New

Pull

New

49

Page 50: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

• Cloning the remote repositories

• The [Remote URL] is saved as Origin

• After committing a few versions, you can push the

branch back to Origin

Cloning & Pushing

git clone [Remote URL]

git push -u origin [Branch Name]

50

Page 51: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

• Updating a branch from the remote repository

• Fetching the remote repository to local

• Merging the remote branch

• Doing above commands in one command

Fetch & Pull

git fetch origin

git merge origin/[Branch Name]

git pull [Branch Name]

51

Page 52: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

13CD8E

34F19D1F9D18

84091E

3827FA

Fork

13CD8E

34F19D1F9D18

84091E

3827FA

The Repo. Under TA’s Account

52

Page 53: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

13CD8E

34F19D1F9D18

84091E

3827FA

Fork

13CD8E

34F19D1F9D18

84091E

3827FA

The Repo. Under TA’s Account The Repo. Under Your Account

53

Page 54: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

13CD8E

34F19D1F9D18

84091E

3827FA

Pull (Merge) Request

13CD8E

34F19D1F9D18

84091E

3827FA

The Repo. Under TA’s Account The Repo. Under Your Account

528A03

Pull

Request

Accept

528A03

54

Page 55: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

.gitignore File

• You can ignore some files that you don’t want them

to be tracked by editing the .gitignore file

• Remember to track and commit your .gitignore file

• Don’t know what should be in .gitignore ?

• https://github.com/github/gitignore

• https://www.gitignore.io/

55

Page 56: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

How to Submit Your

Code to Gitlab

Page 57: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Workflow

• For each lab, you should follow the workflow below

1. Fork our template repository on Gitlab

2. Clone the forked repository to your computer

3. Finish your lab

4. Commit in your computer

5. Push to Gitlab

6. Send merge request of your branch to our template repository

57

Page 58: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Workflow

• For each lab, you should follow the workflow below

1. Fork our template repository on Gitlab

2. Clone the forked repository to your computer

3. Finish your lab

4. Commit in your computer

5. Push to Gitlab

6. Send merge request of your branch to our template repository

58

Page 59: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

59

Page 60: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

60

1. Click to fork

Page 61: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

61

2. Check if this repository is under your account

3. Go to settings

Page 62: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

61

4. Set project to private

Page 63: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

62

5. Scroll down and save changes

Page 64: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Workflow

• For each lab, you should follow the workflow below

1. Fork our template repository on Gitlab

2. Clone the forked repository to your computer

3. Finish your lab

4. Commit in your computer

5. Push to Gitlab

6. Send merge request of your branch to our template repository

64

Page 65: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

65

1. Choose HTTPS

2. Copy the link

Page 66: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

If You use Windows

66

Page 67: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

3. Create a folder to put your repos

4. Type "git clone {URL}"

5. The repo has been successfully cloned

67

Page 68: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Workflow

• For each lab, you should follow the workflow below

1. Fork our template repository on Gitlab

2. Clone the forked repository to your computer

3. Finish your lab

4. Commit in your computer

5. Push to Gitlab

6. Send merge request of your branch to our template repository

68

Page 69: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

1. -A means all files

2. Check if your file is added to git

3. Commit your changes

69

Page 70: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

If you see these message, type

git config --global user.name "{name}"

git config --global user.email "{email}"

{email} is the email you use on gitlab

70

Page 71: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Workflow

• For each lab, you should follow the workflow below

1. Fork our template repository on Gitlab

2. Clone the forked repository to your computer

3. Finish your lab

4. Commit in your computer

5. Push to Gitlab

6. Send merge request of your branch to our template repository

71

Page 72: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Type "git push -u origin master"

72

Page 73: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Workflow

• For each lab, you should follow the workflow below

1. Fork our template repository on Gitlab

2. Clone the forked repository to your computer

3. Finish your lab

4. Commit in your computer

5. Push to Gitlab

6. Send merge request of your branch to our template repository

73

Page 74: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

74

1. Click Merge Requests

Page 75: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

75

2. New merge request

Page 76: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

76

3. Choose the branch you

pushed in your repo

4. Choose the branch

named after your ID

5. Compare branches

Page 77: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

77

6. Set title to "{ID} Submission"

Page 78: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

78

7. If everything is OK,

submit your merge request

Page 79: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your
Page 80: Introduction to Git - GitHub Pages · Workflow • For each lab, you should follow the workflow below 1. Fork our template repository on Gitlab 2. Clone the forked repository to your

Reference

• Learn Git branching (interactive)

• http://pcottle.github.io/learnGitBranching/

• Pro Git

• http://git-scm.com/book/

• 寫給大家的 Git 教學

• http://www.slideshare.net/littlebtc/git-5528339

81