git 튜토리얼


git 튜토리얼은 git 명령어를 시각 자료를 사용해 이해할 수 있도록 만든 웹페이지 기반 튜토리얼입니다.

add, stash는 본 튜토리얼에서 다루지 않습니다. 명령어를 직접 입력해 볼 수 있는 샌드박스에서는 버전 관리하고자 하는 파일이 모두 staging 영역에 올라와 있다고 가정하고 진행됩니다. add나 staging 영역에 관한 개념을 알고 싶다면 링크를 참고하시기 바랍니다.

학습할 수 있는 명령어는 다음과 같습니다.

기본 명령어

git commit git branch

commit 되돌리기

git reset git revert

브랜치 병합하기

git merge git rebase

remote 리포지토리

git fetch git pull

commit 하고자 하는 파일들이 staging 영역에 올라와 있다고 가정합시다. 왼쪽 하단에 git commit 을 입력하고 엔터를 누르면 커밋이 완료됩니다. 원하는 만큼 커밋을 자유롭게 만들어보세요.

git tag name will create a new tag named "name". Creating tags just creates a new tag pointing to the currently checked out commit.

Tags can be deleted using the command git tag -d name (coming soon).

Type git commit and git tag commands to your hearts desire until you understand this concept.

git branch name을 입력하면 'name'이라는 이름을 가진 브랜치가 만들어집니다. 브랜치를 만드는 것은 현재 체크아웃된 커밋을 가르키는 이름표를 만드는 것이라고 생각하시면 됩니다.

git branch -d name을 입력하면 name 브랜치를 삭제할 수 있습니다.

브랜치 개념에 대해 잘 알 수 있을 때까지 git commitgit branch를 자유롭게 입력해보세요.

git checkout은 다양한 상황에서 쓸 수 있는 명령어인데, 주로 브랜치 사이를 왔다 갔다 할 때 사용합니다.
git checkout dev을 입력하면 master 브랜치에서 dev 브랜치로 이동할 수 있죠. 직접 명령어를 입력해 dev 브랜치로 이동한 후, commit을 하나 만들어 어떤 일이 일어나는지 살펴봅시다.

브랜치를 체크아웃 할 수도 있지만, 개별 커밋을 체크아웃하는 것도 가능합니다. 실제로 해봅시다.
새로운 커밋을 몇 개 만든 후 git checkout bb92e0e을 입력해 ID가 bb92e0e인 커밋을 체크아웃하고 무슨 일이 일어나는지 살펴봅시다.

git commit, git branch, git checkout을 여러 번 번갈아 입력하면서 세 명령어가 익숙해질 때까지 자유롭게 실습해보세요.

git checkout -b namegit branch namegit checkout name을 하나로 합친 명령어입니다. name이라는 브랜치가 생성되어이있지 않다는 가정하에, git checkout -b name을 입력하면 name이라는 브랜치가 만들어지면서 동시에 해당 브랜치를 체크아웃하게 됩니다.

git reset을 사용하면 현재 체크아웃한 브랜치의 원하는 과거 커밋으로 HEAD를 이동시키고, 그 이후의 커밋들은 모두 버릴 수 있습니다. 원치 않는 커밋을 되돌릴 때 유용한 명령어 입니다.

reset 명령어는 보통 --soft, --mixed, --hard 플래그(flag) 중 하나를 붙여서 사용합니다. soft와 mixed 플래그는 reset으로 되돌린 커밋에 있는 내용을 가지고 무언가를 다시 하고 싶을 때 주로 사용합니다. 자세한 내용은 링크 에서 확인해 보시기 바랍니다. 두 플래그는 시각적으로 표현할 수 없기 때문에 본 튜토리얼에선 hard 플래그만 사용하겠습니다.

reset은 HEAD^와 함께 사용할 때가 많습니다. HEAD^는 'HEAD 바로 앞의 커밋'을 의미합니다. HEAD^^는 'HEAD 앞의 커밋 두 개'를 의미합니다. ^는 원하는 만큼 붙여서 사용할 수 있습니다.

remote 리포지토리에 이미 푸시했거나 머지된 커밋은 절대 git reset을 사용해서 버리면 안 됩니다. 이렇게 하면 local 리포지토리와 remote 리포지토리의 싱크가 맞지 않기 때문입니다. reset, push 등에 대해 정확히 알기 전까지 reset을 함부로 사용하지 마세요!

이미 push되어서 팀원간에 공유되고 있는 커밋을 되돌릴 땐 git reset을 사용할 수 없습니다. 대신 git revert를 사용해야 합니다.

git revert를 입력하면 되돌리려고 하는 커밋 안에 있는 작업을 삭제해주는 새로운 커밋이 만들어집니다.

git merge를 사용하면 두 개의 부모 커밋을 가지는 새로운 커밋이 하나 만들어집니다. 이 커밋에는 두 브랜치에서 했던 작업의 스냅숏이 포함됩니다.

합치려는 두 개의 브랜치에 분기가 없었다면 'fast-forward' 전략을 사용해 머지합니다.
ff 브랜치를 체크아웃하고 git merge dev를 입력해 직접 확인해봅시다.

git rebase를 사용하면 현재 체크아웃한 브랜치의 커밋들이 '베이스(기준)' 역할을 하는 브랜치 끝으로 '이동'합니다. dev 브랜치를 체크아웃한 후, git rebase master를 입력해 직접 확인해봅시다.

rebase를 사용할 때는 커밋 ID가 변한다는 점에 주의하셔야 합니다.

rebase를 하면 기존 커밋과 동일한 내용을 담은 '새로운 커밋'들이 '이동'하고, 기존 커밋들은 그 자리에 남아있습니다. 따라서 다른 사람에게 공유된 커밋이 들어있는 브랜치를 rebase하면 안됩니다.

git fetch will update all of the "remote tracking branches" in your local repository. Remote tracking branches are tagged in grey.

A git pull is a two step process that first does a git fetch, and then does a git merge of the remote tracking branch associated with your current branch. If you have no current branch, the process will stop after fetching.

If the argument "--rebase" was given by typing git pull --rebase, the second step of pull process will be a rebase instead of a merge. This can be set to the default behavior by configuration by typing: git config branch.BRANCHNAME.rebase true.

A git push will find the commits you have on your local branch that the corresponding branch on the origin server does not have, and send them to the remote repository.

By default, all pushes must cause a fast-forward merge on the remote repository. If there is any divergence between your local branch and the remote branch, your push will be rejected. In this scenario, you need to pull first and then you will be able to push again.

One simple example of the use of git reset is to completely restore your local repository state to that of the origin.
You can do so by typing git reset origin/master.

Note that this won't delete untracked files, you will have to delete those separately with the command git clean -df.

Below is a situation in which you are working in a local branch that is all your own. You want to receive the latest code from the origin server's master branch. To update your local branch, you can do it without having to switch branches!

First do a git fetch, then type git rebase origin/master!

git branch -d is used to delete branches. I have pre-created a bunch of branches for you to delete in the playground below. Have at it.

Do whatever you want in this free playground.