[Git] Feature 브랜치에서 master 관리자의 작업파일로 대체하기

2025. 3. 17. 22:38Developers 공간 [Shorts]/Software Basic

728x90
반응형
<분류>
A. 수단
- OS/Platform/Tool : Linux, Kubernetes(k8s), Docker, AWS
- Package Manager : node.js, yarn, brew, 
- Compiler/Transpillar : React, Nvcc, gcc/g++, Babel, Flutter

- Module Bundler  : React, Webpack, Parcel

B. 언어
- C/C++, python, Javacsript, Typescript, Go-Lang, CUDA, Dart, HTML/CSS

C. 라이브러리 및 프레임워크 및 SDK
- OpenCV, OpenCL, FastAPI, PyTorch, Tensorflow, Nsight

 


1. What? (현상)

 

다른 글에서 master 관리자가 MR 요청한 feature 브랜치의 conflict를 해결하는 것을 살펴보면서, 기본적으로 feature 브랜치가 아래와 같은 코드를 활용해 conflict를 해결하는 과정을 살펴보았습니다.

** https://tkayyoo.tistory.com/209

git stash
git pull origin master
git stash pop

 

다시 한번 상황을 복습해 보겠습니다.

아래와 같이 메인 개발자인 A작업자와, 서브 개발자인 B 작업자가 있을 때, 

  • A개발자 : Master를 관리합니다.
  • B개발자 Branch4라는 브랜치를 만들어 master에 MR를 할 예정입니다.
    ** GitLab MR(Merge Request) == Github PR(Pull Request)

아래의 상황이 있습니다.

상황. 두 개발자 모두 작업을 진행했는데, A개발자가 이미 master를 변경해버렸습니다. 

  • Master
    • Local Commit history : A > B > C
    • Remote Commit history : A > B > C
  • Branch4
    • Local Commit history : A > B > D
    • Remote Commit history : A > B

 

이런 상황에서 feature Branch4 입장에서 아래와 같이 master의 변경 정보인 C를 반영해 진행하는 방법으로 살펴보았었는데

  • Branch4
    • Local Commit history : A > B > D > D+C
    • Remote Commit history : A > B > D+C

이번 글에서는, 그냥 Branch4의 Local 파일들 중 일부 파일을 master의 Local 파일들로 대체하는 방법을 살펴보겠습니다.


2. Why? (원인)

  • X

3. How? (해결책)

 

1. checkout을 활용하기

 

내 브랜치의 local C 파일을 master의 local C파일로 바꾸는 방법은 아래와 같습니다. 아래는 내 브랜치로 이동한 뒤의 명령어입니다.

# Change file C to local master's one
git checkout master -- C

# Change all files to local master's one
git checkout master -- *

 

단, master를 pull 해 놓은 상태여야 최신을 반영할 수 있겠죠

 

2. restore활용하기

 

git checkout은 브랜치를 이동할 때도 사용하기 때문에, 최근에는 git restore라는 특정된 명령어를 사용하기도 합니다.

** 기존 브랜치 이동 기능은 git switch로 사용 가능

# Change file C to local master's one
git restore --source=master -- B
git restore -s master -- B

# Change all files to local master's one
git restore --source=master -- *
git restore -s master -- *

 

위 역시, master를 pull 해 놓은 상태여야 최신을 반영할 수 있습니다.

 

3. master pull하기 귀찮으니, remote의 파일들로 대체하고 싶다.

 

위 첨언으로 적은 바와 같이 master를 최신으로 pull한 뒤, 내 브랜치로 돌아와 위와 같이 작업해야하는데, 그냥 최신화된 remote의 master 파일로 바꾸고 싶다면 아래와 같이 하면 됩니다.

# fetch all recent updates
git fetch origin

# # Change file C to remote master's one
git checkout origin/master -- C
git restore --source=origin/master -- C

 

728x90
반응형