본문 바로가기

개발👩‍💻/git

git: branch와 status관련 명령어 (branch, switch, diff, rm, restore, reset)

728x90

branch & switch

 

$ git branch <branchname>
# 새로운 branch 생성

$ git branch -d <branchname>
# branch delete

$ git branch -v
# branch list + latest commit

$ git branch --merged
# merged branch list

$ git branch --no-merged
# no-merged branch list
# 여기 있는 branch들은 삭제할 수 없다

$ git switch <branchname>
# 기존 branch로 이동

$ git switch -c <branchname>
# branch 생성하고 이동

$ git switch -
# 이전 branch로 이동

$ git branch -vv
# local branch들 목록 + 내용

 

+ 안정된 브랜치일 수록 커밋이 뒤쳐짐

 

브랜치 삭제

$ git push origin --delete <remote branch>
# remote branch를 삭제하고 싶을 때

$ git branch -d <local branch>
# local branch를 삭제하고 싶을 때

 

diff

$ git diff
# working directory와 staged area 비교

$ git diff --staged
# staged area와 latest commit 비교

 

rm

$ git rm <file>
# tracked file(working direcotory)만 삭제 가능
# 원래 local에서 file 삭제하고 나서
# $ git add <file> 
# 명령을 해주어야 한다.

# rm = local file delete + add

$ git rm -f <file>
# modified, staged file 들은 
# -f 로 강제 삭제할 수 있다.

$ git rm -cached <file>
# add해서 staged된 파일들을
# stage area에 있는 부분들을 없애고
# working direcotory만 남겨둠

 

 

restore

$ git restore --staged <file>
# stage area의 파일을 latest commit 스냅샷으로 덮어쓴다
# = unstaged 상태로 돌려놓는다.

$ git restore <>
# working dir의 modified 상태의 파일들을 latest commit 스냅샷으로 덮어쓴다.
# = working dir의 파일을 수정전으로 돌려놓는다.

 

reset

$ git reset --soft HEAD^
# HEAD가 현재 가리키고 있는 branch를 HEAD^ 즉, 부모로 이동

$ git reset --mixed(or none) HEAD^
# index를 HEAD가 가리키는 상태로 만든다 (HEAD 부모로)

# git reset --hard HEAD^
# working dir까지 HEAD^의 상태로 만든다

 

restore vs. reset

restore은 latest commit 스냅샷을 working dir과 (--staged 붙었을 때) stage area에 적용하는 것이다.

그래서 위험 부담이 적고, latest commit 상태로 working dir을 복구하고 싶을 때 사용하거나, add 한 파일들을 취소하고 싶을 때 사용한다.

 

반면 reset은 soft, mixed, hard 모두 branch 자체를 이동하며 원하는 HEAD (log)로 이동한다.

이렇게 branch를 직접 업데이트하고, commit 들을 삭제, 추가하는 일을 하기 때문에 commit history에 직접적 영향을 준다. 그렇기 때문에 상대적으로 더 조심하며 사용해야한다.

반응형