56
Brief tutorial on Git Colin Cheng ( 鄭聖文 ) [email protected] NCTU Robotic Vision class

Brief tutorial on Git

  • Upload
    -

  • View
    3.201

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Brief tutorial on Git

Brief tutorial on Git

Colin Cheng ( 鄭聖文 )[email protected]

NCTU Robotic Vision class

Page 2: Brief tutorial on Git

Outline

Begin● Clone a project from Internet● Create a git project● Git commit● Git push/pull

Intermediate● Branch● Fork a project on Github● Send a pull request

Page 3: Brief tutorial on Git

What is Git?

● A version control system for project management● Create by Linux's author Linus Torvalds

Page 4: Brief tutorial on Git

Before you start using version control system ...

I think I can manage my project very well

without git

code_2016_1_1

Page 5: Brief tutorial on Git

Before you start using version control system ...

I think I can manage my project very well

without git

code_2016_1_1 code_2016_1_3

Page 6: Brief tutorial on Git

Before you start using version control system ...

I think I can manage my project very well

without git

code_2016_1_1 code_2016_1_3 code_2016_2_4

Page 7: Brief tutorial on Git

Before you start using version control system ...

Help!

Dude…

Page 8: Brief tutorial on Git

Before you start using version control system ...

Help!

Dude…

Inefficient!

Page 9: Brief tutorial on Git

Also, if the source code size is too big...

My source code size is approximately 6GB

Page 10: Brief tutorial on Git

Great! Now you know whywe need to learn Git

Page 11: Brief tutorial on Git

Outline

Begin● Clone a project from Internet● Create a git project● Git commit● Git push/pull

Intermediate● Branch● Fork a project on Github● Send a pull request

Page 12: Brief tutorial on Git

Example

Copy the link

https://github.com/duckietown/software

Page 13: Brief tutorial on Git

Git clone

cd ~git clone https://github.com/duckietown/software.gitcd Software/ls

Whereever you want to store the code, ~ means the home directory

Clone command

Page 14: Brief tutorial on Git

Outline

Begin● Clone a project from Internet● Create a git project● Git commit● Git push/pull

Intermediate● Branch● Fork a project on Github● Send a pull request

Page 15: Brief tutorial on Git

Git init

Suppose you have a project called “duckie”,you have to git init the directory once beforeusing the git

Page 16: Brief tutorial on Git

Git init

cd ~mkdir duckiecd duckie/git initls -a

Create new directory called duckie

Init the project with git

List all the files including the hidden file

Git stores all the informations, configurations in this floder

Page 17: Brief tutorial on Git

Outline

Begin● Clone a project from Internet● Create a git project● Git commit● Git push/pull

Intermediate● Branch● Fork a project on Github● Send a pull request

Page 18: Brief tutorial on Git

Setup your git user informationgit config --global user.name "user name"git config --global user.email "email address"

Page 19: Brief tutorial on Git

Modify the file

Now, we are going to modify the file, and usegit to help us store the changes (track).

We will use vim editor, but if you are not yet familiar with vim, you could try gedit (or Sublime)

However, I still hope you know how to use vim

Page 20: Brief tutorial on Git

Create the file as follow

file: duckie/hello.txt

Hello World

Page 21: Brief tutorial on Git

Git status

git status See the change in this project directory

Git found we have a new file, but not tracking yet

Page 22: Brief tutorial on Git

Git add

git add hello.txt Store the new modification

Page 23: Brief tutorial on Git

Commit

After you add all the files (if you have multiple files to add), you can commit all the changes toa single log

Page 24: Brief tutorial on Git

Git commit

Git commit -m ‘Create new file’ Commit description should be brief and easyto understand!

Page 25: Brief tutorial on Git

So...

Page 26: Brief tutorial on Git

Please...

Please do not git add the *.exe file,log file, etc…

Git should only track the source file!

Page 27: Brief tutorial on Git

Hide the file you don't want git to track

For example:

'

Page 28: Brief tutorial on Git

Create .gitignore

You can create a .gitignore file at your projectroot directory and list the file you don’t want git to track

file: duckie/.gitignore

*.exe

Yes, you can use the regular expression!

Page 29: Brief tutorial on Git

Hide the file you dont want to track

See:

Disappear below

Page 30: Brief tutorial on Git

See the changes log

Now, we will like to see all the changes, We can use the program “tig”

Type the following command to install it:sudo apt get install tig

Page 31: Brief tutorial on Git

Check the logtig You will see:

Page 32: Brief tutorial on Git

MoreType [Enter] to check the log, [q] to leave

Git will also generate a unique id for every commit(SHA-1), so we can operate the commit by assinging theid (Like invert the commit, etc)

Page 33: Brief tutorial on Git

Outline

Begin● Clone a project from Internet● Create a git project● Git commit● Git push/pull

Intermediate● Branch● Fork a project on Github● Send a pull request

Page 34: Brief tutorial on Git

Create github repositoryLogin and click the “New repository” button

Page 35: Brief tutorial on Git

Create github repository

Page 36: Brief tutorial on Git

Create github repository

Actually, you can also learn how tocreate a new git project at here

Page 37: Brief tutorial on Git

Back to your conslegit remote add origin https://github.com/XXXX/duckie.git

Check the link on your github repo!

This command will help you create a remotenamed “origin” and point to the github repo

Page 38: Brief tutorial on Git

Git push (Upload)

Now, you can push your code to your github repo (remote)

Page 39: Brief tutorial on Git

Git pushGit push origin master

(git push [REMOTE] [BRANCH]), I will explain what is the branch later

Page 40: Brief tutorial on Git

Now, refresh your github page!

Page 41: Brief tutorial on Git

Git pull

In previous, I taught you how to clone a project,But what if the develper add new features andYou wish to keep your clone up-to-date?

Now, you need to to use the git pull command

Page 42: Brief tutorial on Git

Git pullGit pull origin master

(git pull [REMOTE] [BRANCH])

Well, this is already up-to-date,try this next time when you need it

Page 43: Brief tutorial on Git

Outline

Begin● Clone a project from Internet● Create a git project● Git commit● Git push/pull

Intermediate● Branch● Fork a project on Github● Send a pull request

Page 44: Brief tutorial on Git

Branch

In git, you can create different branches and develop your project at the same time:

Page 45: Brief tutorial on Git

Merge

After you finish developing on certin branch, you can merge it back to the master(or other branch)

This is a merge

Page 46: Brief tutorial on Git

Head

Head is the latest commit on your branch,If you are now at the master branch, this isyour Head:

Head of master

Page 47: Brief tutorial on Git

Head

And this will be the Head of the very_nice_feature branch:

Also a Head

Page 48: Brief tutorial on Git

Some git branch commands

git branch new_branch_name ← Create a new branchgit branch ← Check current branchgit checkout branch_name ← Switch to certin branchgit merge branch_name ←merge certen branch back to the current branch

*If there is any confict during the merge, git will ask you to fix the the conflicts

Page 49: Brief tutorial on Git

Outline

Begin● Clone a project from Internet● Create a git project● Git commit● Git push/pull

Intermediate● Branch● Fork a project on Github● Send a pull request

Page 50: Brief tutorial on Git

Fork

Fork is a Github feature, you can forksomeone’s project and store it in your githubRepo page

Page 51: Brief tutorial on Git

ForkClick the buttonhttps://github.com/duckietown/software

Page 52: Brief tutorial on Git

Fork

Page 53: Brief tutorial on Git

Outline

Begin● Clone a project from Internet● Create a git project● Git commit● Git push/pull

Intermediate● Branch● Fork a project on Github● Send a pull request

Page 54: Brief tutorial on Git

Pull request

Pull request is also a github feature.

After you fork the project, you may add more new functions. you can send a pull request to the original author and ask him/her to merge your changes

Well, the author have the right to merge or not

Page 55: Brief tutorial on Git

Pull request

Page 56: Brief tutorial on Git

Pull request

After you click the button, the Pull request will be sent