code

Git의 특정 개정판에서 단일 파일을 검색하는 방법

codestyles 2020. 9. 29. 07:51
반응형

Git의 특정 개정판에서 단일 파일을 검색하는 방법


Git 저장소가 있고 몇 달 전에 일부 파일이 어떻게 생겼는지보고 싶습니다. 그 날짜에 수정본을 찾았는데 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8. 하나의 파일이 어떻게 생겼는지 확인하고 파일에 저장해야합니다.

을 사용하여 파일을 볼 수 gitk있었지만 저장할 수있는 옵션이 없습니다. 명령 줄 도구로 시도했는데 가장 가까운 것은 다음과 같습니다.

git-show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8 my_file.txt

그러나이 명령은 파일 내용이 아닌 diff를 표시합니다. 나중에 같은 것을 사용하고 PAGER=cat출력을 파일로 리디렉션 할 수 있다는 것을 알고 있지만 실제 파일 콘텐츠를 얻는 방법을 모르겠습니다.

기본적으로 svn cat 과 같은 것을 찾고 있습니다.


자신의 대답을 완성하기 위해 구문은 실제로

git show object
git show $REV:$FILE
git show somebranch:from/the/root/myfile.txt
git show HEAD^^^:test/test.py

이 명령은 일반적인 수정 스타일을 사용하므로 다음 중 하나를 사용할 수 있습니다.

  1. 지점 이름 (같은 제안 에 의해 )
  2. HEAD+ ^문자 수 x
  3. 주어진 개정의 SHA1 해시
  4. 주어진 SHA1 해시의 처음 몇 개 (아마도 5 개) 문자

" git show" 을 사용할 때는 항상 현재 디렉토리 위치가 아닌 저장소 루트의 경로를 지정 하십시오.

( Mike Morearty 가 적어도 git 1.7.5.4에서는 경로 ./시작 부분에 " "를 넣어 상대 경로를 지정할 수 있다고 언급 했지만 예를 들면 다음과 같습니다.

git show HEAD^^:./test.py

)


git1.5.x 이전에는 몇 가지 배관 작업이 수행되었습니다.

git ls-tree <rev>
커밋 내에서 하나 이상의 'blob'객체 목록 표시

git cat-file blob <file-SHA1>
특정 개정판 내에서 커밋 된 파일을 cat 지정합니다 (svn cat과 유사). git ls-tree를 사용하여 주어진 file-sha1의 값을 검색합니다.

git cat-file -p $(git-ls-tree $REV $file | cut -d " " -f 3 | cut -f 1)::

git-ls-tree는 $ REV 개정판에서 $ file에 대한 객체 ID를 나열합니다. 이것은 출력에서 ​​잘라내어 git-cat-file에 대한 인수로 사용됩니다. 실제로는 git-cat-object라고 불리며 단순히 덤프합니다. 그 객체를 stdout에.


참고 : Git 2.11 (2016 년 4 분기)부터 git cat-file출력에 콘텐츠 필터를 적용 할 수 있습니다 !

참조 3,214,594 커밋 , 7bcf341 커밋 (2016 9월 9일)를 7bcf341 커밋 (2016 년 9 월 09), 및 b9e62f6 커밋 , 16dcc29 커밋 에 의해 (2016년 8월 24일) 요하네스 Schindelin을 ( dscho) .
(Merged by Junio ​​C gitsterHamano -- in commit 7889ed2 , 21 Sep 2016)

cat-file: 지원 --textconv/ --filters배치 모드

심지어 "하지만 git hash-objects(예를 들어 줄 끝 변환 및 응용 프로그램 외부 세계 - 투 - 망할 놈의"변환 "는을 수행 할 수 온 - 파일 시스템 데이터 스트림을 가지고 힘내 객체 저장소에 넣어하는 도구 인" 초기부터 기본적으로 기능 git cat-file이 켜졌 고, Git 객체 저장소에서 객체를 가져와 외부 세계에서 사용하기 위해 외부화하는 역 동작 " "은 다음과 같은 메커니즘이 없었습니다. "Git-to-outside-world"실행

git config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <"
git cat-file --textconv --batch

참고 : " git cat-file --textconv"은 최근 (2017)에 segfaulting을 시작했으며 Git 2.15 (2017 년 4 분기)에서 수정되었습니다.

See commit cc0ea7c (21 Sep 2017) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit bfbc2fc, 28 Sep 2017)


Note that to override/replace a file with a past content, you should not use the confusing git checkout command anymore, but git restore (Git 2.23+, August 2019)

git restore -s <SHA1> -- afile

That would restore on the working tree only the file as present in the "source" (-s) commit SHA1.
To restore also the index:

git restore -s <SHA1> -SW -- afile

(-SW: short for --staged --worktree)


If you wish to replace/overwrite the content of a file in your current branch with the content of the file from a previous commit or a different branch, you can do so with these commands:

git checkout 08618129e66127921fbfcbc205a06153c92622fe path/to/file.txt

or

git checkout mybranchname path/to/file.txt

You will then have to commit those changes in order for them to be effective in the current branch.


You need to provide the full path to the file:

git show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8:full/repo/path/to/my_file.txt

The easiest way is to write:

git show HASH:file/path/name.ext > some_new_name.ext

where:

  • HASH is the Git revision SHA-1 hash number
  • file/path/name.ext is name of the file you are looking for
  • some_new_name.ext is path and name where the old file should be saved

Example

git show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8:my_file.txt > my_file.txt.OLD

This will save my_file.txt from revision 27cf8e as a new file with name my_file.txt.OLD

It was tested with Git 2.4.5.

If you want to retrieve deleted file you can use HASH~1 (one commit before specified HASH).

EXAMPLE:

git show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8~1:deleted_file.txt > deleted_file.txt

In Windows, with Git Bash:

  • in your workspace, change dir to the folder where your file lives
  • git show cab485c83b53d56846eb883babaaf4dff2f2cc46:./your_file.ext > old.ext

And to nicely dump it into a file (on Windows at least) - Git Bash:

$ echo "`git show 60d8bdfc:src/services/LocationMonitor.java`" >> LM_60d8bdfc.java

The " quotes are needed so it preserves newlines.


This will help you get all deleted files between commits without specifying the path, useful if there are a lot of files deleted.

git diff --name-only --diff-filter=D $commit~1 $commit | xargs git checkout $commit~1

git checkout {SHA1} -- filename

this command get the copied file from specific commit.


Get the file from a previous commit through checking-out previous commit and copying file.

  • Note which branch you are on: git branch
  • Checkout the previous commit you want: git checkout 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8
  • Copy the file you want to a temporary location
  • Checkout the branch you started from: git checkout theBranchYouNoted
  • Copy in the file you placed in a temporary location
  • Commit your change to git: git commit -m "added file ?? from previous commit"

참고URL : https://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file-from-a-specific-revision-in-git

반응형