다른 지점에서 하나의 파일만 가져오려면 어떻게 해야 합니까?
I have a 나는 가지고 있다master branch with a file called 파일이라는 파일이 있는 분기app.js. . I made changes to this file on an 이 파일을 변경했습니다.experiment분점.분점.
I want to apply only the changes made to 변경된 내용만 적용하려고 합니다.app.js부에서experiment에 onto themaster★★★★★★ 。
git checkout master # first get back to master
git checkout experiment -- app.js # then copy the version of app.js
# from branch "experiment"
파일 변경을 취소하는 방법을 참조하십시오.
2019년 8월 업데이트 Git 2.23
새로운 명령어와 명령어를 사용하면 다음과 같습니다.
git switch master
git restore --source experiment -- app.js
이치노
인덱스를 업데이트(파일 내용을 복원하고 하나의 명령으로 인덱스에 추가하는 것을 의미)하는 경우:
git restore --source experiment --staged --worktree -- app.js
# shorter:
git restore -s experiment -SW -- app.js
Jakub Narbsbski가 코멘트에서 언급했듯이:
git show experiment:path/to/app.js > path/to/app.js
SO 질문 "How to retrieve an single file from specific revision in Git?"에서 자세히 설명한 것처럼 repo의 루트 디렉토리에서 풀 경로를 사용해야 합니다.
ub이ub이이ububtotototo / to / app . js 。
프로스티가 코멘트에서 언급했듯이:
앱의 최신 상태만 표시됩니다.
데...는...git checkout ★★★★★★★★★★★★★★★★★」git showSO 질문의 "git checkout revision of a file in git gui"에서 설명한 것처럼 실제로 원하는 리비전을 참조할 수 있습니다.
$ git show $REVISION:$FILENAME
$ git checkout $REVISION -- $FILENAME
$FILNAME은 버전화된 파일의 풀 경로입니다.
$REVISION는, 에 나타내는 것과 같습니다.
experiment@{yesterday}:app.js # app.js as it was yesterday
experiment^:app.js # app.js on the first commit parent
experiment@{2}:app.js # app.js two commits ago
기타 등등.
저장소에서 이 작업을 수행할 수도 있습니다.
git checkout stash -- app.js이것은, 2개의 브랜치로 작업하고 있어 커밋 하고 싶지 않은 경우에 매우 편리합니다.
모든 것이 훨씬 더 단순합니다. 그것을 위해 git checkout을 사용하세요.
브랜치를 취득하려면 , 브랜치를 다음과 같이 합니다.
git checkout new-feature path/to/app.js
// note that there is no leading slash in the path!
그러면 원하는 파일의 내용이 나타납니다.항상 그렇듯이 새 기능의 브랜치 이름 대신 sha1의 일부를 사용하여 특정 커밋에서와 같이 파일을 가져올 수 있습니다.
주의:new-feature리모트 브런치가 아닌 로컬브런치여야 합니다.
git checkout branch_name file_name
예:
git checkout master App.java
지점 이름에 마침표가 있으면 이 작업이 작동하지 않습니다.
git checkout "fix.june" alive.html
error: pathspec 'fix.june' did not match any file(s) known to git.
의 파일 또는 하는 것에 것git
1. 다른 브랜치에서 파일 또는 디렉토리를 하나 이상 체크아웃하거나 현재 체크아웃된 브랜치에 해시를 커밋하는 방법:
# check out all files in <paths> from branch <branch_name>
git checkout <branch_name> -- <paths>
출처 : http://nicolasgallagher.com/git-checkout-specific-files-from-another-branch/.
「 」도 참조해 .man git checkout.
예:
# Check out "somefile.c" from branch `my_branch`
git checkout my_branch -- somefile.c
# Check out these 4 files from `my_branch`
git checkout my_branch -- file1.h file1.cpp mydir/file2.h mydir/file2.cpp
# Check out ALL files from my_branch which are in
# directory "path/to/dir"
git checkout my_branch -- path/to/dir
하지 않을 " " " 가branch_name으로 HEAD이것은 현재 체크 아웃된 브랜치 중 가장 중요한 커밋입니다.어떤 파일.c"를 체크 아웃하고 커밋되지 않은 로컬 변경을 덮어쓸 수도 있습니다.
# Check out "somefile.c" from `HEAD`, to overwrite any local, uncommitted
# changes
git checkout -- somefile.c
# Or check out a whole folder from `HEAD`:
git checkout -- some_directory
2. 더 나아가다:브랜치에서 파일을 체크 아웃하거나 컴퓨터상의 임의의 장소에 해시를 커밋하는 방법(매우 편리합니다!)
# General form
git show my_branch_or_commit_hash:my_file.cpp > any/path/my_file.cpp
# Example: check out `main.cpp` from 3 commits ago in your currently-checked-out
# branch (3 commits prior to `HEAD`, or `HEAD~3`) into a temporary directory
mkdir ../temp
git show HEAD~3:main.cpp > ../temp/main_old.cpp
이것을 알게 된 출처: @Jakub Narębski의 답변: git-checkout 오래된 파일 리비전을 새 이름으로 변경
? 3. 중이라면?git merge,git cherry-pick,git rebase , 「」git revert변하?????
그렇다면, 다음을 하는 게 좋을 거야.주의: 어떤 커밋해시와 브랜치가 각 컨텍스트에 있는지 확인하려면 다음 답변을 참조하십시오. Git에 따르면 '우리'는 누구이며 '그들'은 누구입니까?
# Keep `--theirs` for all conflicts within this file
git checkout --theirs -- path/to/some/file
# OR: keep `--ours` for all conflicts within this file
git checkout --ours -- path/to/some/file
또는:
# Keep `--theirs` for all conflicts within files inside this dir
git checkout --theirs -- path/to/some/dir
# OR: keep `--ours` for all conflicts within files inside this dir
git checkout --ours -- path/to/some/dir
이 작업을 수행하기 전에는 이전 섹션의 일반 양식을 수행하지 마십시오.위의 답변에서 "경고 경고" 섹션을 참조하십시오.Git에 따르면 "우리"는 누구이고 "그들"은 누구입니까?
의 path does not have our version ★★★★★★★★★★★★★★★★★」path does not have their version ★★★★★
다음과 같은 오류가 발생할 경우:
error: path 'path/to/some/dir/file1.cpp' does not have our version # OR error: path 'path/to/some/dir/file1.cpp' does not have their version
는, 의 만 하면 .상기 명령어를 실행할 때는, 다음의 조작을 실시할 필요가 있습니다.git rm 시험해 보고 해 보세요.git checkout --ours ★★★★★★★★★★★★★★★★★」git checkout --theirs명령어를 다시 입력합니다.이러한 명령어에 대한 자세한 설명은 여기를 참조하십시오.이들 명령어에 대한 자세한 설명은 git checkout -- 파일 사양에 삭제된 파일이 포함되어 있는 경우.
4. 특정 파일 또는 디렉토리를 다른 커밋 또는 브랜치에 있는 해당 파일 또는 디렉토리의 상태와 정확히 일치하도록 리셋하려면 어떻게 해야 합니까?
「」는,git checkout my_branch -- some_file_or_dir되어 있는 에 파일이 , 에 하지 않습니다.my_branch해 주세요, 「」는 「」입니다git checkout는 로컬에 존재하지만 지정된 커밋에 없는 파일을 삭제하지 않습니다.대신 지정된 커밋의 버전으로 로컬에 있는 파일만 덮어씁니다.따라서 로컬에 존재하지 않는 파일도 삭제하여 로컬에 있는 파일이 커밋 또는 브런치에 있는 파일과 완전히 일치하도록 하려면 다음 절차를 수행해야 합니다.
# How to "hard reset" "path/to/some/file_or_dir" to its state exactly as it was
# at commit or branch `my_branch`
#
# WARNING: `git status` should be TOTALLY CLEAN before beginning this process!
# Otherwise, you risk PERMANENTLY LOSING any uncommitted changes shown by
# `git status`, since `git clean -fd` 'f'orce deletes ALL files
# and 'd'irectories which are in your current working tree (file system), but
# which are *not* in the path you specify below in commit or branch `my_branch`.
# Therefore, anything NOT already committed gets **permanently lost** as though
# you had used `rm` on it!
git reset my_branch -- path/to/some/file_or_dir
git checkout-index -fa
git clean -fd # SEE WARNING ABOVE!
git commit -m "hard reset path/to/some/file_or_dir to its state \
as it was at my_branch"
이에 대한 자세한 내용은 여기를 참조하십시오.왜 git은 경로별로 하드/소프트 리셋을 할 수 없는가?
다음 항목도 참조하십시오.
- 제가 자주 참조하는 답변에 대한 빠른 링크는 다음과 같습니다.
- 더 요.
git checkout제 답변은 다음과 같습니다.Git에 따르면 "우리"는 누구이고 "그들"은 누구입니까? - [경로별로 --soft 또는 --hard git reset 하는 방법]경로별로 하드/소프트 리셋을 할 수 없는 이유는 무엇입니까?
- git-revision 파일을 새 이름으로 수정
- [my answer] git checkout -- 파일 사양에 삭제된 파일이 포함되어 있는 경우
- [내 답변] git을 사용하여 작업 트리(로컬 파일 시스템 상태)를 인덱스(스테이징 파일) 상태로 리셋하려면 어떻게 해야 합니까?
git show experiment:path/to/relative/app.js > app.js
# If your current working directory is relative, then just use:
git show experiment:app.js > app.js
또는
git checkout experiment -- app.js
다른 분기에서 파일을 복원하려면 작업 중인 분기에서 다음 명령을 사용하십시오.
git restore -s my-other-branch -- ./path/to/file
-s source예를 들어 파일을 가져올 지점입니다.
(선택한 답변은 매우 유익하지만 다소 압도적이기도 합니다.)
또는 다른 분기의 모든 파일을 원하는 경우:
git checkout <branch name> -- .
git checkout <branch_name> -- <paths>
커밋의 브랜치에서 파일을 로 하는 는, 「(어느 브랜치)」라고 .06f8251f
git checkout 06f8251f path_to_file
예를 들어 Windows의 경우:
git checkout 06f8251f C:\A\B\C\D\file.h
github의 파일을 확인하고 거기에서 꺼냅니다.
이것은 OP에 직접 답하지 않는 실용적인 접근법이지만, 일부에서는 유용하다고 생각합니다.
문제가 되는 브런치가 GitHub에 있는 경우 GitHub에서 제공하는 많은 툴 중 하나를 사용하여 원하는 브런치 및 파일로 이동한 후 'Raw'를 클릭하여 플레인 텍스트를 보고 원하는 대로 복사하여 붙여넣을 수 있습니다.
로컬 머신에 리모트파일을 풀링하기 전에 리모트파일을 전체적으로 확인할 수 있기 때문에, 이 어프로치가 마음에 듭니다.
또한 알려지지 않은 git 명령어를 피하고 싶거나 무언가를 파괴하는 것을 두려워하는 모든 사람들을 위한 간단한 해결책이 있다.
- 파일이 있는 지점에 체크 아웃하다
- 그 파일을 컴퓨터의 다른 곳에 복사하다
- 파일이 도착해야 하는 지점을 체크 아웃합니다.
- 파일을 제자리에 붙여넣다
적절한 git 명령어보다 시간이 더 걸릴 수 있습니다.하지만 그것은 효과가 있고 이해하기 쉽다.
또 다른 방법은 차이점을 가진 패치를 생성하여 마스터 브랜치에 적용하는 것입니다.예를 들어 app.js 작업을 시작하기 전 마지막 커밋은 00000aaaa이고 원하는 버전을 포함하는 커밋은 00000bbbb라고 합니다.
실험 분기에서 다음을 실행합니다.
git diff 00000aaaaa 00000bbbbb app.js > ~/app_changes.git
그러면 app.js에 대한 두 가지 커밋의 차이가 모두 있는 파일이 생성되어 원하는 위치에 적용할 수 있습니다.이 파일은 프로젝트 외부 어디에나 보관할 수 있습니다.
그런 다음 마스터에서 실행하기만 하면 됩니다.
git apply ~/app_changes.git
이제 프로젝트의 변경 사항을 수동으로 작성한 것처럼 확인할 수 있습니다.
다른 브랜치에서 파일을 체크 아웃하려면 다음 명령을 수행합니다.
git checkout branch C:\path\to\file.cs
여러 파일을 원하는 경우
git checkout branch C:\path\to\file1.cs C:\path\to\file2.cs
git checkout master -go to the master branch first
git checkout <your-branch> -- <your-file> --copy your file data from your branch.
git show <your-branch>:path/to/<your-file>
이게 도움이 되길 바라.문의사항이 있으시면 연락주세요.
언급URL : https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch
'programing' 카테고리의 다른 글
| Windows 배치파일은 자신의 파일명을 결정할 수 있습니까? (0) | 2023.04.10 |
|---|---|
| wpf 링크 버튼 (0) | 2023.04.10 |
| 사전에서 키 목록을 가져오려면 어떻게 해야 합니까? (0) | 2023.04.10 |
| 문자열을 연결하는 가장 효율적인 방법? (0) | 2023.04.10 |
| Python에서 사전 키를 목록으로 반환하려면 어떻게 해야 하나요? (0) | 2023.04.10 |