Skip to content

Instantly share code, notes, and snippets.

@bynaki
Last active June 30, 2022 15:16
Show Gist options
  • Save bynaki/55509dc0bdddc365190c to your computer and use it in GitHub Desktop.
Save bynaki/55509dc0bdddc365190c to your computer and use it in GitHub Desktop.
Git :: 튜토리얼 - 용어 정리 - SSH Key 설정 - ssh키 생성: - 공개키 확인: - GitH

Git :: 튜토리얼

용어 정리

  • repository: commit 되어 저장된 저장소
  • stage: add 되어 commit 하기 위한 준비 상태
  • working tree: 현재 보여지는 파일들의 상태

SSH Key 설정

ssh키 생성:
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# Creates a new ssh key, using the provided email as a label
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

id_rsa이 비밀 키이고, id_rsa.pub이 공개 키이다.

공개키 확인:
$ cat ~/.ssh/id_rsa.pub
GitHub에 공개키 등록:
라이선스(license) 고르기:

Global 설정

$ git config --global user.name "My Name"
$ git config --global user.email "my@email.com"
$ git config --global color.ui "auto"
$ git config --global core.editor "subl --wait"

기본 사용

git init: 리포지토리 초기화
$ git init
Initialized empty Git repository in /Users/naki/Projects/GitHub/test-git/.git/
git status: 리포지토리 상태 확인
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)

$ touch README.md
$ git status
On branch master
Initial commit
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	README.md
nothing added to commit but untracked files present (use "git add" to track)
git add: 스테이지 영역에 파일 추가
$ git add README.md
$ git status
On branch master
Initial commit
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   README.md

모든 untracked 파일들을 스테이지 영역에.

$ git add .
git commit: 리포지토리 변경 내용 기록

한줄의 commit 메시지 기록:

$ git commit -m "First commit"
[master (root-commit) 7807920] First commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

상세한 commit 메시지 기록:

$ git commit

설정한 에디터가 실행된다. 아무것도 적지 않으면 commit 이 취소된다.

editor:

First commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
#	new file:   README.md
#
  • 첫 번째 줄: commit으로 인한 변경 내용을 한 줄로 요약해서 작성
  • 두 번째 줄: 공백
  • 세 번째 줄 이후: 변경과 관련된 내용을 상세하게 기록
git log: commit 확인
$ git log
commit 6d69b96ce86ee94d8e7f188298c32972f7025558
Author: bynaki <bynaki@icloud.com>
Date:   Fri Nov 27 00:27:52 2015 +0900

    First commit

commit 메시지의 첫 번째 줄만 출력하는 방법:

$ git log --pretty=short
commit 6d69b96ce86ee94d8e7f188298c32972f7025558
Author: bynaki <bynaki@icloud.com>

    First commit

선택한 폴더 또는 파일의 로그를 출력하는 방법:

$ git log README.md
git diff: 변경 내역 확인

git diff 명령어는 working tree, stage 영역, 최신 commit 사이의 변경을 확인할 때 사용.

working tree와 stage 영역의 차이를 확인하는 방법:

$ git diff
diff --git a/README.md b/README.md
index e69de29..152bf18 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+# Git 튜토리얼
\ No newline at end of file

working tree와 최신 commit 과의 차이를 확인하는 방법:

$ git diff HEAD
diff --git a/README.md b/README.md
index e69de29..152bf18 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+# Git 튜토리얼
\ No newline at end of file

Branch

git branch: 브랜치를 보는 방법
$ git branch
* master
git checkout -b: 브랜치를 만들고 변경하는 방법

feature-A 브랜치로 변경하는 commit 수행:

$ git checkout -b feature-A
Switched to a new branch 'feature-A'

or

$ git branch feature-A
$ git checkout feature-A

확인하자:

$ git branch
* feature-A
  master

한 단계 전의 브랜치로 돌아가는 방법:

$ git checkout -
Switched to branch 'master'
git merge: 브랜치 merge
$ git checkout master
Switched to branch 'master'
$ git merge --no-ff feature-A

브랜치로부터 merge하는 것을 기록으로 명확히 남기려면 --no-ff 옵션을 주어 merge 한다. 이렇게 입력하면 merge commit 메시지 작성을 위해 에디터가 실행된다.

Merge branch 'feature-A'

# 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.

commit 을 변경하는 조작

git reset: 과거 상태로 복원

feature-A 브랜치를 분리하기 전의 상태로 복원:

$ git reset --hard 7c14aeb433dffb980dc529f204fdd657bbaeb99d
HEAD is now at 7c14aeb Add index

repository의 HEAD, stage, 현재의 working tree 모두 지정한 상태까지 복원하려면 git reset --hard 명령어를 사용한다. --hard 옵션은 working tree 까지 변경한다.

$ git reset HEAD

최신 commit 상태로 복원한다. HEAD는 최신 commit 이므로 결과적으로 지워지는 commit은 없다. stage된 파일들이 있다면 stage가 해제된다. 그러나 working tree 는 영향을 미치지 않기 때문에 복원 이후 변경된 내용이 있어도 그대로 유지된다.

$ git reset --hard HEAD

working tree 도 최신 commit 상태로 복원된다.

fix-B 브랜치 작성:

$ git checkout -b fix-B
Switched to a new branch 'fix-B'

..README.md 변경..

$ git add README.md
$ git commit -m "Fix B"
[fix-B 44ca38c] Fix B
 1 file changed, 3 insertions(+), 1 deletion(-)
git reflog: 지워진 commit 내용들도 볼수 있다.

feature-A 브랜치를 merge한 이후의 상태로 복원:

$ git reflog
44ca38c HEAD@{0}: commit: Fix B
7c14aeb HEAD@{1}: checkout: moving from master to fix-B
7c14aeb HEAD@{2}: reset: moving to 7c14aeb433dffb980dc529f204fdd657bbaeb99d
4cd3d13 HEAD@{3}: merge feature-A: Merge made by the 'recursive' strategy.
7c14aeb HEAD@{4}: checkout: moving from feature-A to master
9032bbc HEAD@{5}: checkout: moving from master to feature-A
7c14aeb HEAD@{6}: checkout: moving from master to master
7c14aeb HEAD@{7}: checkout: moving from master to master
7c14aeb HEAD@{8}: checkout: moving from feature-A to master
9032bbc HEAD@{9}: commit: Add feature-A
7c14aeb HEAD@{10}: checkout: moving from master to feature-A
7c14aeb HEAD@{11}: checkout: moving from feature-A to master
7c14aeb HEAD@{12}: checkout: moving from master to feature-A
7c14aeb HEAD@{13}: commit: Add index
6d69b96 HEAD@{14}: commit (initial): First commit

진행된 작업 로그를 확인한다.

$ git checkout master
Switched to branch 'master'
$ git reset --hard 4cd3d13
HEAD is now at 4cd3d13 Merge branch 'feature-A'

4cd3d13 commit으로 복원한다.

충돌 문제 해결

fix-B 브랜치를 merge 하면 충돌한다:

$ git merge --no-ff fix-B
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

README.md 가 다음과 같이 되어있을 것이다:

# Git 튜토리얼

<<<<<<< HEAD
- feature-A
=======
- fix-B
>>>>>>> fix-B

======= 윗 부분이 현재 HEAD의 내용이고, 아래 부분이 merge하려는 fix-B의 내용이다.

수정한다:

# Git 튜토리얼

- feature-A
- fix-B

문제를 해결하고 commit:

$ git add README.md
$ git commit -m "Fix conflict"
[master b2be38c] Fix conflict
git commit --amend: commit 메시지 수정

직전에 작성했던 commit 메시지를 수정한다.

$ git commit --amend

editor:

Merge branch 'fix-B'

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Nov 27 04:04:34 2015 +0900
#
# On branch master
# Changes to be committed:
#	modified:   README.md
#

result:

[master 535a8f7] Merge branch 'fix-B'
git rebase -i: 변경 내역 조작 (이미 commit한 변경을 수정하여 뭉개기)

토픽 브랜치를 merge하기 전, 이미 commit된 내용에 철자 오류 등이 있을 수 있다. 이런 경우에는 코드를 수정하고 commit한 뒤, 바로 앞의 commit에 합쳐버린다. 철자 오류 같은 경우는 그리 중요한 병경 사항이 아니므로 뭉개 버리는 것이다.

feature-C 브랜치 생성:

$ git checkout -b feature-C
Switched to a new branch 'feature-C'

아래는 일부러 철자 오류를 냈다.

README.md:

# Git 튜토리얼

- feature-A
- fix-B
- featura-C

commit:

$ git commit -am "Add feature-C"
[feature-C 12a34c8] Add feature-C
 1 file changed, 2 insertions(+), 1 deletion(-)

오타 수정:

# Git 튜토리얼

- feature-A
- fix-B
- feature-C

commit:

$ git commit -am "Fix typo"
[feature-C ee5aa3f] Fix typo
 1 file changed, 1 insertion(+), 1 deletion(-)

변경 내역 조작:

$ git rebase -i HEAD~2

현재 브랜치의 HEAD를 포함한 두 개의 변경 내역과 관련된 내용이 에디터에 표시된다:

pick 12a34c8 Add feature-C
pick ee5aa3f Fix typo

# Rebase 535a8f7..ee5aa3f onto 535a8f7 (2 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# 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 12a34c8 Add feature-C
fixup ee5aa3f Fix typo

rebase가 성공하면 다음 메시지가 표시된다:

[detached HEAD 887925c] Add feature-C
 Date: Fri Nov 27 06:08:29 2015 +0900
 1 file changed, 2 insertions(+), 1 deletion(-)
Successfully rebased and updated refs/heads/feature-C.

log를 확인하자:

$ git log
commit 887925c840d9bb663298944539174d7458fbe172
Author: bynaki <bynaki@icloud.com>
Date:   Fri Nov 27 06:08:29 2015 +0900

    Add feature-C

commit 535a8f7a3b3fed5bf702245ffa9282d3bba78cb8
Merge: 4cd3d13 44ca38c
Author: bynaki <bynaki@icloud.com>
Date:   Fri Nov 27 04:04:34 2015 +0900

    Merge branch 'fix-B'

Remote Repository :: 원격 리포지토리 송신

먼저 GitHub에 빈 리포지토리를 만든다.

git remote add: 원격 리포지토리 등록

GitHub에 작성된 리포지토리의 경로는 git@github.com:id/test-git.git 이 된다.

로컬 리포지토리의 원격 리포지토리로 등록:

$ git remote add origin git@github.com:bynaki/test-git.git
git push: 원격 리포지토리 전송

master 브랜치에서 전송:

$ git push -u origin master
Counting objects: 16, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (16/16), 1.25 KiB | 0 bytes/s, done.
Total 16 (delta 1), reused 0 (delta 0)
To git@github.com:bynaki/test-git.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

-u 옵션은 --set-upstream 과 동일한 뜻이며 현재 브랜치에 대해 매번 remote source와 branch name을 입력해야 하는 부분을 생략해도 동작하도록 만들어 준다. 이 옵션을 사용하면 git pull 명령어를 실행할 때 추가적인 옵션을 입력하지 않아도 로컬 리보지토리의 브랜치를 origin 리포지토리의 master 브랜치에서 받아올 수 있다.

master 브랜치 이외의 브랜치에 전송:

$ git checkout -b feature-D

push 하자:

$ git push -u origin feature-D
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:bynaki/test-git.git
 * [new branch]      feature-D -> feature-D
Branch feature-D set up to track remote branch feature-D from origin.

Remote Repository에서 가져오기

git clone: 원격 리포지토리를 가져오기
$ git clone git@github.com:bynaki/test-git.git
Cloning into 'test-git'...
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 19 (delta 1), reused 19 (delta 1), pack-reused 0
Receiving objects: 100% (19/19), done.
Resolving deltas: 100% (1/1), done.
Checking connectivity... done.
$ cd test-git

git clone 명령어를 실행하면 master 브랜치가 해당 시점의 상태로 복사된다. 그리고 원격 리포지토리를 쉽게 참조 할 수 있도록 origin 브랜치도 자동으로 설정된다.

git branch -a: 로컬과 원격 branch 모두 보기
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-D
  remotes/origin/master
git checkout -b feature-D origin/feature-D: 원격 리포지토리의 해당 브랜치로 체크아웃
$ git checkout -b feature-D origin/feature-D
Branch feature-D set up to track remote branch feature-D from origin.
Switched to a new branch 'feature-D'

로컬 리포지토리의 feature-D 브랜치에 변경 사항을 commit:

$ git commit -am "Add feature-D"
[feature-D 2197fb3] Add feature-D
 1 file changed, 2 insertions(+), 1 deletion(-)

feature-D 브랜치에 push:

$ git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 283 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:bynaki/test-git.git
   887925c..2197fb3  feature-D -> feature-D
git pull: 최신 원격 리포지토리를 가져오기
$ git pull origin feature-D
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:bynaki/test-git
 * branch            feature-D  -> FETCH_HEAD
   887925c..2197fb3  feature-D  -> origin/feature-D
Updating 887925c..2197fb3
Fast-forward
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

빈 Branch 생성 :: How to Create a New and Empty Branch in Git

In many coding projects the code is kept in one repository and the documentation is kept in another.

If the project is a web application with a database backend perhaps a backup of the SQL is kept in yet another repository.

In Git you can have it all in one repository and keep things separated and isolated in branches.

Normally branches share files from the directory, but in Git it is possible to create empty branches.

You can create a new empty branch like this:

$ git checkout --orphan NEWBRANCH

--orphan creates a new branch, but it starts without any commit. After running the above command you are on a new branch "NEWBRANCH", and the first commit you create from this state will start a new history without any ancestry.

The --orphan command keeps the index and the working tree files intact in order to make it convenient for creating a new history whose trees resemble the ones from the original branch.

Since you want to create a new empty branch that has nothing to do with the original branch, you can delete all files in the new working directory:

$ git rm -rf .

Now you can start adding files and commit them and they will live in their own branch. If you take a look at the log, you will see that it is isolated from the original log.

Using the checkout command you can switch back and forth between the different branches like this:

$ git checkout master (back at the master branch)
$ git checkout NEWBRANCH (back at the new isolated branch)

You need to run git version 1.7.2 or higher in order for the --orphan option to be supported.

If you have any comments or corrections feel free to email me.

Git 참고 자료

Tools

{"noteId":"15143e690d5-fc7dc7cb","main":"15143e690d5-fc7dc7cb.md","title":"Git :: 튜토리얼 - 용어 정리 - SSH Key 설정 - ssh키 생성: - 공개키 확인: - GitHub에 공개키 등록: - 라이선스(license) 고르기: - Global 설정 - 기본 사용 - git init: 리포지토리 초기화 - git status: 리포지토리 상태 확인 - git add: 스테이지 영역에 파일 추가 - git commit: 리포지토리 변경 내용 기록 - git log: commit 확인 - git diff: 변경 내역 확인 - Branch - git branch: 브랜치를 보는 방법 - git checkout -b: 브랜치를 만들고 변경하는 방법 - git merge: 브랜치 merge - commit 을 변경하는 조작 - git reset: 과거 상태로 복원 - git reflog: 지워진 commit 내용들도 볼수 있다. - 충돌 문제 해결 - git commit --amend: commit 메시지 수정 - git rebase -i: 변경 내역 조작 (이미 commit한 변경을 수정하여 뭉개기) - Remote Repository :: 원격 리포지토리 송신 - git remote add: 원격 리포지토리 등록 - git push: 원격 리포지토리 전송 - Remote Repository에서 가져오기 - git clone: 원격 리포지토리를 가져오기 - git branch -a: 로컬과 원격 branch 모두 보기 - git checkout -b feature-D origin/feature-D: 원격 리포지토리의 해당 브랜치로 체크아웃 - git pull: 최신 원격 리포지토리를 가져오기 - 빈 Branch 생성 :: How to Create a New and Empty Branch in Git - Git 참고 자료 - Tools"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment