이번 포스팅에서는 터미널에서 사용할 수 있는 Git 기본 명령어,  Branch 만들기 합치기, 원격 repository에 push 하고 pull 하는 Git 명령어 사용법을 알아보겠습니다. 



  1. Git 기본 사용법


  • git init : repository 초기화


ihongdon-ui-MacBook-Pro:GitHub ihongdon$ mkdir git-test

ihongdon-ui-MacBook-Pro:GitHub ihongdon$ cd git-test

ihongdon-ui-MacBook-Pro:git-test ihongdon$ pwd

/Users/ihongdon/Documents/GitHub/git-test


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git init

Initialized empty Git repository in /Users/ihongdon/Documents/GitHub/git-test/.git/

 




  • git status : repository 상태 확인


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git status

On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

 




  • README.md 파일 생성


ihongdon-ui-MacBook-Pro:git-test ihongdon$ touch READMD.md

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git status

On branch master


No commits yet


Untracked files:

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


READMD.md


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

 




  • git add : stage 영역에 파일을 추가하여 git repository에서 파일을 관리하도록 해줌


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git add READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git status

On branch master


No commits yet


Changes to be committed:

  (use "git rm --cached <file>..." to unstage)


new file:   READMD.md

 




  • git commit : repository에 변경 내용을 기록 ( -m : 한줄의 commit 메시지 기록)


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git commit -m "commit of README.md file"

[master (root-commit) d337268] commit of README.md file

 1 file changed, 2 insertions(+)

 create mode 100644 READMD.md

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git status

On branch master

nothing to commit, working tree clean

 




  • git log : repository에 commite된 로그를 확인


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git log

commit d337268ccec9ffcb3a19d6d1ec86304b791ae093 (HEAD -> master)

Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

Date:   Sun Aug 5 16:51:55 2018 +0900


    commit of README.md file

 




  • git log --pretty=short : commit 메시지의 첫 번째 줄만 출력(여러 개의 commit 파악 용이)


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git log --pretty=short

commit d337268ccec9ffcb3a19d6d1ec86304b791ae093 (HEAD -> master)

Author: xxxxxx_xxx <xxxxxx@xxxxx.com>


    commit of README.md file

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git log filename : 선택한 폴더 혹은 파일의 로그만 출력하기


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git log READMD.md 

commit d337268ccec9ffcb3a19d6d1ec86304b791ae093 (HEAD -> master)

Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

Date:   Sun Aug 5 16:51:55 2018 +0900


    commit of README.md file

i-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git log -p filename : 파일의 변경된 내용을 출력하여 확인하기


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git log -p READMD.md 

commit d337268ccec9ffcb3a19d6d1ec86304b791ae093 (HEAD -> master)

Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

Date:   Sun Aug 5 16:51:55 2018 +0900


    commit of README.md file


diff --git a/READMD.md b/READMD.md

new file mode 100644

index 0000000..c90fe08

--- /dev/null

+++ b/READMD.md

@@ -0,0 +1,2 @@

+This is a git-test repository. 

+I'm writing README.md file as a test.

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git diff : working tree, stage 영역, 최신 commit 사이의 변경 내역을 확인 
    => working tree와 stage 영역의 차이를 확인


git diffihongdon-ui-MacBook-Pro:git-test ihongdon$ git diff

diff --git a/READMD.md b/READMD.md

index c90fe08..2424cd6 100644

--- a/READMD.md

+++ b/READMD.md

@@ -1,2 +1,6 @@

+# Git Tutorial

+

+## version 1

+

 This is a git-test repository. 

 I'm writing README.md file as a test.

ihongdon-ui-MacBook-Pro:git-test ihongdon$

 




  • git add 명령어로 stage 영역에 README.md 파일을 추가한 후 git diff 명령어를 사용하면 working tree와 stage 영역 사이의 차이가 없으므로 아무것도 표시되지 않습니다. 


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git add READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git diff

ihongdon-ui-MacBook-Pro:git-test ihongdon$ # 아무것도 표시되지 않음

 




  • git diff HEAD : working tree에서 최신의 변경 부분을 확인하기 (HEAD는 현재 작업하고 있는 branch의 최신 commit을 참조하는 포인터)


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git diff HEAD

diff --git a/READMD.md b/READMD.md

index c90fe08..2424cd6 100644

--- a/READMD.md

+++ b/READMD.md

@@ -1,2 +1,6 @@

+# Git Tutorial

+

+## version 1

+

 This is a git-test repository. 

 I'm writing README.md file as a test.

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git commit -m : 수정사항 commit


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git commit -m "Add title and version"

[master ea87d54] Add title and version

 1 file changed, 4 insertions(+)

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git log : commit 확인


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git log

commit ea87d546d8649ea000d071727c0b14e870bc714e (HEAD -> master)

Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

Date:   Sun Aug 5 17:30:31 2018 +0900


    Add title and version


commit d337268ccec9ffcb3a19d6d1ec86304b791ae093

Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

Date:   Sun Aug 5 16:51:55 2018 +0900


    commit of README.md file

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  2. Branch 생성하고 합치기


  • git branch : 현재 사용하고 있는 branch 확인 하기


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git branch

* master

 




  • git checkout -b branch_name : 새로운 branch를 만들고, 새로운 branch로 변경하기


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git checkout -b readme-edit-v2

Switched to a new branch 'readme-edit-v2'


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git branch # current branch check

  master

* readme-edit-v2

 





  • 'readme-edit-v2' branch 에서 date 추가하고 commit 하기


ihongdon-ui-MacBook-Pro:git-test ihongdon$ vi READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial


## version 1


### date : as of 5th Aug.


This is a git-test repository. 

I'm writing README.md file as a test.


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git add READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git commit -m "Add date"

[readme-edit-v2 8254c2e] Add date

 1 file changed, 2 insertions(+)

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git log

commit 8254c2e24c83e0e3f8e843228ca84fbba8998e54 (HEAD -> readme-edit-v2)

Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

Date:   Sun Aug 5 19:30:04 2018 +0900


    Add date


commit ea87d546d8649ea000d071727c0b14e870bc714e (master)

Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

Date:   Sun Aug 5 17:30:31 2018 +0900


    Add title and version


commit d337268ccec9ffcb3a19d6d1ec86304b791ae093

Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

Date:   Sun Aug 5 16:51:55 2018 +0900


    commit of README.md file

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git checkout master : master branch로 변경


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git checkout master

Switched to branch 'master'

 




>> 아직 master branch에 'readme-edit-v2' branch의 변경사항이 반영되지 않았음

 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial


## version 1


This is a git-test repository. 

I'm writing README.md file as a test.

ihongdon-ui-MacBook-Pro:git-test ihongdon$





  • git merge --no-f topic_branch_name : Topic branch를 master branch에 통합하기


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git merge --no-ff readme-edit-v2


아래와 같은 에디터가 나타납니다. 


hint: Waiting for your editor to close the file... 

Merge branch 'readme-edit-v2'

  

# Please enter a commit message to explain why this merge is necessary,

# especially if it merges an updated upstream into a topic branch.

#

# Lines starting with '#' will be ignored, and an empty message aborts

# the commit.



에디터를 저장하고 종료(esc 후 :wq!)하면 아래와 같은 메시지가 나옵니다. 


Merge made by the 'recursive' strategy.

 READMD.md | 2 ++

 1 file changed, 2 insertions(+)

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




>> date 통합 확인



ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial


## version 1


### date : as of 5th Aug.


This is a git-test repository. 

I'm writing README.md file as a test.

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git log --graph : branch 분기 또는 통합을 그래프로 표시


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git log --graph

*   commit f36bf439021e2220a56c01e6bf7e20ea897c8598 (HEAD -> master)

|\  Merge: ea87d54 8254c2e

| | Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

| | Date:   Sun Aug 5 19:55:05 2018 +0900

| | 

| |     Merge branch 'readme-edit-v2'

| | 

| * commit 8254c2e24c83e0e3f8e843228ca84fbba8998e54 (readme-edit-v2)

|/  Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

|   Date:   Sun Aug 5 19:30:04 2018 +0900

|   

|       Add date

* commit ea87d546d8649ea000d071727c0b14e870bc714e

| Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

| Date:   Sun Aug 5 17:30:31 2018 +0900

|     Add title and version

* commit d337268ccec9ffcb3a19d6d1ec86304b791ae093

  Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

  Date:   Sun Aug 5 16:51:55 2018 +0900

  

      commit of README.md file

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




######################################################################


  • git reset : 과거 상태로 되돌리기 (다른 위치의 repository에는 영향 없음)   
    --> readme-edit-v2 통합하기 이전 상태로 되돌리기


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git reset --hard ea87d546d8649ea000d071727c0b14e870bc714e

HEAD is now at ea87d54 Add title and version

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git branch

* master

  readme-edit-v2

ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial


## version 1


This is a git-test repository. 

I'm writing README.md file as a test.

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 







  • git checkout -b readme-edit-v3 : readme-edit-v3라는 이름의 새로운 branch 생성


 ihongdon-ui-MacBook-Pro:git-test ihongdon$ git checkout -b readme-edit-v3

Switched to a new branch 'readme-edit-v3'


ihongdon-ui-MacBook-Pro:git-test ihongdon$ vi READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial


## version 3


### new topic branch readme-edit-v3


This is a git-test repository. 

I'm writing README.md file as a test.





  • git reflog : 현재 repository에서 수행된 모든 commit 로그 확인 하기 
    (vs. git log 명령어는 현재 branch의 로그만 확인 가능)


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git reflog

ea87d54 (HEAD -> readme-edit-v3, master) HEAD@{0}: checkout: moving from master to readme-edit-v3

ea87d54 (HEAD -> readme-edit-v3, master) HEAD@{1}: reset: moving to ea87d546d8649ea000d071727c0b14e870bc714e

f36bf43 HEAD@{2}: merge readme-edit-v2: Merge made by the 'recursive' strategy.

ea87d54 (HEAD -> readme-edit-v3, master) HEAD@{3}: checkout: moving from readme-edit-v2 to master

8254c2e (readme-edit-v2) HEAD@{4}: checkout: moving from master to readme-edit-v2

ea87d54 (HEAD -> readme-edit-v3, master) HEAD@{5}: checkout: moving from readme-edit-v2 to master

8254c2e (readme-edit-v2) HEAD@{6}: commit: Add date

ea87d54 (HEAD -> readme-edit-v3, master) HEAD@{7}: checkout: moving from master to readme-edit-v2

ea87d54 (HEAD -> readme-edit-v3, master) HEAD@{8}: commit: Add title and version

d337268 HEAD@{9}: commit (initial): commit of README.md file

 




  • git reset --hard log_hash : 다시 'readme-edit-v2' 분리 하기 이전 상태로 되돌리기 (reset)


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git checkout master

M READMD.md

Switched to branch 'master'

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git reset --hard f36bf43

HEAD is now at f36bf43 Merge branch 'readme-edit-v2'

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 


ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial


## version 1


### date : as of 5th Aug.


This is a git-test repository. 

I'm writing README.md file as a test.

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




>> git commit  



ihongdon-ui-MacBook-Pro:git-test ihongdon$ vi READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial

## version 3

## readme-edit-v3

i

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git add READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git commit -m "readme edit version 3"

[readme-edit-v3 59e4132] readme edit version 3

 1 file changed, 2 insertions(+), 2 deletions(-)

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git reflog : 현재 repository에서 수행된 모든 commit 로그 확인 하기


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git reflog

59e4132 (HEAD -> readme-edit-v3) HEAD@{0}: commit: readme edit version 3

ea87d54 HEAD@{1}: checkout: moving from master to readme-edit-v3

f36bf43 (master) HEAD@{2}: reset: moving to f36bf43

ea87d54 HEAD@{3}: checkout: moving from readme-edit-v3 to master

ea87d54 HEAD@{4}: checkout: moving from master to readme-edit-v3

ea87d54 HEAD@{5}: reset: moving to ea87d546d8649ea000d071727c0b14e870bc714e

f36bf43 (master) HEAD@{6}: merge readme-edit-v2: Merge made by the 'recursive' strategy.

ea87d54 HEAD@{7}: checkout: moving from readme-edit-v2 to master

8254c2e (readme-edit-v2) HEAD@{8}: checkout: moving from master to readme-edit-v2

ea87d54 HEAD@{9}: checkout: moving from readme-edit-v2 to master

8254c2e (readme-edit-v2) HEAD@{10}: commit: Add date

ea87d54 HEAD@{11}: checkout: moving from master to readme-edit-v2

ea87d54 HEAD@{12}: commit: Add title and version

d337268 HEAD@{13}: commit (initial): commit of README.md file

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git commit --amend : 직전에 작성했던 commit 메시지 수정


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git commit --amend

hint: Waiting for your editor to close the file... 

Merge branch 'readme-edit-v3'

  

# Please enter the commit message for your changes. Lines starting

# with '#' will be ignored, and an empty message aborts the commit.

#

# Date:      Sun Aug 5 20:26:01 2018 +0900

#

# On branch master

# Changes to be committed:

#       modified:   READMD.md


[master 7b45ed5] amend commit message by using git commit --amend Merge branch 'readme-edit-v3'

 Date: Sun Aug 5 20:26:01 2018 +0900

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git log graph : branch 분기 또는 통합을 그래프로 표시


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git log --graph

*   commit 7b45ed51381013214cddf19e02e4df38b83587e4 (HEAD -> master)

|\  Merge: f36bf43 59e4132

| | Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

| | Date:   Sun Aug 5 20:26:01 2018 +0900

| | 

| |     amend commit message by using git commit --amend

| |     Merge branch 'readme-edit-v3'

| | 

| * commit 59e4132d3928f580c3a26613b83220af04d13540 (readme-edit-v3)

| | Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

| | Date:   Sun Aug 5 20:23:47 2018 +0900

| | 

| |     readme edit version 3

| |   

* |   commit f36bf439021e2220a56c01e6bf7e20ea897c8598

|\ \  Merge: ea87d54 8254c2e

| |/  Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

|/|   Date:   Sun Aug 5 19:55:05 2018 +0900

| |   

| |       Merge branch 'readme-edit-v2'

| | 

| * commit 8254c2e24c83e0e3f8e843228ca84fbba8998e54 (readme-edit-v2)

|/  Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

|   Date:   Sun Aug 5 19:30:04 2018 +0900

|   

|       Add date

* commit ea87d546d8649ea000d071727c0b14e870bc714e

| Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

| Date:   Sun Aug 5 17:30:31 2018 +0900

|     Add title and version

* commit d337268ccec9ffcb3a19d6d1ec86304b791ae093

  Author: xxxxxx_xxx <xxxxxx@xxxxx.com>

  Date:   Sun Aug 5 16:51:55 2018 +0900

  

      commit of README.md file

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git rebase -i HEAD~2 : 코드를 수정하고 commit 한 뒤에, 바로 앞의 commit에 합치기 
    (간단한 오탈자 수정 시 자주 사용)


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git checkout -b readmd-edit-v4

Switched to a new branch 'readmd-edit-v4'

ihongdon-ui-MacBook-Pro:git-test ihongdon$ vi READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial

## version 3

## readme-edit-v3

## readyou-edit-v4. <- 오탈자


### date : as of 5th Aug.


This is a git-test repository. 

I'm writing README.md file as a test.

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git add READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git commit -m "readme-edit-v4"

[readmd-edit-v4 b2e7b0b] readme-edit-v4

 1 file changed, 1 insertion(+)


ihongdon-ui-MacBook-Pro:git-test ihongdon$ vi READMD.md  # 오탈자 수정함

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git diff

diff --git a/READMD.md b/READMD.md

index 2b9a28f..91bda6a 100644

--- a/READMD.md

+++ b/READMD.md

@@ -1,7 +1,7 @@

 # Git Tutorial

 ## version 3

 ## readme-edit-v3

-## readyou-edit-v4

+## readme-edit-v4

 

 ### date : as of 5th Aug.

 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git add READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git commit -m "Fix typo"

[readmd-edit-v4 71d869f] Fix typo

 1 file changed, 1 insertion(+), 1 deletion(-)

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git rebase -i HEAD~2


pick b2e7b0b readme-edit-v4

pick 71d869f Fix typo


# Rebase 7b45ed5..71d869f onto 7b45ed5 (2 commands)

#

# Commands:

# p, pick <commit> = use commit

# r, reword <commit> = use commit, but edit the commit message

# e, edit <commit> = use commit, but stop for amending

# s, squash <commit> = use commit, but meld into previous commit

# f, fixup <commit> = like "squash", but discard this commit's log message

# x, exec <command> = run command (the rest of the line) using shell

# d, drop <commit> = remove commit

# l, label <label> = label current HEAD with a name

# t, reset <label> = reset HEAD to a label

# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]

# .       create a merge commit using the original merge commit's

# .       message (or the oneline, if no original merge commit was

# .       specified). Use -c <commit> to reword the commit message.

#

# These lines can be re-ordered; they are executed from top to bottom.

#

# If you remove a line here THAT COMMIT WILL BE LOST.

#

#       However, if you remove everything, the rebase will be aborted.

#

#

# Note that empty commits are commented out



>> pick -> fixup 으로 수정하고 코드 편집기의 내용을 저장 후 종료

pick b2e7b0b readme-edit-v4

fixup 71d869f Fix typo


esc 후 :wq!



Successfully rebased and updated refs/heads/readmd-edit-v4.

(b2e7b0b readme-edit-v4 와 71d869f Fix typo 의 두 개의 commit이 하나로 합쳐졌음)



>> master branch 에 통합하기

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git checkout master

Switched to branch 'master'

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git merge --no-ff readmd-edit-v4

Merge made by the 'recursive' strategy.

 READMD.md | 1 +

 1 file changed, 1 insertion(+)

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 





  3. 원격 Repository 에 내보내기 (push)


pull, push, pull request 의 작업 흐름은 아래의 도식을 참고하세요. 





  • git remote add : 원격 repository 등록 
     (origin이라는 식별자가 git@github.com:hdlee4u/git-test.git 을 가리킴)


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git remote add origin git@github.com:hdlee4u/git-test.git

 




  • git push : 로컬 repository의 내용을 원격 repository에 전송


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git push -u origin master

Warning: Permanently added the RSA host key for IP address '192.30.255.113' to the list of known hosts.

Enter passphrase for key '/Users/ihongdon/.ssh/id_rsa': 

Enumerating objects: 20, done.

Counting objects: 100% (20/20), done.

Delta compression using up to 8 threads.

Compressing objects: 100% (14/14), done.

Writing objects: 100% (20/20), 1.86 KiB | 1.86 MiB/s, done.

Total 20 (delta 5), reused 0 (delta 0)

remote: Resolving deltas: 100% (5/5), done.

To github.com:hdlee4u/git-test.git

 * [new branch]      master -> master

Branch 'master' set up to track remote branch 'master' from 'origin'.

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




로컬 repository에서 만들었던 내용들이 git push 로 원격 repository에 보내기를 한 결과, GitHub 페이지에 가보면 아래와 같이 'git-test' repository의 master branch 에 README.md 파일이 올라와 있습니다. 

 




>> 로컬 repository 의 master branch 이외의 'readme-edit-v2' branch를 원격 repositiory에 전송하기



ihongdon-ui-MacBook-Pro:git-test ihongdon$ git checkout readme-edit-v2

Switched to branch 'readme-edit-v2'

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git push -u origin readme-edit-v2

Enter passphrase for key '/Users/ihongdon/.ssh/id_rsa': 

Total 0 (delta 0), reused 0 (delta 0)

To github.com:hdlee4u/git-test.git

 * [new branch]      readme-edit-v2 -> readme-edit-v2

Branch 'readme-edit-v2' set up to track remote branch 'readme-edit-v2' from 'origin'.

ihongdon-ui-MacBook-Pro:git-test ihongdon$

 




  4. 원격 repository 에서 가져오기 (clone)


여기서 부터는 같이 협업을 하는 '제 3의 개발자'가 있다고 가정하고, 저의 repository에 저장되어 있는 코드를 '제 3의 개발자'가 자신의 repository로 가져다가 작업을 한다고 가정을 하고 아래 글들을 읽으시길 바랍니다.  폴더를 새로 만들고, 폴더 사이를 왔다갔다 하는게 '제 3의 개발자'의 개발 환경으로 이동하는 것이라고 여기면 되겠습니다. 


>> 새로운 폴더 만들기



ihongdon-ui-MacBook-Pro:git-test ihongdon$ cd

ihongdon-ui-MacBook-Pro:~ ihongdon$ pwd

/Users/ihongdon

ihongdon-ui-MacBook-Pro:~ ihongdon$ cd Documents

ihongdon-ui-MacBook-Pro:Documents ihongdon$ cd GitHub

ihongdon-ui-MacBook-Pro:GitHub ihongdon$ mkdir git-test2

ihongdon-ui-MacBook-Pro:GitHub ihongdon$ cd git-test2

ihongdon-ui-MacBook-Pro:git-test2 ihongdon$ 

 




  • git clone remote_repository : 원격 repository 복사해서 가져오기


ihongdon-ui-MacBook-Pro:git-test2 ihongdon$ git clone git@github.com:hdlee4u/git-test.git

Cloning into 'git-test'...

Enter passphrase for key '/Users/ihongdon/.ssh/id_rsa': xxxxxxxx

remote: Counting objects: 20, done.

remote: Compressing objects: 100% (9/9), done.

Receiving objects: 100% (20/20), done.

Resolving deltas: 100% (5/5), done.

remote: Total 20 (delta 5), reused 20 (delta 5), pack-reused 0

ihongdon-ui-MacBook-Pro:git-test2 ihongdon$ 

ihongdon-ui-MacBook-Pro:git-test2 ihongdon$ cd git-test

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git branch -a  # -a 를 붙이면 로컬 repository와 원격 repository 정보를 함께 출력함

* master

  remotes/origin/HEAD -> origin/master

  remotes/origin/master

  remotes/origin/readme-edit-v2

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




  • git checkout -b branch_name origin/branch_name : 원격 repository의 origin branch 를 가져다가 로컬 repository에 branch 생성


ihongdon-ui-MacBook-Pro:git-test ihongdon$ git checkout -b readme-edit-v2 origin/readme-edit-v2

Branch 'readme-edit-v2' set up to track remote branch 'readme-edit-v2' from 'origin'.

Switched to a new branch 'readme-edit-v2'

ihongdon-ui-MacBook-Pro:git-test ihongdon$

 




>> readme-edit-v2 branch에 변경사항 commit



ihongdon-ui-MacBook-Pro:git-test ihongdon$ vi READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial


## version 1

## version 2

### date : as of 5th Aug.

### test remote branch


This is a git-test repository. 

I'm writing README.md file as a test. 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git add READMD.md 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git commit -m "test remote repository with readme-edit-v2"

[readme-edit-v2 c147dbb] test remote repository with readme-edit-v2

 1 file changed, 2 insertions(+), 1 deletion(-)

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




>> readme-edit-v2 commit 한 변경사항을 원격 repository에 전송하기 (push)



ihongdon-ui-MacBook-Pro:git-test ihongdon$ git push

Enter passphrase for key '/Users/ihongdon/.ssh/id_rsa': xxxxxxxx

Enumerating objects: 5, done.

Counting objects: 100% (5/5), done.

Delta compression using up to 8 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 399 bytes | 399.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To github.com:hdlee4u/git-test.git

   8254c2e..c147dbb  readme-edit-v2 -> readme-edit-v2

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 




>> 이전 작업 폴더로 돌아가서 방금 전에 수정한 readme-edit-v2 branch 가져오기 (pull)



ihongdon-ui-MacBook-Pro:git-test ihongdon$ cd

ihongdon-ui-MacBook-Pro:~ ihongdon$ cd Documents

ihongdon-ui-MacBook-Pro:Documents ihongdon$ cd GitHub

ihongdon-ui-MacBook-Pro:GitHub ihongdon$ cd git-test

ihongdon-ui-MacBook-Pro:git-test ihongdon$ ls -al

total 8

drwxr-xr-x   4 ihongdon  staff  128  8  5 21:35 .

drwxr-xr-x  10 ihongdon  staff  320  8  5 21:38 ..

drwxr-xr-x  14 ihongdon  staff  448  8  5 21:35 .git

-rw-r--r--   1 ihongdon  staff  127  8  5 21:35 READMD.md

ihongdon-ui-MacBook-Pro:git-test ihongdon$ git pull origin readme-edit-v2

Enter passphrase for key '/Users/ihongdon/.ssh/id_rsa': xxxxxxxx

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (2/2), done.

Unpacking objects: 100% (3/3), done.

remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0

From github.com:hdlee4u/git-test

 * branch            readme-edit-v2 -> FETCH_HEAD

   8254c2e..c147dbb  readme-edit-v2 -> origin/readme-edit-v2

Updating 8254c2e..c147dbb

Fast-forward

 READMD.md | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

ihongdon-ui-MacBook-Pro:git-test ihongdon$ cat READMD.md 

# Git Tutorial


## version 1

## version 2

### date : as of 5th Aug.

### test remote branch


This is a git-test repository. 

I'm writing README.md file as a test.

ihongdon-ui-MacBook-Pro:git-test ihongdon$ 

 



Git, GitHub 을 한번도 사용해보지 않은 분이 이번 포스팅을 보게 되면 '이게 다 무슨 얘기지?' 하고 어리둥절 할 것 같습니다. ^^;;;


아무래도 이번 포스팅을 Git 을 사용하시는 분들 중에서 Git 명령어가 잘 기억이 안날 때 참고할 수 있는 용도로 사용하는게 좋을 것 같습니다. 


많은 도움이 되었기를 바랍니다. 


728x90
반응형
Posted by Rfriend
,