basic git-commands
TRANSCRIPT
Basic
Commands
insanehong@KGUG2014. 12. 20
NAVER / NAVER LABS
twitter: @insanehong
email : [email protected]
http://about.me/insanehong
insanehong.talk.start(“Hi! Bro!”)
이시간에 다루지 않을 것들
https://www.flickr.com/photos/miguelpdl/4994396370
이시간에 전달하려고 하는 것들
기본적인 사용법 & 개념 내부 동작 원리
주의! 이 교육은 간접 광고를 포함하고 있지 않습니다.
http://www.dailycal.org/2013/11/21/everybody-macbook/
Why do programmers use or recommend
macs or linux?
http://www.dailycal.org/2013/11/21/everybody-macbook/
주의! 이 교육은 간접 광고를 포함하고 있지 않습니다.
& CLIGUIRecommend
NAVER give up!!e
이 교육은 간접 광고를 절대 포함하고 있지 않습니다.
photo by https://www.flickr.com/photos/slworking/
Try git better, together
우리 모두는 A사 입사를 위해 퀴즈를 풀어 제출해야 합니다.
채용 퀴즈
“같은 문자가 연속적으로 반복되는 경우에 그 반복 횟수를 표시하여 문자열을 압축하기”
제출 방법
• [email protected] 으로 github url 제출
• 첨부파일로는 받지 않습니다.
Great a Git repository
git init
“This command creates an empty git repository”
- https://www.kernel.org/pub/software/scm/git/docs/git-init.html -
• 현재 디렉토리를 git 저장소로 만들 때
$> git init
• 새로운 디렉토리를 만들때
$> git init <directory>
/workspace
$> git init git-study && cd git-study Initialized empty Git repository /workspace/git-study/.git/
/git-study
.git└─
/git-study
.git└─
Repository Structure
Working tree
index (Staging area)
object database
Working area
Cache storage Storage
Working tree
index (Staging area)
object database
git add git commit
여기서 잠깐!
What is commit?
“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) -
“특정 시점에 대한 스냅샷 혹은 상태”
Git 사용한다는 것?
Git 으로 commit 을 다루는것!
Untracked Staged Committed Modified
Tracked
git add git commit edit file
git add
file
Be committed to doing
/git-study
.git├─
compress.js└─
│
http://git.io/Tq59Hw
Working tree
index (Staging area)
object database
git add git commit
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 -
“working directory 에서 변경되거나 추가된 파일들 중에서 커밋에 포함될 파일들을 등록해 놓는 것”
• 특정 파일들만을 지정
$> git add <file1> <file2>
• 변경되거나 추가된 모든 파일을 다룰때
$> git add -a
여기서 잠깐 Two!
변경 파일이 많거나 어떤 파일이 수정 되었는지 모를때는?
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 -
“Woking directory 의 상태를 보여준다. ”
• 자세한 내용을 확인 할때
$> git status
• 파일들의 변경 상태만 확인 할때
$> git status -s
$> 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
/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
Working tree
index (Staging area)
object database
git add git commit
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 -
/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 문제풀이 완료
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
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
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
자 이제 코드를 제출하면 되나요?
$> node compress hello world, hello git > hel2o world, hel2o git helloooooooo > hel2o
제가 한번 실행해 보겠습니다!
https://www.flickr.com/photos/slworking/8739080572
이상한 점을 찾으셨나요?
“helloooooooo”> hel2o
currently
“helloooooooo”> hel2o8
Goals
8 if( str.charAt(i) === prev ) { 9 repeat++; 10 if ( i === str.length -1) result+=repeat; 11 } else {
Solution
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 */
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 -
• working directory 와 index 를 비교 $> git diff
• 커밋 간 차이점 을 비교 $> git diff <commitA>..<commitB>
• index 와 HEAD를 비교 $> git diff --staged
/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 문제풀이 완료
/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 버그 수정
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
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
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
/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 헤더 추가
Commit inspection
git log
“Shows the commit logs.”
- https://www.kernel.org/pub/software/scm/git/docs/git-log.html -
• 현재 저장소의 시간순 커밋 히스토리 $> git log
• diff 를 함께 보기 $> git log —p
• 커밋 메세지 조회 $> git log --grep <검색어>
$> 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 헤더 추가
git show
“Shows one or more objects (blobs, trees, tags and commits)”
- https://www.kernel.org/pub/software/scm/git/docs/git-show.html -
$> 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 헤더 추가
Push to remote repository
bare
None-bare
None-bare
None-bare
Github
Local repo
Local repo
Local repo
git remote
“Manage the set of repositories ("remotes") whose branches you track.”
- https://www.kernel.org/pub/software/scm/git/docs/git-remote.html -
• 원격 저장소 추가 $> git remote add <alias> <url>
• 원격 저장소 alias 변경 $> git remote rename <old> <new>
• 원격 저장소 url 변경 $> git remote set-url <alias> <url>
• 원격 저장소 확인 $> git remote -v
$> 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└─
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 -
$> 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 헤더 추가
• README.md 파일 추가 후 원격저장소로 push • commit message : “README 추가”
1 Compress.js 2 —- 3 4 같은 문자가 연속적으로 반복되는 경우에 그 반복 횟수를 표시하여 문자열 압축하기
/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 추가
Tips
• refs : object의 SHA-1 값을 저장한 파일들, ./git/refs 하위 모든 파일
• branch : 어떤 작업들 중 마지막 작업을 가리키는 레퍼런스
• branch name : branch 의 last commit 에 대한 alias (master)
• HEAD : 현재 checkout 되어 있는 커밋을 가리키는 레퍼런스
• tag : 특정 commit 을 가리키는 object
5525f5f 문제풀이 완료
f5cd615 버그 수정
b6f9dc3 헤더 추가
master branch
master HEAD650b6d7 READMD 추가
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 -
• Working tree 를 유지하며 되돌리기 $> git reset <commit>, --mixed
• Working tree, index 를 유지하며 되돌리기 $> git reset --soft <commit>
• 모두 되돌리기 $> git reset --hard <commit>
$> git reset b6f9dc3
$> git status -s ?? README.md
5525f5f 문제풀이 완료
f5cd615 버그 수정
b6f9dc3 헤더 추가 master HEAD
650b6d7 READMD
$> git reset --soft f5cd615 $> git status -s M compress.js ?? README.md
5525f5f 문제풀이 완료
f5cd615 버그 수정
b6f9dc3
master HEAD
650b6d7 READMD
$> 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 추가
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 -
“다른 프로젝트에 참여하거나(Contribute) Git 저장소를 복사하고 싶을 때 사용.”
• remote 받아오기 $> git clone <url>
• 디렉토리를 생성하면서 remote 받아오기 $> git clone <url> <directory>
• remote 의 특정 branch 를 지정$> git clone <url> -b <branch>
/git-study
.git├─
compress.js└─
│
README.md└─Github
https://github.com/insanehong/git-study.git
/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└─
/git-study-2
.git├─
compress.js└─
│
README.md└─
/workspace/
git-study
└─
├─
• git stash• git blame• git checkout• git bisect• git ls-files• git fetch • git merge• git rebase• git pull• git rm• git mv
http://www.flickr.com/photos/adulau/8442476626/
insanehong.talk.end(“Thanks. Bye!!”)
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
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]