Programing

새로운 (그리고 비어있는) "루트"브랜치를 만드는 방법?

lottogame 2020. 7. 14. 08:20
반응형

새로운 (그리고 비어있는) "루트"브랜치를 만드는 방법?


이 자식 저장소에 새로운 "루트"브랜치를 정의하고 싶습니다. "루트"분기 란 저장소 1 의 다른 모든 분기와 완전히 독립적 인 분기를 의미합니다 .

불행히도, Arepo의 커밋 트리의 맨 아래에 있는 커밋 (이것을 호출하자)조차 많은 파일을 포함합니다 (이것은 이미 상당히 성숙한 프로젝트에서 초기화 된 저장소였습니다).

이것은 내가 A새로운 브랜치로 주었다고해도이 <start-point>새로운 브랜치는 "클린 슬레이트"에서 시작하는 것이 아니라에 커밋 된 모든 파일을 포함한다는 것을 의미합니다 A.

이 리포지토리에 가능한 한 <start-point>가깝게 완전히 베어 브랜치를 만들 수있는 방법이 A있습니까?


1 BTW, 이것은 새로운 저장소를 만드는 것과 동등 하지 않습니다 . 여러 가지 이유로 별도의 repos가 덜 편리합니다.


편집 : 좋아, 이것은 vcsjones 의 답변 에 따라 내가 한 일입니다 .

# save rev of the current earliest commit
OLDBASE=$(git rev-list --max-parents=0 HEAD)

# create a new orphan branch and switch to it
git checkout --orphan newbranch
# make sure it's empty
git rm -rf .

# create a new empty commit in the new branch, and
# save its rev in NEWBASE
git commit --allow-empty -m 'base commit (empty)'
NEWBASE=$(git rev-list HEAD)

# specify $NEWBASE as the new parent for $OLDBASE, and
# run filter-branch on the original branch
echo "$OLDBASE $NEWBASE" > .git/info/grafts
git checkout master
git filter-branch

# NOTE: this assumes that the original repo had only one
# branch; if not, a git-filter-branch -f <branch> command
# need to be run for each additional branch.

rm .git/info/grafts

이 절차는 다소 복잡하지만 최종 결과는 새로운 "클린 슬레이트 브랜치"의 역할을 할 수 있는 기본 커밋입니다 <start-point>. 그때해야 할 일은

git checkout -b cleanslate $(git rev-list --max-parents=0 HEAD)

앞으로 나는 항상 다음과 같은 새로운 저장소를 만들 것입니다.

git init
git commit --allow-empty -m 'base commit (empty)'

...so that the first commit is empty, and always available for starting a new independent branch. (This would be, I know, a very rarely needed facility, but it is quite effortless to make it readily available.)


Use the --orphan when creating the branch:

git checkout --orphan YourBranchName

This will create a new branch with zero commits on it, however all of your files will be staged. At that point you could just remove them.
("remove them": A git reset --hard will empty the index, leaving you with an empty working tree)

Take a look at the man page for checkout for more information on --orphan.


To add to the accepted answer - best practice to revert to clean state is to create an initial empty commit so you can easily rebase while setting up your branches for posterity. Plus since you want a clean state you probably have committed files that you shouldn't, so you have to remove them from the index. With those in mind you should:

$ git checkout --orphan dev2
Switched to a new branch 'dev2'
$ git reset # unstage all the files, you probably don't want to commit all of them
$ git commit --allow-empty -m 'Initial empty commit'
[dev2 (root-commit) a515c28] Initial empty commit

참고URL : https://stackoverflow.com/questions/15034390/how-to-create-a-new-and-empty-root-branch

반응형