brief tutorial on git

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

Upload: -

Post on 06-Jan-2017

3.091 views

Category:

Engineering


1 download

TRANSCRIPT

Brief tutorial on Git

Colin Cheng ( 鄭聖文 )[email protected]

NCTU Robotic Vision class

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

What is Git?

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

Before you start using version control system ...

I think I can manage my project very well

without git

code_2016_1_1

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

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

Before you start using version control system ...

Help!

Dude…

Before you start using version control system ...

Help!

Dude…

Inefficient!

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

My source code size is approximately 6GB

Great! Now you know whywe need to learn 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

Example

Copy the link

https://github.com/duckietown/software

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

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

Git init

Suppose you have a project called “duckie”,you have to git init the directory once beforeusing the 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

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

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

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

Create the file as follow

file: duckie/hello.txt

Hello World

Git status

git status See the change in this project directory

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

Git add

git add hello.txt Store the new modification

Commit

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

Git commit

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

So...

Please...

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

Git should only track the source file!

Hide the file you don't want git to track

For example:

'

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!

Hide the file you dont want to track

See:

Disappear below

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

Check the logtig You will see:

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)

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

Create github repositoryLogin and click the “New repository” button

Create github repository

Create github repository

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

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

Git push (Upload)

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

Git pushGit push origin master

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

Now, refresh your github page!

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

Git pullGit pull origin master

(git pull [REMOTE] [BRANCH])

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

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

Branch

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

Merge

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

This is a merge

Head

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

Head of master

Head

And this will be the Head of the very_nice_feature branch:

Also a Head

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

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

Fork

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

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

Fork

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

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

Pull request

Pull request

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