Skip to content

Instantly share code, notes, and snippets.

@sigridjineth
Last active April 2, 2020 04:36
Show Gist options
  • Save sigridjineth/47a103bff6c27424d2ddec7c94fb10da to your computer and use it in GitHub Desktop.
Save sigridjineth/47a103bff6c27424d2ddec7c94fb10da to your computer and use it in GitHub Desktop.
Git Tip: 특정 브랜치에서 다른 브랜치로 파일만 이동시키고 싶은 경우

Git Tip: 특정 브랜치에서 다른 브랜치로 파일만 이동시키고 싶은 경우

Git tip: git-checkout specific files from another branch

TL;DR

# 파일이 도착하게 될 브랜치에서 작업한다.
git checkout <파일이 지금 있는 브랜치 이름> -- <파일이 지금 있는 브랜치에서의 경로>

Remarks

  • 파일의 도착 지점이 되는 브랜치에서 git checkout 명령어를 실행해서, 파일이 현재 위치하는 브랜치 이름을 명시하고 그 브랜치에서 파일이 존재하는 경로를 명시한다.
  • git checkout 명령어는 브랜치 전환 뿐만 아니라, 현재 작업하고 있는 working tree를 다른 브랜치에서 파일을 가져와 병합하는 데 사용할 수 있다.
  • 해당 명령어는 git workflow를 통해 여러 브랜치에서 기능 피쳐별로 작업하고 있는 경우에 유용하다. 또는 정적 Github Pages 구축에도 쓴다고 한다.
  • git checkout 관련 공식 문서에서는 꼭 브랜치 전환에만 checkout 명령어를 쓰는 것이 아님을 명시하고 있다.

When or --patch are given, git checkout does not switch branches. It updates the named paths in the working tree from the index file or from a named (most often a commit). The argument can be used to specify a specific tree-ish (i.e. commit, tag or tree) to update the index for the given paths before updating the working tree.

  • 예시를 들어보자. 미세먼지 어플리케이션의 어떤 기능 브랜치 A에서 작업되고 있는 파일이 있었는데, 또 다른 기능 브랜치 B에서도 별도로 작업되고 있다. 작업을 하다보니 브랜치 B에서 브랜치 A의 파일을 가져오고 싶은데 머지까지 하기에는 너무 부담스럽다. 그러면 다음과 같이 명령어를 실행해보면 된다.
# On branch B
git checkout B // B 브랜치로 파일을 가져오고 싶으니까 B로 체크아웃하여 브랜치 변경을 수행한다.
git checkout A -- myplugin.js // A 브랜치에서 myplugin.js를 가져온다.
git commit -m "Update myplugin.js from master" // B 브랜치에서 A 브랜치로 파일을 가져왔다.
  • 유의할 점은 복수의 파일을 가져오고 싶은 경우에 파일 하나를 가져오는 명령어를 실행했으면 꼭 커밋하고 다른 파일을 가져와야 한다는 것이다. 왜 그런지는 잘 모르겠다. <- 확인이 필요하다.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment