📝 Git Rebase

[meɪ]·2021년 8월 18일
1

Preface

📌 개발 공부 1개월 차인 to-be 개발자의 자습 블로그🙂ᅠSep 13 ~ 17, 2021

현재 상태
terminal이랑 Git 아직도 적응 중인데 merge는 잊으라고...
Git Rebase는 9월에 배웠지만 관련 명령어를 Git 게시물 다음으로 옮겨둬서 같이 봐야겠다...!

intro

git merge는 잊어라
2차 프로젝트 때는 commit과 merge는 한 번만!!

rebase 하면서 squash를 할 수 있는데 그럼 commit을 하나만 남길 수 있다.
최종적으로는 commit 하나만 남길 거고, 멘토는 commit 하나만 보고 merge 할 거다.
이렇게 되어 있지 않으면 merge하지 않을 거다.

그럼 commit을 하나만 남기는 방법은?

(모르면 질문하기)

  • git flow : branch 관리 전략
    main 사용자한테 보이는 거
    develop
    feature, release, hotfix는 임시
  • branch를 병합하는 두 가지 방법인 rebase와 merge의 차이점을 알아야 함
    • rebase에서 squash 같이 하는 거야. squash 해서 commit을 하나로 관리하는 것 (branch들을 하나로 합쳐줌)

git merge (x) → git rebase (o)
git rebase -i main을 사용해서 중간중간 branch를 합쳐줘야 함
git add . → git rebase continue


body

product release hotfix가...

명령어도 있긴 한데 우리는 안 쓸 예정 (명령어 뭐지)

기존 방식의 단점

commit message의 형식 다름
merge는 합쳤다는 사실일 뿐 commit message가 필요 없는데 이게 보여서 지저분
불필요한 중간 과정이 다 있어서 지저분
입사하면 PR message 보고 파악하는데 위처럼 지저분하면 기록의 의미가 퇴색됨

rebase 하면

불필요한 merge commit이 제거되고, 같은 작업을 진행한 commit끼리 모아서 볼 수 있음

Main : Base Commit
base가 기준인데, 이 base(기준)을 다시 잡아서 rebase라고 하는 거임
정리하면, rebase는 내 commit history를 일렬로 잘 정리
※ 해당 branch의 base commit 확인하는 방법 : git merge-base main feature/something

branch와 branch 사이가 아닌, commit과 commit 사이에 일어나는 게 conflict.
conflict 해결해야 할 일 많으니까 commit 3~4개 생기면 squash해서 합쳐주는 게 상책!

rebase 중 conflict 해결

  1. 새로운 작업을 모두 마치고 push하기 전에는
    1) main branch로 가서 remote main을 pull 받음
    2) feature brnach로 이동
    3) git rebase -i main진행
        (feature branch에서 main으로 rebase 하겠다는 뜻)
        (명령어에서 i는 interactive라서 terminal이랑 git이랑 계속 상호작용하겠다는 뜻. 그래서 pick, commit hash, commit message가 뜰 것. 이때 squash 진행)
        (현재 commit 형태는 사라지고 내용만 살려서 이전 commit에 녹여 붙이겠다는 뜻)

  2. rebase하는 동안 squash 진행할 때에는
    1) 가장 오래된 commit을 pick하고 wq로 빠져나와
    (가장 위에 있는 pick만 살리고, 나머지는 s로 바꿔줄 거임 i 누르고 변경한 후 ESC → :wq)
    (s=squash라서 squash라고 입력해도 됨)
    ex
        pick (hash 1) (commit 1)
        pick (hash 2) (commit 2)
        pick (hash 3) (commit 3)

        pick (hash 1) (commit 1)
        s (hash 2) (commit 2)
        s (hash 3) (commit 3)
    2) 충돌이 일어나지 않는다면 create OO file in feature/OO이라고 나옴
    (최종적으로 이 rebase된 commit 내용을 작성하는 부분임)
    (현재까지 적은 모든 commit message가 나타남)
    3) 불필요한 내용은 제거하고 현재 수정 내역에 대한 commit message를 multiline으로 잘 작성한 후 ESC → :wq!로 저장해 에디터에서 빠져나옴

  3. 'Successfully Rebased!'라고 뜨면
    1) git log로 확인
    2) push

  4. 하나의 pick만 남기고 나머지는 's'로 남긴 후 conflict가 발생한다면
    1) editor에서 충돌 해결하고
    2) git add .
    3) (수정 사항이 없으므로 git commit은 할 필요 없고) git rebase --continue
    4) 해결이 되지 않으면 git rebase --abort로 rebase를 진행하기 전 상황으로 돌아가는 것 가능

  5. push
    1) 최초 push는 잘 되지만, 다시 rebase를 하면 오류 뜸 error: failed to push some regs to ''
    2) force push로 강제로 push해야 함 git push origin feature/OO -f

mkdir git-rebase
cd git rebase
git clone https://github.com/wecode-bootcamp-korea/git-rebase.git (→ hi.md랑 README.md 생김)
cd git-rebase
git branch feature/haengun 
git checkout feature/haengun
touch haengun.md
git add .
git commit -m "commit 1"
git add .
git commit -m "commit 2"
git add .
git commit -m "commit 3"
git log
git rebase -i main
------
ᅠᅠpick 61e05e2 commit 1
ᅠᅠpick dc5b26c commit 2
ᅠᅠpick 4fda302 commit 3
ᅠᅠ↓
ᅠᅠpick 61e05e2 commit 1
ᅠᅠs dc5b26c commit 2
ᅠᅠs 4fda302 commit 3
------
ᅠᅠcommit 1
ᅠᅠcommit 2
ᅠᅠcommit 3
ᅠᅠ↓
ᅠᅠcommit 1 ~ commit 3
------
ᅠᅠSuccessfully rebased and updated refs/heads/feature/haengun.
------
git log
(참고) git commit --amend (하면 commit 수정 가능!)
git checkout main
git pull origin main
git checkout feature/haengun
git rebase -i main
------
ᅠᅠSuccessfully rebased and updated refs/heads/feature/haengun.
------
git push origin feature/haengun
(참고) git rebase --abort (rebase 중일 땐 이 명령어를 입력하면 git rebase -i main 하기 전으로 돌아감)
(참고) git reflog를 입력해서 돌아가고 싶은 commit ('rebase start 이전 중 원하는 곳)을 복사하고 git rebase --hard NNNNN (rebase 끝난 후 이 명령어를 입력하면 원하는 commit으로 돌아감)
(참고) git log는 현재 commit만 보여주고, git reflog는 이미 없어져버린 commit도 볼 수 있음
git add .
git commit -m "변경 사항 반영"
git rebase -i main
------
ᅠᅠpick e906338 commit 1 ~ commit 3
ᅠᅠpick dab0a53 변경 사항 반영
ᅠᅠ↓
ᅠᅠpick e906338 commit 1 ~ commit 3
ᅠᅠs dab0a53 변경 사항 반영
------
ᅠᅠcommit 1 ~ commit 3
ᅠᅠ변경 사항 반영
ᅠᅠ↓
ᅠᅠcommit 1 ~ commit 3
ᅠᅠ종택님 요청 사항 반영
------
git push origin feature/haengun
------
ᅠᅠerror: failed to push some refs to 'https://github.com/wecode-bootcamp-korea/git-rebase.git'
------
git push origin feature/haengun -f
------
ᅠᅠ+ e906338...4706001 feature/haengun -> feature/haengun (forced update)
------
(참고) git rebase --continue 언제 쓰는 건지 메모 필요



Endnote

🙇 the source of this content

WeCode

profile
느려도 할 거야.......

0개의 댓글