Download pdf - Basic git-commands

Transcript
Page 1: Basic git-commands

Basic

Commands

insanehong@KGUG2014. 12. 20

Page 2: Basic git-commands

NAVER / NAVER LABS

twitter: @insanehong

email : [email protected]

http://about.me/insanehong

insanehong.talk.start(“Hi! Bro!”)

Page 3: Basic git-commands

이시간에 다루지 않을 것들

Page 4: Basic git-commands

https://www.flickr.com/photos/miguelpdl/4994396370

Page 5: Basic git-commands

이시간에 전달하려고 하는 것들

Page 6: Basic git-commands

기본적인 사용법 & 개념 내부 동작 원리

Page 7: Basic git-commands

주의! 이 교육은 간접 광고를 포함하고 있지 않습니다.

http://www.dailycal.org/2013/11/21/everybody-macbook/

Page 8: Basic git-commands

Why do programmers use or recommend

macs or linux?

http://www.dailycal.org/2013/11/21/everybody-macbook/

주의! 이 교육은 간접 광고를 포함하고 있지 않습니다.

Page 9: Basic git-commands

& CLIGUIRecommend

Page 10: Basic git-commands

NAVER give up!!e

이 교육은 간접 광고를 절대 포함하고 있지 않습니다.

photo by https://www.flickr.com/photos/slworking/

Page 11: Basic git-commands

Try git better, together

Page 12: Basic git-commands

우리 모두는 A사 입사를 위해 퀴즈를 풀어 제출해야 합니다.

Page 13: Basic git-commands

채용 퀴즈

“같은 문자가 연속적으로 반복되는 경우에 그 반복 횟수를 표시하여 문자열을 압축하기”

제출 방법

[email protected] 으로 github url 제출

• 첨부파일로는 받지 않습니다.

Page 14: Basic git-commands

Great a Git repository

Page 15: Basic git-commands

git init

“This command creates an empty git repository”

- https://www.kernel.org/pub/software/scm/git/docs/git-init.html -

Page 16: Basic git-commands

• 현재 디렉토리를 git 저장소로 만들 때

$> git init

• 새로운 디렉토리를 만들때

$> git init <directory>

Page 17: Basic git-commands

/workspace

$> git init git-study && cd git-study Initialized empty Git repository /workspace/git-study/.git/

Page 18: Basic git-commands

/git-study

.git└─

Page 19: Basic git-commands

/git-study

.git└─

Page 20: Basic git-commands

Repository Structure

Page 21: Basic git-commands

Working tree

index (Staging area)

object database

Working area

Cache storage Storage

Page 22: Basic git-commands

Working tree

index (Staging area)

object database

git add git commit

Page 23: Basic git-commands

여기서 잠깐!

Page 24: Basic git-commands

What is commit?

Page 25: Basic git-commands

“In computer science and data management, a commit is the making of a set of tentative

changes permanent.”- http://en.wikipedia.org/wiki/Commit_(data_management) -

Page 26: Basic git-commands

“특정 시점에 대한 스냅샷 혹은 상태”

Page 27: Basic git-commands

Git 사용한다는 것?

Page 28: Basic git-commands

Git 으로 commit 을 다루는것!

Page 29: Basic git-commands

Untracked Staged Committed Modified

Tracked

git add git commit edit file

git add

file

Page 30: Basic git-commands

Be committed to doing

Page 31: Basic git-commands

/git-study

.git├─

compress.js└─

Page 32: Basic git-commands

http://git.io/Tq59Hw

Page 33: Basic git-commands

Working tree

index (Staging area)

object database

git add git commit

Page 34: Basic git-commands

git add “This command updates the index using

the current content found in the working tree, to prepare the content staged for the next commit”

- https://www.kernel.org/pub/software/scm/git/docs/git-add.html -

Page 35: Basic git-commands

“working directory 에서 변경되거나 추가된 파일들 중에서 커밋에 포함될 파일들을 등록해 놓는 것”

Page 36: Basic git-commands

• 특정 파일들만을 지정

$> git add <file1> <file2>

• 변경되거나 추가된 모든 파일을 다룰때

$> git add -a

Page 37: Basic git-commands

여기서 잠깐 Two!

Page 38: Basic git-commands

변경 파일이 많거나 어떤 파일이 수정 되었는지 모를때는?

Page 39: Basic git-commands

git status “Displays paths that have differences between the index

file and the current HEAD commit, and paths in the working tree that are not tracked by git”

- https://www.kernel.org/pub/software/scm/git/docs/git-status.html -

Page 40: Basic git-commands

“Woking directory 의 상태를 보여준다. ”

Page 41: Basic git-commands

• 자세한 내용을 확인 할때

$> git status

• 파일들의 변경 상태만 확인 할때

$> git status -s

Page 42: Basic git-commands

$> git status On branch master

Initial commit

Untracked files: (use "git add <file>..." to include in what will be committed)

compress.js

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

$> git status -s ?? compress.js

Page 43: Basic git-commands

/git-study

.git├─

compress.js└─

$> git add compress.js $> git status On branch master

Initial commit

Changes to be committed: (use "git rm --cached <file>..." to unstage)

new file: compress.js

$> git status -s A compress.js

Page 44: Basic git-commands

Working tree

index (Staging area)

object database

git add git commit

Page 45: Basic git-commands

git commit “Stores the current contents of the index in a

new commit along with a log message from the user describing the changes.”

- https://www.kernel.org/pub/software/scm/git/docs/git-commit.html -

Page 46: Basic git-commands

/git-study

.git├─

compress.js└─

$> git commit -m “문제풀이 완료” [master (root-commit) 5525f5f] 문제풀이 완료 1 file changed, 28 insertions(+) create mode 100644 compress.js

$> git status On branch master nothing to commit, working directory clean

5525f5f 문제풀이 완료

Page 47: Basic git-commands

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; } else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

Untracked

Page 48: Basic git-commands

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; } else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; } else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

blobfunction compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

81b90e8

Staged

Page 49: Basic git-commands

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; } else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; } else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

blobfunction compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

tree

81b90e8

3cfa7a2

compress.js 81b90e8

commit 5525f5f

tree 3cfa7a2

author insnae<insnae@mail> 1330482646 +0900

commiter insnae<insnae@mail> 1330482646 +0900

문제풀이 완료

Commited

Page 50: Basic git-commands

자 이제 코드를 제출하면 되나요?

Page 51: Basic git-commands

$> node compress hello world, hello git > hel2o world, hel2o git helloooooooo > hel2o

제가 한번 실행해 보겠습니다!

Page 52: Basic git-commands

https://www.flickr.com/photos/slworking/8739080572

이상한 점을 찾으셨나요?

Page 53: Basic git-commands

“helloooooooo”> hel2o

currently

Page 54: Basic git-commands

“helloooooooo”> hel2o8

Goals

Page 55: Basic git-commands

8 if( str.charAt(i) === prev ) { 9 repeat++; 10 if ( i === str.length -1) result+=repeat; 11 } else {

Solution

Page 56: Basic git-commands

8 if( str.charAt(i) === prev ) { 9 repeat++; 10 if ( i === str.length - 1 ) result += repeat; 11 } else {

• 버그 수정후 “버그 수정” 이란 메세지로 커밋

• header 추가 후 커밋 “헤더 추가” 란 메세지로 커밋

1 /* 2 Author insanehong <[email protected]> 3 LICENSE under BSD 4 */

Page 57: Basic git-commands

git diff “Show changes between the working tree and

the index or a tree, changes between the index and a tree, changes between two trees, or changes

between two files on disk..”- https://www.kernel.org/pub/software/scm/git/docs/git-diff.html -

Page 58: Basic git-commands

• working directory 와 index 를 비교 $> git diff

• 커밋 간 차이점 을 비교 $> git diff <commitA>..<commitB>

• index 와 HEAD를 비교 $> git diff --staged

Page 59: Basic git-commands

/git-study

.git├─

compress.js└─

│$> git diff diff --git a/compress.js b/compress.js index 81b90e8..46c3d6e 100644 --- a/compress.js +++ b/compress.js @@ -7,6 +7,7 @@ function compress(str) {

if( str.charAt(i) === prev ) { repeat++; + if ( i === str.length -1) result+=repeat; } else { if (repeat > 1) result+=repeat; result+=str.charAt(i);

5525f5f 문제풀이 완료

Page 60: Basic git-commands

/git-study

.git├─

compress.js└─

│ $> git add compress.js

$> git status -s M compress.js

$> git commit -m “버그수정” [master f5cd615] 버그 수정 1 file changed, 1 insertion(+)

5525f5f 문제풀이 완료

f5cd615 버그 수정

Page 61: Basic git-commands

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; if ( i === str.length - 1 ) result += repeat;

} else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; } else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

blobfunction compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

tree

81b90e8

3cfa7a2

compress.js 81b90e8

commit 5525f5f

tree 3cfa7a2

author insnae<insnae@mail> 1330482646 +0900

commiter insnae<insnae@mail> 1330482646 +0900

문제풀이 완료

Modified

Page 62: Basic git-commands

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; if ( i === str.length - 1 ) result += repeat;

} else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; if ( i === str.length - 1 ) result += repeat;

} else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

blobif( str.charAt(i) === prev ) { repeat++; if ( i === str.length - 1 ) result += repeat;

} e

b3bddc

commit

81b90e8

tree

3cfa7a2

blob

81b90e8

Staged

Page 63: Basic git-commands

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; if ( i === str.length - 1 ) result += repeat;

} else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

compress.js

function compress(str) { var repeat = 1; var result = str.charAt(0); var prev = result;

for( var i =1; i < str.length; i++ ) {

if( str.charAt(i) === prev ) { repeat++; if ( i === str.length - 1 ) result += repeat;

} else { if (repeat > 1) result += repeat; result += str.charAt(i); repeat = 1; }

prev = str.charAt(i);

}

console.log(str, ' > ', result);

}

var test1 = "hello world, hello git"; var test2 = "helloooooooo";

compress(test1); compress(test2);

commit

81b90e8

tree

3cfa7a2

blob

81b90e8

commit

f5cd61

tree

1b3d8b

blob

b3bddc

Commited

Page 64: Basic git-commands

/git-study

.git├─

compress.js└─

│ $> git add compress.js

$> git status -s M compress.js

$> git commit -m “헤더 추가” [master b6f9dc3] 헤더 추가 1 file changed, 4 insertions(+)

5525f5f 문제풀이 완료

f5cd615 버그 수정

b6f9dc3 헤더 추가

Page 65: Basic git-commands

Commit inspection

Page 66: Basic git-commands

git log

“Shows the commit logs.”

- https://www.kernel.org/pub/software/scm/git/docs/git-log.html -

Page 67: Basic git-commands

• 현재 저장소의 시간순 커밋 히스토리 $> git log

• diff 를 함께 보기 $> git log —p

• 커밋 메세지 조회 $> git log --grep <검색어>

Page 68: Basic git-commands

$> git log commit b6f9dc38625689032f6172bef905ae0a2aeebaa0 Author: insanehong <[email protected]> Date: Sat Dec 20 00:00:47 2014 +0900

헤더 추가

commit f5cd615c4643dce328fe56e291a16c43ef9c3a77 Author: insanehong <[email protected]> Date: Fri Dec 19 23:54:00 2014 +0900

버그 수정

commit 5525f5f0b8945c342a489ab1ff72469e5745a33e Author: insanehong <[email protected]> Date: Fri Dec 19 23:18:42 2014 +0900

문제풀이 완료

$> git log --grep "버그" commit f5cd615c4643dce328fe56e291a16c43ef9c3a77 Author: insanehong <[email protected]> Date: Fri Dec 19 23:54:00 2014 +0900

버그 수정

5525f5f 문제풀이 완료

f5cd615 버그 수정

b6f9dc3 헤더 추가

Page 69: Basic git-commands

git show

“Shows one or more objects (blobs, trees, tags and commits)”

- https://www.kernel.org/pub/software/scm/git/docs/git-show.html -

Page 70: Basic git-commands

$> git show commit b6f9dc38625689032f6172bef905ae0a2aeebaa0 Author: insanehong <[email protected]> Date: Sat Dec 20 00:00:47 2014 +0900

헤더 추가

diff --git a/compress.js b/compress.js index 46c3d6e..cfdcf63 100644 --- a/compress.js +++ b/compress.js @@ -1,3 +1,7 @@ +/* +Author insanehong <[email protected]> +*/ + function compress(str) { var repeat = 1; var result = str.charAt(0);

5525f5f 문제풀이 완료

f5cd615 버그 수정

b6f9dc3 헤더 추가

Page 71: Basic git-commands

Push to remote repository

Page 72: Basic git-commands

bare

None-bare

None-bare

None-bare

Page 73: Basic git-commands

Github

Local repo

Local repo

Local repo

Page 74: Basic git-commands

git remote

“Manage the set of repositories ("remotes") whose branches you track.”

- https://www.kernel.org/pub/software/scm/git/docs/git-remote.html -

Page 75: Basic git-commands

• 원격 저장소 추가 $> git remote add <alias> <url>

• 원격 저장소 alias 변경 $> git remote rename <old> <new>

• 원격 저장소 url 변경 $> git remote set-url <alias> <url>

• 원격 저장소 확인 $> git remote -v

Page 76: Basic git-commands

$> git remote add origin [email protected]:insanehong/git-study.git $> git remote -v origin https://github.com/insanehong/git-repo.git (fetch) origin https://github.com/insanehong/git-repo.git (push)

.git

config└─

Page 77: Basic git-commands

git push

“Updates remote refs using local refs, while sending objects necessary to complete the given refs.”

- https://www.kernel.org/pub/software/scm/git/docs/git-push.html -

Page 78: Basic git-commands

$> git push origin master Counting objects: 9, done. Delta compression using up to 4 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (9/9), 965 bytes | 0 bytes/s, done. Total 9 (delta 2), reused 0 (delta 0) To [email protected]:insanehong/git-study.git * [new branch] master -> master

/git-study

.git├─

compress.js└─

5525f5f 문제풀이 완료

f5cd615 버그 수정

b6f9dc3 헤더 추가

Page 79: Basic git-commands
Page 80: Basic git-commands

• README.md 파일 추가 후 원격저장소로 push • commit message : “README 추가”

1 Compress.js 2 —- 3 4 같은 문자가 연속적으로 반복되는 경우에 그 반복 횟수를 표시하여 문자열 압축하기

Page 81: Basic git-commands

/git-study

.git├─

compress.js└─

README.md└─

$> git add READMD.md $> git commit -m “READMD 추가” [master 650b6d7] READMD.md 추가 1 file changed, 4 insertions(+) create mode 100644 README.md $> git push origin master Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 416 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:insanehong/git-study.git b6f9dc3..650b6d7 master -> master

5525f5f 문제풀이 완료

f5cd615 버그 수정

b6f9dc3 헤더 추가

650b6d7 READMD 추가

Page 82: Basic git-commands

Tips

Page 83: Basic git-commands

• refs : object의 SHA-1 값을 저장한 파일들, ./git/refs 하위 모든 파일

• branch : 어떤 작업들 중 마지막 작업을 가리키는 레퍼런스

• branch name : branch 의 last commit 에 대한 alias (master)

• HEAD : 현재 checkout 되어 있는 커밋을 가리키는 레퍼런스

• tag : 특정 commit 을 가리키는 object

Page 84: Basic git-commands

5525f5f 문제풀이 완료

f5cd615 버그 수정

b6f9dc3 헤더 추가

master branch

master HEAD650b6d7 READMD 추가

Page 85: Basic git-commands

git reset “This form resets the current branch head to <commit>

and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>. If

<mode> is omitted, defaults to "--mixed". The <mode> must be one of the following:”

- https://www.kernel.org/pub/software/scm/git/docs/git-reset.html -

Page 86: Basic git-commands

• Working tree 를 유지하며 되돌리기 $> git reset <commit>, --mixed

• Working tree, index 를 유지하며 되돌리기 $> git reset --soft <commit>

• 모두 되돌리기 $> git reset --hard <commit>

Page 87: Basic git-commands

$> git reset b6f9dc3

$> git status -s ?? README.md

5525f5f 문제풀이 완료

f5cd615 버그 수정

b6f9dc3 헤더 추가 master HEAD

650b6d7 READMD

Page 88: Basic git-commands

$> git reset --soft f5cd615 $> git status -s M compress.js ?? README.md

5525f5f 문제풀이 완료

f5cd615 버그 수정

b6f9dc3

master HEAD

650b6d7 READMD

Page 89: Basic git-commands

$> git reset --hard 650b6d7 HEAD is now at 650b6d7 READMD 추가 $> git status On branch master nothing to commit, working directory clean

5525f5f 문제풀이 완료

f5cd615 버그 수정

b6f9dc3 헤더 추가

master HEAD650b6d7 READMD 추가

Page 90: Basic git-commands

git clone “Clones a repository into a newly created directory,

creates remote-tracking branches for each branch in the cloned repository, and creates and checks out an initial

branch that is forked from the cloned repository’s currently active branch.”

- https://www.kernel.org/pub/software/scm/git/docs/git-clone.html -

Page 91: Basic git-commands

“다른 프로젝트에 참여하거나(Contribute) Git 저장소를 복사하고 싶을 때 사용.”

Page 92: Basic git-commands

• remote 받아오기 $> git clone <url>

• 디렉토리를 생성하면서 remote 받아오기 $> git clone <url> <directory>

• remote 의 특정 branch 를 지정$> git clone <url> -b <branch>

Page 93: Basic git-commands

/git-study

.git├─

compress.js└─

README.md└─Github

https://github.com/insanehong/git-study.git

Page 94: Basic git-commands

/workspace/

$> git clone [email protected]:insanehong/git-study.git git-study-2 Cloning into 'git-study-2'... remote: Counting objects: 12, done. remote: Compressing objects: 100% (7/7), done. remote: Total 12 (delta 2), reused 12 (delta 2) Receiving objects: 100% (12/12), done. Resolving deltas: 100% (2/2), done. Checking connectivity... done.

$> cd git-study-2

git-study└─

Page 95: Basic git-commands

/git-study-2

.git├─

compress.js└─

README.md└─

/workspace/

git-study

└─

├─

Page 96: Basic git-commands

• git stash• git blame• git checkout• git bisect• git ls-files• git fetch • git merge• git rebase• git pull• git rm• git mv

Page 97: Basic git-commands

http://www.flickr.com/photos/adulau/8442476626/

Page 98: Basic git-commands

insanehong.talk.end(“Thanks. Bye!!”)

Page 99: Basic git-commands

Git is a just tool!

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

• https://www.kernel.org/pub/software/scm/git/

• Thanks to Yi Eung Jun at NAVER LABS (http://npcode.com)

• 본자료에 사용된 commit internal 부분은 Yi Eung Jun 님의 git basic 강의 자료를 참고 했습니다.

Reference

Page 100: Basic git-commands

Git is a just tool!

본 자료는 14.12.20 KGUG(Korea Git User Group) 주최 대학생 대상 Git 교육인

“Getting Started with git” 에서 발표된 “Git Basic Commands” 의 발표내용을 담고 있습니다.

본 자료는 크리에이티브 커먼즈 저작자표시-비영리-변경금지(CC BY-NC-ND) 3.0 Unported 라이선스에 따라 이용할 수 있습니다.

본 자료에 사용 된 이미지들은 Creative Common License 를 따르며 이미지 출처는 해당 이미지 하단에 기제 되어 있습니다.

twitter : @insanehongemail : [email protected]