Git Rebase and Merge Guide

1️⃣ Fetch the Latest Changes from the Remote

Bash
git fetch origin

2️⃣ Rebase Your Current Branch onto the Remote Branch

Bash
git rebase origin/main

You might encounter merge conflicts during this process. If a conflict occurs:

Resolve the conflicts in the affected files, then stage the changes using the commands below:

Bash
git add <resolved-file>
git rebase --continue

If the conflicts are too complex, you can abort the rebase and try pulling again:

Bash
git rebase --abort
git pull origin main --rebase

3️⃣ Push the Changes Once the rebase is complete, you can push your commits.

Bash
git push origin main --force-with-lease

🚨 --force-with-lease is a safer alternative to --force. It safely force-pushes your changes only if the remote branch hasn't been unexpectedly updated by someone else.

❗ Want to Merge Instead of Rebase? If you'd rather simply merge the changes instead of rebasing, use the following commands:

Bash
git pull origin main
git push origin main

This will merge the remote changes into your local branch before pushing them to GitHub.

Git 리베이스(Rebase)와 병합(Merge) 가이드

🚀 Git 리베이스(Rebase)와 병합(Merge) 가이드

1️⃣ 원격 저장소의 최신 변경 사항 가져오기

git fetch origin

2️⃣ 현재 브랜치를 원격 브랜치와 리베이스하기

git rebase origin/main

이 과정에서 충돌(conflict)이 발생할 수 있습니다.

만약 충돌이 발생하면:

  • 충돌이 난 파일을 수정한 후
  • 아래 명령어로 변경 사항을 반영하세요.

git add <충돌 해결한 파일>
git rebase --continue
  

충돌이 너무 복잡하면 리베이스를 취소하고 병합 방식으로 변경할 수도 있습니다:


git rebase --abort
git pull origin main --rebase
  

3️⃣ 변경 사항을 푸시하기

리베이스가 끝났다면 이제 푸시합니다.

git push origin main --force-with-lease
🚨 --force-with-lease--force보다 안전한 옵션입니다. 원격 저장소가 예상치 못하게 변경되지 않은 경우에만 강제 푸시를 실행합니다.

❗ 리베이스 대신 병합(Merge)을 원한다면?

리베이스가 아니라 단순히 병합하고 싶다면 다음 명령어를 사용하세요:


git pull origin main
git push origin main
  

이렇게 하면 원격 저장소의 변경 사항을 로컬에 병합한 후 푸시합니다.

댓글

이 블로그의 인기 게시물

How to Open, Set, and Save Environment Variables (Windows, macOS, Linux)

My Experience Publishing a React Native App as an iOS Developer

How to Fix Auto Layout Constraint Conflicts and Spacing Issues (Question, Image, and Options)