HOWTO · Git
Git에서 브랜치 생성 및 사용
이 튜토리얼에서는 Git 브랜치를 소개합니다. Git 브랜치가 프로젝트를 구성하는 데 어떻게 도움이 되는지 알아보겠습니다.
이 튜토리얼에서는 Git 브랜치를 소개합니다. Git 브랜치가 프로젝트를 구성하는 데 어떻게 도움이 되는지 알아보겠습니다.
우리가 다룰 명령 중 일부는 git branch와 git checkout입니다.
git branch 명령을 사용하여 Git에서 분기 생성, 표시 및 삭제
git branch 명령을 사용하여 분기를 생성, 표시 및 삭제합니다.
이 명령을 사용하여 다른 분기 간에 전환할 수 없습니다.
git 분기는 저장소의 모든 분기를 표시합니다.git branch <branch name>은 저장소에<branch name>새 분기를 만듭니다.git branch -d <branch name>은<branch name>브랜치를 삭제합니다. 이 명령을 실행하기 전에 먼저 변경 사항을 병합하십시오.git branch -D <branch name>은 예외 없이 분기를 삭제합니다. 결정에 대해 긍정적인 경우 이 명령을 사용하십시오.git branch -m <branch name>은 분기의 이름을 바꾸거나 이동합니다.
New_Branch라는 새 분기를 생성해 보겠습니다.
pc@JOHN MINGW64 ~/Git (main)
$ git branch New_Branch
분기가 있는지 확인하십시오.
pc@JOHN MINGW64 ~/Git (main)
$ git branch
New_Branch
* main
위의 출력에서 New_Branch와 main의 두 가지 분기가 있습니다.
이제 New_Branch를 삭제해 보겠습니다.
pc@JOHN MINGW64 ~/Git (main)
$ git branch -d New_Branch
Deleted branch New_Branch (was 78129a6).
변경 사항을 병합 해제하면 오류 메시지가 표시됩니다. git push origin --delete <branch name>을 사용하여 원격 저장소에서 분기를 삭제합니다.
git checkout -b <branch>를 사용하여 Git의 현재 변경 사항으로 새 분기 생성
git checkout 명령은 프로젝트의 분기 간에 전환합니다.
저장소의 기존 분기를 확인하려면 git checkout <branch>를 사용하십시오. 아래는 예시입니다.
$ git branch
Last_Branch
New_Branch
* main
pc@JOHN MINGW64 ~/Git (main)
$ git checkout New_Branch
Switched to branch 'New_Branch'
M .bash_history
M text.txt.txt
M text.txt.txt.bak
위의 코드에는 New_Branch와 Last_Branch라는 두 개의 분기가 있습니다. git checkout New_Branch를 사용하여 main 분기에서 New_Branch로 전환합니다.
git checkout -b <branch>를 사용하여 새 분기로 전환합니다. 예를 들어 보겠습니다.
pc@JOHN MINGW64 ~/Git (New_Branch)
$ git checkout -b Branch1
Switched to a new branch 'Branch1'
새 분기로 전환하면 Git은 현재 분기의 변경 사항을 새 분기에 자동으로 저장합니다. 이것 좀 봐.
pc@JOHN MINGW64 ~/Git (Branch1)
$ git status
On branch Branch1
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: .bash_history
modified: text.txt.txt
modified: text.txt.txt.bak
Untracked files:
(use "git add <file>..." to include in what will be committed)
.bash_history.bak
no changes added to commit (use "git add" and/or "git commit -a")
위의 출력은 Git이 main 분기에서 Branch1으로 변경 사항을 저장했음을 증명합니다.