programing

로컬 및 원격 Git 저장소의 마스터 분기 이름 변경

fastcode 2023. 4. 10. 22:17
반응형

로컬 및 원격 Git 저장소의 마스터 분기 이름 변경

는 지점을 있다.master합니다.origin/master.

을 이이이 i i i로 바꾸고 master-old로컬과 리모트 양쪽에서 사용할 수 있습니다.게가 ??

른른른른을 추적한 origin/master 로컬(로컬, "로컬", "", "로컬"을 참조해 주세요.mastergit pull리모트 브랜치의 이름을 변경하면 어떻게 됩니까?
의 연?는?git pull수 없는 할 수 .origin/master더더??

다음, 저는 .master한 번 말씀드리지만, 하다, 하다, 하다, 하다, 하다, 하다, 하다, 하다, 하면 어떻게 요?git pull

이 모든 것이 많은 문제를 야기할 것 같아요.제가 원하는 것을 얻을 수 있는 깔끔한 방법은 없나요?아니면 그냥 떠날까?mastermaster-new기서서고고고고고고고고고?

이름 변경에 가장 가까운 것은 원격에서 삭제한 후 다시 생성하는 것입니다.예를 들어 다음과 같습니다.

git branch -m master master-old
git push remote :master         # Delete master
git push remote master-old      # Create master-old on remote

git checkout -b master some-ref # Create a new local master
git push remote master          # Create master on remote

하지만 여기에는 많은 주의사항이 있습니다.첫째, 기존 체크아웃은 이름 변경에 대해 알 수 없습니다. Git은 지점 이름을 추적하지 않습니다.새로운 기능master아직 존재하지 않습니다.git pull은 에러가 발생합니다.새로운 기능master가 생성되었습니다.풀은 머지를 시도합니다.master ★★★★★★★★★★★★★★★★★」master-old따라서 이전에 저장소를 체크아웃한 모든 사용자의 협조가 없으면 일반적으로 좋지 않습니다.

주의: Git의 새로운 버전에서는 기본적으로는 마스터 브랜치를 원격으로 삭제할 수 없습니다. , 「」를 합니다.receive.denyDeleteCurrent에서 ""로warn ★★★★★★★★★★★★★★★★★」ignore리모트 저장소로 이동합니다.그렇지 않으면 즉시 새 마스터를 만들 준비가 되어 있으면git push remote :master 패스합니다.--forcegit push remote master스텝. 리모트 설정을 변경할 수 없는 경우 마스터 브랜치를 완전히 삭제할 수 없습니다.

은 현재 은 지점)에만됩니다.master브런치). 기타 브런치는 위와 같이 삭제하고 다시 작성할 수 있습니다.

에 있다고 요.master:

git push origin master:master-old        # 1
git branch master-old origin/master-old  # 2
git reset --hard $new_master_commit      # 3
git push -f origin                       # 4
  1. ★★★★★★★★★★★★★★★.master-oldorigin " " "에 한"master이치노
  2. .origin/master-oldbranch(트래킹브런치로서 자동적으로 올바르게 설정됩니다).
  3. , 그럼 이제 자기 보세요.master당신이 지적하고 싶은 어떤 약속이든요
  4. force-change ( 체인지)master origin 로컬을 master.

한 더 .master-old올바르게 설정되어 있다.origin/master-old이 글의 작성 시점에서 투고된 다른 솔루션에는, 그러한 솔루션이 포함되어 있지 않습니다.)

Git v1.7에서는 조금 달라진 것 같습니다.새로운 리모트에 대한 로컬 지점의 추적 참조를 쉽게 업데이트할 수 있습니다.

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

브랜치 이름을 변경하는 방법은 여러 가지가 있지만, 더 큰 문제에 초점을 맞추겠습니다."클라이언트가 로컬에서 지사를 조작하지 않고 빠르게 전송할 수 있도록 하는 방법"

먼저 간단한 사진:

이것은 실제로 하기 쉬운 것입니다.하지만 악용하지 마세요.전체 아이디어는 머지 커밋에 의존합니다.머지 커밋을 통해 브랜치의 이력을 빠르게 전달하고 다른 브랜치와 링크할 수 있기 때문입니다.

브랜치 이름 변경:

# rename the branch "master" to "master-old"
# this works even if you are on branch "master"
git branch -m master master-old

새 "마스터" 분기를 만듭니다.

# create master from new starting point
git branch master <new-master-start-point>

부모-자녀 이력을 가지는 머지 커밋을 작성합니다.

# now we've got to fix the new branch...
git checkout master

# ... by doing a merge commit that obsoletes
# "master-old" hence the "ours" strategy.
git merge -s ours master-old

그리고 부라.

git push origin master

은, 을 합니다.mergecommit을 사용하면 브런치를 새로운 리비전으로 고속 전송할 수 있습니다.

적절한 머지 커밋메시지를 사용합니다.

renamed branch "master" to "master-old" and use commit ba2f9cc as new "master"
-- this is done by doing a merge commit with "ours" strategy which obsoletes
   the branch.

these are the steps I did:

git branch -m master master-old
git branch master ba2f9cc
git checkout master
git merge -s ours master-old
git checkout -b new-branch-name
git push remote-name new-branch-name :old-branch-name

해야 할 도 있습니다.new-branch-nameold-branch-name

이전 질문에서와 같은 상황을 여전히 묻고 있을 거라고 생각합니다.즉, master-new의 이력에는 master-old가 포함되지 않습니다.* master-new를 master라고 부르면 사실상 역사를 다시 쓰게 됩니다.어떻게 마스터가 이전 마스터 위치의 후손이 아닌 상태가 되었는지는 중요하지 않습니다. 단지 마스터가 그 상태에 있다는 것입니다.

마스터가 존재하지 않는 동안 풀하려고 하는 다른 사용자는 풀링에 실패하게 됩니다(리모트에 대한 참조는 없습니다).이러한 사용자는 마스터가 다시 새로운 장소에 존재하게 되면 마스터를 새로운 리모트마스터와 결합할 필요가 있습니다.마치 마스터는 저장소에서 master-old와 master-new를 Marge한 경우와 같습니다.여기서 하려는 일을 고려하면 병합에 충돌이 있을 수 있습니다.(이러한 문제가 해결되어 결과가 저장소로 되돌려지면 두 버전의 이력 모두 더 나쁜 상태가 됩니다.)

간단히 대답하자면, 여러분은 때때로 여러분의 역사에 실수가 있을 것이라는 것을 받아들여야 합니다.이거 괜찮아요.누구에게나 있는 일이야.git.git 저장소에 되돌린 커밋이 있습니다.중요한 것은 일단 역사를 출판하면 누구나 믿을 수 있다는 것이다.

*이러한 경우 마스터에 일부 변경을 푸시한 후 기존 위치에 새로운 브랜치를 작성하는 것과 같습니다.괜찮아요.

선택한 답변은 시도했을 때 실패했습니다.다음 오류가 발생합니다.refusing to delete the current branch: refs/heads/master나에게 맞는 글을 올려야겠다.

git checkout master             # If not in master already

git branch placeholder          # Create placeholder branch
git checkout placeholder        # Check out to placeholder
git push remote placeholder     # Push placeholder to remote repository

git branch -d master            # Remove master in local repository
git push remote :master         # Remove master from remote repository.

원격 저장소로 푸시하기 직전에 플레이스 홀더를 체크 아웃하는 것이 중요합니다.나머지는 자체 설명입니다.마스터 브랜치를 삭제하고 리모트저장소로 푸시하면 됩니다.여기서 발췌했습니다.

서버에 로그인하고 Git 디렉토리로 이동하여 Bare 저장소의 브랜치 이름을 변경합니다.

이것은, 같은 브랜치의 재업로드와 관련된 모든 문제를 수반하는 것은 아닙니다.실제로 '클라이언트'는 수정된 이름을 자동으로 인식하고 원격 참조를 변경합니다.

이후(또는 이전) 지점의 로컬 이름을 수정할 수도 있습니다.

로컬에서도 리모트에서도 브랜치명을 변경하는 것은 매우 간단합니다.

브랜치에서는, 다음의 작업을 간단하게 실시할 수 있습니다.

git branch -m <branch>

또는 그렇지 않은 경우 다음을 수행해야 합니다.

git branch -m <your_old_branch> <your_new_branch>

그런 다음 다음과 같이 삭제를 리모트에 푸시합니다.

git push origin <your_old_branch>

이것으로 끝입니다.

푸시 중에 업스트림에러가 발생했을 경우는, 다음의 조작을 실시합니다.

git push --set-upstream origin <your_new_branch>

또, 실제의 커맨드 라인의 스텝을 나타내기 위해서, 이하의 이미지를 작성했습니다.다음 단계를 따르기만 하면 됩니다.

여기에 이미지 설명을 입력하십시오.

그럼 어떻게 되는 거죠?

git checkout old-branch-name
git push remote-name new-branch-name
git push remote-name :old-branch-name
git branch -m new-branch-name

이것은 내가 알고 있는 것 중 가장 심플하고 알기 쉬운 방법입니다.

-m을 사용하여 로컬브런치를 '이동'

git branch -m my_old_branch_name my_new_branch_name

'이동된' 분기를 리모컨에 푸시하고 -u를 사용하여 '업스트림'을 설정하십시오.

git push origin -u my_new_branch_name

「업스트림」을 설정하면, 로컬 브랜치를 리모트에 「접속」할 수 있기 때문에, fetch, pull, push등의 조작이 가능하게 됩니다.

원격에서 이전 분기를 삭제합니다.

git push origin -D <old_name>

첫 번째 단계에서 로컬 지점을 '이동'했으므로 로컬 지점이 이미 사라졌습니다.

다음 작업을 수행할 수 있습니다.

git -m master master-old #rename current master
git checkout -b master   #create a new branch master
git push -f origin master #force push to master

그러나 다른 사람이 이 저장소를 공유하고 있다면 강제 푸시는 좋지 않은 생각입니다.강제 푸시는 그들의 개정 이력을 새로운 개정 이력과 충돌하게 할 것이다.

셸 스크립트에 다음 정보를 저장하여 작업을 수행할 수 있습니다.

예를 들어 다음과 같습니다.

remote="origin"

if [ "$#" -eq 0 ] # if there are no arguments, just quit
then
    echo "Usage: $0 oldName newName or $0 newName" >&2
    exit 1
elif
    [ "$#" -eq 1 ] # if only one argument is given, rename current branch
then
    oldBranchName="$(git branch | grep \* | cut -d ' ' -f2)" #save current branch name
    newBranchName=$1
else
    oldBranchName=$1
    newBranchName=$2
fi

git branch -m $oldBranchName $newBranchName

git push $remote :$oldBranchName # Delete old branch on remote
git push --set-upstream $remote $newBranchName # Add new branch name on remote and track it

여기서 기본 원격 이름 "origin"은 하드 코딩되어 있습니다.스크립트를 확장하여 구성할 수 있습니다.

그러면 이 스크립트를 Bash 에일리어스, Git 에일리어스 또는 Sourcetree 커스텀액션과 함께 사용할 수 있습니다.

github.com 또는 github.com에 접속하여 브랜치를 클릭하여 이름을 변경합니다.그런 다음 로컬로 실행합니다.

git branch -m <old-branch-name> <new-branch-name>
git fetch origin
git branch -u origin/<new-branch-name> <new-branch-name>

2022년 솔루션 업데이트

GitHub은 현재 공식적으로 지사의 이름을 바꾸고 GitHub Docs에서 가이던스를 이용할 수 있도록 사용자를 지원하고 있다.

저는 그들의 절차를 따라 로컬 지점과 리모트 지점의 이름을 모두 성공적으로 변경했습니다.

URL이 파손되었을 경우의 해결 방법은 다음과 같습니다.

원격 분기 이름 변경

  1. GitHub.com에서 저장소의 기본 페이지로 이동합니다.
  2. 파일 목록 위에서 분기를 클릭합니다.여기에 이미지 설명 입력
  3. 분기 목록에서 이름을 변경할 분기 오른쪽에 있는 편집 기호를 클릭합니다.여기에 이미지 설명 입력
  4. 브랜치의 새 이름을 입력하고 정보를 검토한 다음 브랜치 이름 변경을 클릭합니다.

지점 이름 변경 후 로컬 복제 업데이트

GitHub Docs에 따르면:

GitHub 저장소의 브랜치 이름을 변경한 후 저장소의 로컬 클론을 가진 공동작업자는 클론을 업데이트해야 합니다.

시스템의 저장소 로컬 복제에서 다음 명령을 실행하여 기본 분기 이름을 업데이트합니다.

$ git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME
$ git fetch origin
$ git branch -u origin/NEW-BRANCH-NAME NEW-BRANCH-NAME
$ git remote set-head origin -a

필요에 따라 다음 명령을 실행하여 이전 지점 이름에 대한 추적 참조를 제거합니다.

$ git remote prune origin

중요한 것은 이중 이름을 사용하고 있다는 것을 깨닫는 것입니다.master로로 합니다.master-old, 「」도 있습니다.master-new로로 합니다.master.

다른 모든 답변에서 나는 이것을 종합했다.

doublerename master-new master master-old

하겠습니다.doublerenameBash 수 :

# doublerename NEW CURRENT OLD
#   - arguments are branch names
#   - see COMMIT_MESSAGE below
#   - the result is pushed to origin, with upstream tracking info updated
doublerename() {
  local NEW=$1
  local CUR=$2
  local OLD=$3
  local COMMIT_MESSAGE="Double rename: $NEW -> $CUR -> $OLD.

This commit replaces the contents of '$CUR' with the contents of '$NEW'.
The old contents of '$CUR' now lives in '$OLD'.
The name '$NEW' will be deleted.

This way the public history of '$CUR' is not rewritten and clients do not have
to perform a Rebase Recovery.
"

  git branch --move $CUR $OLD
  git branch --move $NEW $CUR

  git checkout $CUR
  git merge -s ours $OLD -m $COMMIT_MESSAGE

  git push --set-upstream --atomic origin $OLD $CUR :$NEW
}

은 역사를 것과 .git rebase은 상당히 다르지만 할 수 .git pull master.

git update-ref newref oldref
git update-ref -d oldref newref

언급URL : https://stackoverflow.com/questions/1526794/rename-master-branch-for-both-local-and-remote-git-repositories

반응형