ProgramingTip

한 git 브랜치에서 다른 브랜치로 단일 파일 버전을 비용 지불해야합니까?

bestdevel 2020. 9. 28. 09:37
반응형

한 git 브랜치에서 다른 브랜치로 단일 파일 버전을 비용 지불해야합니까?


완전히 합쳐진 두 개의 브랜치가 있습니다.

그러나 병합이 더 후 하나의 파일이 병합에 의해 엉망이 자동 서식을 수행했습니다. gah 다른 분기에서 새 버전으로 변경하는 것이 더 쉬울 것입니다. 그런 다음 내 지점으로 일련 후 한 줄 변경 사항을 다시 삽입하십시오.

git에서 수행하는 가장 쉬운 방법은 무엇입니까?


파일을 끝낼 지점에서 실행하십시오.

git checkout otherbranch myfile.txt

일반 공식 :

git checkout <commit_hash> <relative_path_to_file_or_dir>
git checkout <remote_name>/<branch_name> <file_or_dir>

일부 메모 (댓글에서) :

  • 커밋 해시를 사용하면 모든 커밋에서 파일을 사용할 수 있습니다.
  • 이 파일 및 디렉토리에서 작동합니다.
  • 파일을 쓰기 쓰기 myfile.txtmydir
  • 와일드 카드는 작동하지 않지만 상대 경로는 작동합니다.
  • 여러 경로를 이용할 수 있습니다.

대안 :

git show commit_id:path/to/file > path/to/file

나는 내 검색 에서이 질문에 끝났습니다. 제 경우에는 다른 브랜치에서 파일의 원래 위치와 다른 현재 작업 디렉토리로 파일을 찾았습니다. 답변 :

git show TREEISH:path/to/file >path/to/local/file

체크 아웃 명령 사용은 어떻습니까?

  git diff --stat "$branch"
  git checkout --merge "$branch" "$file"
  git diff --stat "$branch"

1) 파일 사본이 필요한 지점에 있는지 확인하십시오. 예를 들어 : 나는 마스터에 하위 파일을 원 체크 아웃해야하거나 마스터에 있어야합니다.

git checkout master

2) 이제 서브 브랜치에서 마스터로 원하는 특정 파일 만 체크 아웃하고,

git checkout sub_branch file_path/my_file.ext

여기는 sub_branch해당 파일이있는 위치와 복사해야하는 파일 이름을 의미합니다.


madlep의 대답에 따라 디렉터리 blob을 사용하여 다른 분기에서 한 디렉터리를 복사 할 수도 있습니다.

git checkout other-branch app/**

op의 질문에있는 거기에서 하나의 파일 만 변경이 잘 작동합니다 ^ _ ^


제발 허용 대답에, 첫 번째 옵션이 있습니다 스테이지 (같은 다른 지점에서 전체 파일을 git add ...수행했다), 그리고 당신이 가진 것처럼 보이게 파일을 복사 단지 결과 두 번째 옵션이 있습니다. 편집은 서로 다른 차이점이 있습니다.)

준비하지 않고 다른 분기에서 Git 복사 파일

단계적 변경 (예 git add filename):

$ git checkout directory/somefile.php feature-B

$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   directory/somefile.php

미해결 변경 (스테이지 또는 커밋되지 않음) :

$ git show feature-B:directory/somefile.php > directory/somefile.php

$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   directory/somefile.php

no changes added to commit (use "git add" and/or "git commit -a")

참고 URL : https://stackoverflow.com/questions/307579/how-do-i-copy-a-version-of-a-single-file-from-one-git-branch-to-another

반응형