Repository
많은 사람들이 레포지토리가 프로젝트 아니야? 라는 오해를 하곤 합니다. 레포지토리는 무엇이고 왜 프로젝트라고 생각할까요? 한 번 자세하게 알아봅시다.
Repository란?
레포지토리는 깃 트리의 시작점입니다. 레포지토리를 생성하면 레포지토리의 url이 생성되며 url을 통해 웹에서 접근할 수 있습니다.
.git
레포지토리를 생성하면 하위 폴더에 .git 폴더가 생성되는 것을 볼 수 있습니다. .git폴더의 내용물을 통해서 레포지토리에 대한 많은 설정을 할 수 있습니다. 예를 들면 코드를 올리기 전(푸시하기 전)에 코드 포멧팅 체크, 테스팅 진행 등의 훅 설정과 같은 것을 할 수 있습니다.
훅이 궁금하다면? https://techblog.woowahan.com/2530/
훅으로 Git에 훅 들어가기 | 우아한형제들 기술블로그
{{item.name}} 들어가며… 안녕하세요. 우아한형제들 CTO실 주문시스템개발팀의 라태웅입니다. 요새 Git은 어느 조직이건 개인이건 많이 사용하고 계신데요, 굉장히 많은 기능이 있죠. 이중 몰라도
techblog.woowahan.com
브랜치란?
브랜치는 한글로 가지 라는 뜻입니다. 레포지토리라는 트리에서 뻗어 나온 가지를 브랜치라고 합니다. 그러면 브랜치를 한 번 만들어 볼까요? vscode로 저번에 생성했던 레포지토리를 열어주세요.
컨트롤 + ~ 단축키로 터미널을 열고 git branch -vv를 입력해주세요 -vv는 더 자세하게 보는 옵션입니다. 커밋 주소와 리모트/브랜치를 같이 볼 수 있어요 리모트는 나중에 배우니 넘어갑시다.
C:\\Users\\order\\Documents\\GitHub\\fork\\test>git branch -vv
* main 3e330ac [origin/main] Initial commit
위와 같이 브랜치 이름을 볼 수 있습니다. 그러면 브랜치를 하나 생성해볼까요?
터미널에서 git checkout -b dev 를 입력해주세요
C:\\Users\\order\\Documents\\GitHub\\fork\\test>git checkout -b dev
Switched to a new branch 'dev'
dev 브랜치로 스위칭 된 것을 볼 수 있습니다. 그러면 다시 git branch -vv 를 입력해볼까요?
C:\\Users\\order\\Documents\\GitHub\\fork\\test>git branch -vv
* dev 3e330ac Initial commit
main 3e330ac [origin/main] Initial commit
dev 브랜치에 위치해 있는 것을 볼 수 있습니다. 브랜치 생성 완료!
그렇다면 브랜치 간에 이동은 어떻게 하는 걸까요? 터미널에 git checkout main 을 입력해주세요.
C:\\Users\\order\\Documents\\GitHub\\fork\\test>git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
우리는 이제 브랜치 생성, 브랜치 이동 모두 할 수 있게 되었습니다!
! 주의사항
- 깃에서 변경 사항이 남아있으면 브랜치간 이동을 할 수 없으니 변경 사항을 꼭 없애고 이동해주세요.
branch 명령어
git branch -h를 입력하면 도움말을 볼 수 있습니다.
-a, --all list both remote-tracking and local branches
-d, --delete delete fully merged branch
-D delete branch (even if not merged)
-m, --move move/rename a branch and its reflog
-M move/rename a branch, even if target exists
-c, --copy copy a branch and its reflog
-C copy a branch, even if target exists
-l, --list list branch names
--show-current show current branch name
--create-reflog create the branch's reflog
--edit-description edit the description for the branch
-f, --force force creation, move/rename, deletion
--merged <commit> print only branches that are merged
--no-merged <commit> print only branches that are not merged
--column[=<style>] list branches in columns
--sort <key> field name to sort on
--points-at <object> print only branches of the object
-i, --ignore-case sorting and filtering are case insensitive
--recurse-submodules recurse through submodules
--format <format> format to use for the output
git branch -m, -d 등 브랜치를 이동, 이름 변경, 삭제 등의 기능을 주로 사용합니다.
!주의
- git branch -m oldname newname 으로 브랜치 이름을 변경할 수 있습니다. 그런데 변경하고 나선 제 로컬에서만 적용됩니다. 리모트의 브랜치 이름을 변경해야 된다면 리모트도 변경도록 꼭push를 해주세요! push가 안되면 git push -f 옵션으로 강제로 올릴 수 있습니다.
branch 명령어 모음
git branch # 새로운 브랜치 생성
git branch -vv # 브랜치 목록 자세하게 보기
git branch -D <branch> # 로컬 브랜치 강제 삭제
git branch --set-upstream-to=<remote>/<branch> <branch> # 로컬과 리모트 브랜치를 연결함
# 로컬 브랜치 이름을 바꿈, git push -f 해야 리모트도 바뀜
git branch -m <old branch> <new branch>
git branch -c <branch> <copy branch> # 브랜치 복사
checkout 명령어
git checkout -h 를 입력하면 도움말을 볼 수 있습니다.
usage: git checkout [<options>] <branch>
or: git checkout [<options>] [<branch>] -- <file>...
-b <branch> create and checkout a new branch
-B <branch> create/reset and checkout a branch
-l create reflog for new branch
--guess second guess 'git checkout <no-such-branch>' (default)
--overlay use overlay mode (default)
-q, --quiet suppress progress reporting
--recurse-submodules[=<checkout>]
control recursive updating of submodules
--progress force progress reporting
-m, --merge perform a 3-way merge with the new branch
--conflict <style> conflict style (merge, diff3, or zdiff3)
-d, --detach detach HEAD at named commit
-t, --track[=(direct|inherit)]
set branch tracking configuration
-f, --force force checkout (throw away local modifications)
--orphan <new-branch>
new unparented branch
--overwrite-ignore update ignored files (default)
--ignore-other-worktrees
do not check if another worktree is holding the given ref
-2, --ours checkout our version for unmerged files
-3, --theirs checkout their version for unmerged files
-p, --patch select hunks interactively
--ignore-skip-worktree-bits
do not limit pathspecs to sparse entries only
--pathspec-from-file <file>
read pathspec from file
--pathspec-file-nul with --pathspec-from-file, pathspec elements are
separated with NUL character
git checkout <branch 이름>으로 많이 사용합니다
checkout 명령어 모음
git checkout <branch> # 로컬 브랜치 이동
git checkout . # 현재 브랜치의 가장 위 커밋(헤드)으로 이동
git checkout -b <branch> # 브랜치 새로 생성하고 이동
git checkout <remote> <branch> # 리모트와 연결된 로컬 브랜치로 이동(깃이 자동으로 찾아줌)
git checkout <remote>/<branch> # 리모트 브랜치로 이동
- git branch <이름>을 하면 브랜치가 생성되지만 브랜치로 이동되지는 않습니다. git checkout -b를 이용하면 브랜치를 생성하면서 이동할 수 있고 이동했단 메세지도 뜹니다!
- git에서 reset등의 키워드로 뒤의 커밋을 확인하다가 맨 위의 커밋으로 돌아가고 싶다면 git checkout . 을 하면 돌아갈 수 있습니다.
'GIT > GIT 배워보기' 카테고리의 다른 글
Config (0) | 2023.01.28 |
---|---|
Stash, Clean (0) | 2023.01.28 |
Add, Reset (0) | 2023.01.28 |
Remote, Changes, Staged Changes (0) | 2023.01.28 |
Git이란? (0) | 2023.01.28 |