본문 바로가기

TIL/Git & Github

[TIL][Git][CLI] GIT으로 뭘할건데? - 3. 새로운 Branch 에서 커밋하기

이전 포스팅에 이어서

 

the master branch

 

처음 git 저장소를 만들었을 때 뜨는 메세지와 git log 을 참고해서

git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>
Initialized empty Git repository in /Users/로컬사용자/mypractice/.git/
git log
commit 10e5d5b8b2a13560796bc032cb5e5ed9bfdc5a64 (HEAD -> master)
Author:  
Date:   Wed Apr 27 13:58:46 2022 +0900

    add type of 김밥

commit cfe32de762d1384aac61116f27fd79de9725a31a
Author: 
Date:   Wed Apr 27 13:42:42 2022 +0900

    create a food file

기본 브랜치 master에 속해있다는 것을 안다. 또 HEAD가 가장 최근에 커밋에 가있다는 것도 알 수 있다.

 

1. 새로운 커밋만들기

food 파일의 내용을 수정하고 othermenu라는 새로운 파일을 만들었다.

 git status      
On branch master
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:   food.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	othermenu.txt

 

모두 커밋후

commit e469ba4d24e98989152859f27b01c00c71c77a76 (HEAD -> master)
Author:
Date:   Wed Apr 27 14:28:52 2022 +0900

    add 참치/땡초

commit cefe31c2968a77f1267f543041516c41c7d8cc31
Author:
Date:   Wed Apr 27 14:25:18 2022 +0900

    "add othermenu"

commit 10e5d5b8b2a13560796bc032cb5e5ed9bfdc5a64
Author: 
Date:   Wed Apr 27 13:58:46 2022 +0900

    add type of 김밥

commit cfe32de762d1384aac61116f27fd79de9725a31a
Author: 
Date:   Wed Apr 27 13:42:42 2022 +0900

    create a food file

master 브랜치 안에서 위와 같은 커밋들이 생겼다.

 

이제 새로운 branch 를 만들어서 수정을 이어가다 다시 master 돌아오는 것을 해볼차례다.

2.새로운 branch 만들기 : git branch <name>

git branch foodmarket
git branch
  foodmarket
* master

foodmarket이라는 새로운 브랜치를 만들었다.

*표시는 지금 위치는 master 브랜치에 있다는 뜻.

새로운 foodmarket이라는 브랜치로 옮겨가고 싶으면  switch 을 사용하면 된다.

 

3. 다른 branch 로 switch 하기 : git switch <name>

git switch foodmarket
Switched to branch 'foodmarket'

git status
On branch foodmarket
nothing to commit, working tree clean

앞으로 생길 커밋을 switch 한 branch에 저장된다.

 

4.옮겨간 브렌치에서 커밋하기

새로운 브랜치에서 커밋 만들기

# 이전의 화면과 다른점이 보이시나요? 맨 아래 왼쪽 branch의 이름이 master 에서 foodmarket으로 바뀌었습니다.

 git add chinese.txt  
 
git commit -m " 중식추가"
[foodmarket e1b9759]  중식추가
 1 file changed, 3 insertions(+)
 create mode 100644 chinese.txt
 
git log --oneline
e1b9759 (HEAD -> foodmarket)  중식추가
e469ba4 (master) add 참치/땡초
cefe31c "add othermenu"
10e5d5b add type of 김밥
cfe32de create a food fil

git commit -am "add more menu"
[foodmarket 1164c75] add more menu
 1 file changed, 3 insertions(+), 1 deletion(-) 
 
 git log --oneline
1164c75 (HEAD -> foodmarket) add more menu
e1b9759  중식추가
e469ba4 (master) add 참치/땡초
cefe31c "add othermenu"
10e5d5b add type of 김밥
cfe32de create a food file

5. master branch 로 돌아가기 : git switch master

git switch master
Switched to branch 'master'

git log --oneline
e469ba4 (HEAD -> master) add 참치/땡초
cefe31c "add othermenu"
10e5d5b add type of 김밥
cfe32de create a food file

master 에 없던 file이라 저렇게 표시된다.

이전에 master에서 작성했던 커밋만 보인다.

이후 다른 브랜치에서 작성한 파일을 저런식으로 표시된다.