IT박스

Git의 하위 디렉토리를 확인 하시겠습니까?

itboxs 2020. 6. 8. 21:16
반응형

Git의 하위 디렉토리를 확인 하시겠습니까?


Git에서 저장소의 서브 디렉토리를 체크 아웃 할 수 있습니까?

새 WordPress 설치를 설정한다고 가정하십시오. 플러그인 및 테마 사용자 정의를 위해 두 개의 새 디렉토리를 작성합니다.

  • wordpress/wp-content/plugins/myplugins/
  • wordpress/wp-content/themes/mytheme/

Git을 통해 이러한 디렉토리를 유지하고 싶습니다. Subversion에서는 trunk/myplugins/trunk/mytheme/디렉토리 를 갖고 하위 디렉토리를 확인하여 이를 수행합니다 . Git은 단일 저장소를 사용하여 동일한 작업을 수행 할 수있는 방법이 있습니까?

Git에 거의 노출되지 않은 오랜 SVN 사용자 인 Git 패러다임에서 보트를 놓칠 수 있습니다.

편집 : 다른 내용을 저장하는 여러 분기 는 이것을 처리하는 흥미로운 방법입니다.


스파 스 체크 아웃이제 Git 1.7에 있습니다.

전체 리포지토리를 먼저 체크 아웃하지 않고 스파 스 체크 아웃을 수행 할 수 있습니까? 질문도 참조하십시오 . ”.

Git 다운로드 파일 중 일부가 작업 트리에서 끝나지 않더라도 희소 한 체크 아웃을 수행하려면 여전히 전체 저장소 다운로드 해야합니다 .


자식에서 그렇게하는 실제 방법은 없습니다. 또한 단일 작업 단위로 한 번에 두 트리에 모두 영향을 미치는 변경 사항을 작성하지 않으면 두 저장소 모두에 단일 저장소를 사용해야 할 이유가 없습니다. 나는이 Subversion 기능을 놓칠 것이라고 생각했지만 리포지토리를 만들 때 관리 정신적 오버 헤드가 거의 없다는 것을 알았습니다 (간단히 리포지토리가 작업 복사본 옆에 저장되어 있기 때문에 명시 적으로 외부의 장소를 선택하지 않아도 됨) 작업 사본) 작은 단일 목적 저장소를 많이 만드는 데 익숙해졌습니다.

당신이 주장 (또는 정말 필요)하면,하지만 당신은 단지와 자식 저장소를 만들 수 mythememyplugins디렉토리 및 설치 워드 프레스 내에서 그 심볼릭 링크.


MDCore는 다음과 같이 썼습니다.

예를 들어, mytheme에 대한 커밋 myplugin 의 개정 번호를 증가시킵니다.

git은 모든 형식의 개정 번호를 단조 증가시키는 개념으로 완전히 없애기 때문에 두 디렉토리를 단일 저장소에두기로 결정하면 git에 대해 걱정할 필요가 없습니다.

git의 단일 저장소에 정리할 항목에 대한 유일한 기준은 그것이 단일 단위를 구성하는지 여부입니다. 귀하의 경우에는 각 디렉토리의 편집 내용을 개별적으로 보는 것이 타당하지 않은 변경 사항이 있는지 여부. 한 번에 두 디렉토리 모두에서 파일을 편집해야하는 위치에 변경 사항이 있고 편집 내용이 함께 속한 경우 하나의 저장소 여야합니다. 그렇지 않다면 함께 뭉개지 마십시오.

Git은 실제로 별도의 엔티티에 대해 별도의 저장소를 사용하기를 원합니다.

서브 모듈

서브 모듈은 두 디렉토리를 하나의 저장소에 유지하려는 욕구를 해결하지 못합니다. 실제로 각 디렉토리에 대해 별도의 저장소를 갖는 것을 강제 한 다음 하위 모듈을 사용하여 다른 저장소에 모입니다. 설상가상으로 WordPress 설치 내의 디렉토리는 동일한 디렉토리의 직접 서브 디렉토리가 아니며 다른 파일이 많은 계층 구조의 일부이므로 디렉토리 별 저장소를 통합 저장소의 서브 모듈로 사용하면 통합되지 않기 때문에 이점이 없습니다. 저장소는 유스 케이스 / 필요를 반영하지 않습니다.


드문 드문 체크 아웃에 대해 내가 싫어하는 한 가지는 디렉토리 깊이가 몇 개인 서브 디렉토리를 체크 아웃하려면 디렉토리 구조에 이어지는 모든 디렉토리가 포함되어야한다는 것입니다.

이 문제를 해결하는 방법은 내 작업 공간이 아닌 장소에서 저장소를 복제 한 다음 내 작업 공간 디렉토리에 저장소의 하위 디렉토리에 대한 심볼릭 링크를 만드는 것입니다. 자식 상태는 현재 작업 디렉토리에 상대적인 변경 파일을 표시하기 때문에 Git은 이와 같이 잘 작동합니다.


실제로, "좁은"또는 "부분적인"또는 "희소 한"체크 아웃은 Git에 대한 현재의 대규모 개발 중에 있습니다. 여전히 아래에 전체 저장소가 .git있습니다. 따라서 다른 두 개의 게시물은 Git의 현재 상태에 대한 최신 정보이지만 결국 드문 드문 체크 아웃을 수행 할 수있는 것처럼 보입니다 . 더 자세한 정보가 필요하면 메일 링리스트를 확인하십시오. 빠르게 변경되고 있습니다.


git clone --filter 힘내 2.19에서

이 옵션은 실제로 서버에서 불필요한 객체 가져 오기를 건너 뜁니다.

git clone --depth 1 --no-checkout --filter=blob:none \
  "file://$(pwd)/server_repo" local_repo
cd local_repo
git checkout master -- mdir/

서버는 다음과 같이 구성되어야합니다.

git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1

v2.19.0부터 서버 지원은 없지만 이미 로컬에서 테스트 할 수 있습니다.

file://$(path)git clone프로토콜 shenanigans 를 극복하기 위해 필요합니다 : 상대 경로로 로컬 자식 저장소를 얕은 복제하는 방법은 무엇입니까?

--depth 1이미 내포되어 있음을 기억 --single-branch하십시오. Git에서 단일 브랜치를 어떻게 복제합니까?

TODO : --filter=blob:none모든 얼룩을 건너 뛰지 만 여전히 모든 트리 개체를 가져옵니다. 그러나 일반적인 저장소에서는 파일 자체와 비교할 때 크기가 작아야하므로 이미 충분합니다. https://www.spinics.net/lists/git/msg342006.html 에서 물어보십시오. 개발자는 --filter=tree:0작업을 수행하고 있다고 대답했습니다 .

의 형식은 --filter에 설명되어 man git-rev-list있습니다.

이 기능을 지원하기 위해 Git 원격 프로토콜이 확장되었습니다.

힘내 나무에 대한 문서 :

그것을 테스트

#!/usr/bin/env bash
set -eu

list-objects() (
  git rev-list --all --objects
  echo "master commit SHA: $(git log -1 --format="%H")"
  echo "mybranch commit SHA: $(git log -1 --format="%H")"
  git ls-tree master
  git ls-tree mybranch | grep mybranch
  git ls-tree master~ | grep root
)

# Reproducibility.
export GIT_COMMITTER_NAME='a'
export GIT_COMMITTER_EMAIL='a'
export GIT_AUTHOR_NAME='a'
export GIT_AUTHOR_EMAIL='a'
export GIT_COMMITTER_DATE='2000-01-01T00:00:00+0000'
export GIT_AUTHOR_DATE='2000-01-01T00:00:00+0000'

rm -rf server_repo local_repo
mkdir server_repo
cd server_repo

# Create repo.
git init --quiet
git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1

# First commit.
# Directories present in all branches.
mkdir d1 d2
printf 'd1/a' > ./d1/a
printf 'd1/b' > ./d1/b
printf 'd2/a' > ./d2/a
printf 'd2/b' > ./d2/b
# Present only in root.
mkdir 'root'
printf 'root' > ./root/root
git add .
git commit -m 'root' --quiet

# Second commit only on master.
git rm --quiet -r ./root
mkdir 'master'
printf 'master' > ./master/master
git add .
git commit -m 'master commit' --quiet

# Second commit only on mybranch.
git checkout -b mybranch --quiet master~
git rm --quiet -r ./root
mkdir 'mybranch'
printf 'mybranch' > ./mybranch/mybranch
git add .
git commit -m 'mybranch commit' --quiet

echo "# List and identify all objects"
list-objects
echo

# Restore master.
git checkout --quiet master
cd ..

# Clone. Don't checkout for now, only .git/ dir.
git clone --depth 1 --quiet --no-checkout --filter=blob:none "file://$(pwd)/server_repo" local_repo
cd local_repo

# List missing objects from master.
echo "# Missing objects after --no-checkout"
git rev-list --all --quiet --objects --missing=print
echo

echo "# Git checkout fails without internet"
mv ../server_repo ../server_repo.off
! git checkout master
echo

echo "# Git checkout fetches the missing directory from internet"
mv ../server_repo.off ../server_repo
git checkout master -- d1/
echo

echo "# Missing objects after checking out d1"
git rev-list --all --quiet --objects --missing=print

GitHub의 상류 .

힘내 v2.19의 출력 :

# List and identify all objects
c6fcdfaf2b1462f809aecdad83a186eeec00f9c1
fc5e97944480982cfc180a6d6634699921ee63ec
7251a83be9a03161acde7b71a8fda9be19f47128
62d67bce3c672fe2b9065f372726a11e57bade7e
b64bf435a3e54c5208a1b70b7bcb0fc627463a75 d1
308150e8fddde043f3dbbb8573abb6af1df96e63 d1/a
f70a17f51b7b30fec48a32e4f19ac15e261fd1a4 d1/b
84de03c312dc741d0f2a66df7b2f168d823e122a d2
0975df9b39e23c15f63db194df7f45c76528bccb d2/a
41484c13520fcbb6e7243a26fdb1fc9405c08520 d2/b
7d5230379e4652f1b1da7ed1e78e0b8253e03ba3 master
8b25206ff90e9432f6f1a8600f87a7bd695a24af master/master
ef29f15c9a7c5417944cc09711b6a9ee51b01d89
19f7a4ca4a038aff89d803f017f76d2b66063043 mybranch
1b671b190e293aa091239b8b5e8c149411d00523 mybranch/mybranch
c3760bb1a0ece87cdbaf9a563c77a45e30a4e30e
a0234da53ec608b54813b4271fbf00ba5318b99f root
93ca1422a8da0a9effc465eccbcb17e23015542d root/root
master commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
mybranch commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
040000 tree b64bf435a3e54c5208a1b70b7bcb0fc627463a75    d1
040000 tree 84de03c312dc741d0f2a66df7b2f168d823e122a    d2
040000 tree 7d5230379e4652f1b1da7ed1e78e0b8253e03ba3    master
040000 tree 19f7a4ca4a038aff89d803f017f76d2b66063043    mybranch
040000 tree a0234da53ec608b54813b4271fbf00ba5318b99f    root

# Missing objects after --no-checkout
?f70a17f51b7b30fec48a32e4f19ac15e261fd1a4
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb
?308150e8fddde043f3dbbb8573abb6af1df96e63

# Git checkout fails without internet
fatal: '/home/ciro/bak/git/test-git-web-interface/other-test-repos/partial-clone.tmp/server_repo' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

# Git checkout fetches the missing directory from internet
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.

# Missing objects after checking out d1
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb

결론 : 외부의 모든 얼룩이 d1/없습니다.

참고 root/rootmybranch/mybranch도 누락,하지만 --depth 1가죽이 누락 된 파일 목록에서. 를 제거하면 --depth 1누락 된 파일 목록에 표시됩니다.


As your edit points out, you can use two separate branches to store the two separate directories. This does keep them both in the same repository, but you still can't have commits spanning both directory trees. If you have a change in one that requires a change in the other, you'll have to do those as two separate commits, and you open up the possibility that a pair of checkouts of the two directories can go out of sync.

If you want to treat the pair of directories as one unit, you can use 'wordpress/wp-content' as the root of your repo and use .gitignore file at the top level to ignore everything but the two subdirectories of interest. This is probably the most reasonable solution at this point.

Sparse checkouts have been allegedly coming for two years now, but there's still no sign of them in the git development repo, nor any indication that the necessary changes will ever arrive there. I wouldn't count on them.


You can't checkout a single directory of a repository because the entire repository is handled by the single .git folder in the root of the project instead of subversion's myriad of .svn directories.

The problem with working on plugins in a single repository is that making a commit to, e.g., mytheme will increment the revision number for myplugin, so even in subversion it is better to use separate repositories.

The subversion paradigm for sub-projects is svn:externals which translates somewhat to submodules in git (but not exactly in case you've used svn:externals before.)


There is an inspiration here. Just utilize shell regex or git regex.

git checkout commit_id */*.bat  # *.bat in 1-depth subdir exclude current dir, shell regex  
git checkout commit_id '*.bat'  # *.bat in all subdir include current dir, git regex

Use quotation to escape shell regex interpretation and pass wildcards to git.

The first one is not recursive, only files in 1-depth subdir. But the second one is recursive.

As for your situation, the following may be enough.

git checkout master */*/wp-content/*/*
git checkout master '*/wp-content/*'

Just hack the lines as required.

참고URL : https://stackoverflow.com/questions/180052/checkout-subdirectories-in-git

반응형