GIT

내가 자주쓰는 GIT 명령어

sendkite 2022. 10. 9. 12:48

썸네일

 

- 0222 : reset과 revert 차이와 reset에 대한 옵션 기록 추가

기본 명령어 (깃 add, push, merge, reset, branch)

// 깃 상태 확인
git status 

// 파일의 바뀐 부분 확인
git diff {/경로/파일이름}

// 커밋
git add {/경로/파일이름}
git add .
git commit -m "커밋내용"

// 설정
git config —local user.name "커밋하는 사람"
git config —local user.email "커밋맨 이메일"

// 브랜치 만들기, 확인 
git checkout -b newbranch
git branch

git push —set-upstream origin newbranch (—set-upstream `==` u)
git checkout main
git merge newbranch (Fast-forward)
git log (해시코드 확인)
git reset —hard {해시코드}
git merge —no-ff —log newbranch (merge commit - branch가 분기됨)

PULL 할때 리베이스 기본 설정

git config --global pull.rebase true
// 설정 없이 pull은 아래와 같다.
git pull --rebase origin 브랜치

리베이스 사용하기

git checkout -b songyeonbranch
git status
git add .
git commit -m “commit pac”
git checkout dev
git merge —no-ff —log songyeonbranch

// {그 사이 동료가 dev에 커밋이 생겨서 충돌!} -> 충돌해결

git status
git add .
git commit -m “dd”
git reset —hard origin/dev
git log
git checkout songyeonbranch
git rebase dev

// 충돌 수정

git add .
git rebase --continue
git checkout dev
git merge -no-ff -log songyeonbranch

깃 스테시 (git stash) 사용하기

  • 작업 임시 저장 (add, commit 안하고 잠시 저장하고 다른 브랜치 checkout 가능)
git stash
git checkout branch2
git checkout orignbranch
git stash list // 다시 돌아와서 임시저장 내역 확인
git stash apply {해시코드} // 임시저장소 리스트에 돌아가고 싶은 시점으로 적용

깃 되돌리기 (reset vs revert)

git reset으로 취소

  • git reset은 되돌아가고 history에서 지우는 것
git log # 되돌릴 해쉬 확인
# 되돌릴 hash로 돌아가기 working directory까지 삭제
git reset --hard {되돌릴 시점 해쉬}
# Working directory는 남기고 되돌림 (Default)
git reset --mixed
# Staging 상태로 되돌리기
git reset --soft
# 직전 시점으로 돌아가기
git reset --hard
git reset HEAD^

git revert 로 취소

  • git revert는 history는 남기고 되돌아가는 것
git log # 돌아갈 해쉬 확인
# 되돌릴 시점으로 revert하면 커밋 메시지 수정창이 뜬다. 저장하면 새로운 커밋이 뜬다.
git revert {돌아갈 시점 해쉬}

GIT 직전 커밋 취소

git reset HEAD^

GIT 커밋 메시지 수정

가장 최근 단건 수정

git commit --amend

다건 수정 with Rebase

최근 커밋에서 3개 소환

git rebase -i HEAD~3

그럼 아래 같이 창이 뜸

pick {해시코드} {내 커밋 메시지}
pick {해시코드} {내 커밋 메시지}
pick {해시코드} {내 커밋 메시지}

pick을 reword로 바꾸고 esc → :wq! 하면 순서대로 커밋메시지를 수정할 수 있는 창이 뜬다. (wow..)

git status에 필요없는 파일 무시 설정

추적하지 않는 파일이 생기는 경우, git add . 를 쓸 수가 없다. 명령어로 status에서 제거 할 수 있다.

git status
git clean -d -f {경로}

깃 스쿼시 머지

  • ticket 단위로 커밋 합친다. (스쿼시를 하지 않으면, 리뷰어 입장에서 커밋 하나하나 눌러보게 된다.)
  • 최상위 커밋은 pick 나머지는 squash로 입력 (s로 생략 가능)
git rebase -i HEAD~3

pick 해쉬 ... 
squash 해쉬 .. 
squash 해쉬 .. 
  • 참조

https://learngitbranching.js.org/?locale=ko

https://techblog.woowahan.com/2553/

https://www.lainyzine.com/ko/article/git-clean-removing-untracked-files-in-git-repository/

https://velog.io/@mayinjanuary/git-커밋-메세지-수정하기-changing-commit-message

반응형