Programming

현재 분기를 다른 분기로 병합하는 방법

procodes 2020. 5. 21. 21:21
반응형

현재 분기를 다른 분기로 병합하는 방법


master와 dev의 두 가지가 있습니다. 나는 항상 개발 작업을하고 코드가 프로덕션 용도로 승인되면 마스터 지점에서만 코드를 확인합니다. 그렇게 할 때 다음을 수행해야합니다.

git checkout master
git merge dev
git checkout dev

정말 장황하고 자주하기 때문에 최소화하고 싶습니다. 마스터 브랜치를 먼저 체크 아웃하지 않고 현재 브랜치 개발에서 다른 브랜치 마스터로 병합하는 데 사용할 수있는 하나의 git 명령이 있습니까? 다음과 같은 것이있을 수 있습니다.

git merge dev to master

대단 할 것입니다. 나는 자식 문서를 살펴 보았고 아무것도 보지 못했다.


1. 로컬 저장소에 대한 원격 별명을 추가하십시오 (예 :

git remote add self file:///path/to/your/repository

(또는 창문에서 git remote add self C:\path\to\your\repository)

2. 예 :

git push self dev:master

@zerome에 의해 현재 가장 많이 투표 된 답변은 좋은 답변이지만 불필요하게 장황합니다.

git repo의 기초에서 당신은 이것을 할 수 있습니다 : git push . dev:master

트리의 어느 곳에서나 작동하는보다 일반적인 솔루션은 다음과 같습니다.

git push $(git rev-parse --show-toplevel) dev:master

가장 좋은 방법은 전역 gitconfig ( ~/.gitconfig) 에있는 별칭을 사용하는 것입니다 .

[alias]
    merge-to = "!f() { git checkout $1 && git merge $2 && git checkout -; }; f"

모든 저장소에서 다음과 같이 호출 할 수 있습니다.

git merge-to master dev

Jefromi 별칭을 약간 수정하면 현재 분기를 입력하지 않아도됩니다.

따라서 다음과 같이 사용하십시오 git merge-to dev.

dev분기로 전환 하고 CURRENT와 병합 한 다음 다시 전환합니다.

예를 들어, master지점 에 있다고 가정하면 마스터에 dev로 병합되고 여전히 마스터에있게됩니다.

확실히 내 dotfiles로 이동합니다 :)

[alias]
  merge-to = "!gitmergeto() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git checkout $1 && git merge $tmp_branch && git checkout $tmp_branch; unset tmp_branch; }; gitmergeto"

이건 낡았지만 ...

위의 @ kevin-lyda와 @ dmytrii-nagirniak의 솔루션을 결합합니다. 이 별명은 현재 분기를 지정된 분기로 병합합니다. 원격 메소드를 사용하고 git 명령을 사용하여 컨텍스트를 가져옵니다.

[alias]
    merge-to = "!gitmergeto() { git push \"`git rev-parse --show-toplevel`\" `git rev-parse --abbrev-ref HEAD`:$1; } && gitmergeto"

다음과 같이 사용하십시오 :

git merge-to other-branch-name

다른 분기를 체크 아웃하지 않고 현재 분기를 다른 분기로 병합하려면 다음을 수행하십시오.

빨리 감기 병합

정말 쉽습니다. 정의에 따르면 빨리 감기 병합은 단순히 분기 포인터가 커밋 트리에서 앞으로 이동 함을 의미합니다. 따라서 다음을 시뮬레이션하기 만하면됩니다 .

git branch -f master dev

주의 사항 : 이것은 분기 또는 다른 분기 master에있는 커밋 가리키는 것으로 가정합니다 dev. 그렇지 않으면 작업 손실이 발생할 위험이 있습니다! git merge빨리 감기가 불가능할 때 병합 커밋 (또는 불평)을 생성하는 것과 달리 ,이 방법은 자동 으로 분기 포인터가 다른 커밋을 가리 키도록합니다.

이것은 또한 당신이 repo에서 일하는 유일한 사람이고 / 또는 당신이하는 일을 알고 있다고 가정합니다.

팁 : 당신이를했다면 git fetch당신은 새로운 커밋이 origin/master당신이 이동할 수, master사용하여 확인하지 않고 지점 :

git branch -f master origin/master

병합 커밋을 통한 병합

항상 가능하지는 않습니다. 병합 커밋을 만들려면 병합 작업을 수행해야합니다. 병합 작업을 수행하려면 현재 분기에없는 다른 분기에 커밋이 있어야합니다.

당신은에서 커밋이있는 경우 master입니다 지점 하지dev, 당신이 할 수있는 지점 :

면책 조항 : 이것은 단지 개념 증명 일뿐 입니다. 체크 아웃하지 않고 때로는 다른 지점으로 병합하는 것이 가능하다는 것을 보여주기 위해서입니다. 매일 사용하고 싶다면 쉘 리디렉션을 사용하여 별칭을 만들거나 쉘 스크립트를 만들고 싶을 것입니다. 그런 다음 질문에 표시된 더 짧은 프로세스를위한 쉘 스크립트를 만들 수도 있습니다.

git checkout -b temp
git merge --no-ff -e master
git branch -f master temp
git checkout dev
git branch -D temp

설명:

  1. 현재 브랜치와 동일한 커밋을 가리키는 임시 브랜치를 확인하십시오.
  2. Merge master into the temporary branch and launch commit message editor. If you want the merge commit to look like you had merged the dev branch into master, edit it from this:

    Merge branch 'master' into temp
    

    to this:

    Merge branch 'dev'
    

    Tip: You can use -m "Merge branch 'dev'" instead of -e to be quicker.

  3. Update the master branch pointer to point to the merge commit.
  4. Check out the dev branch.
  5. Force delete the temporary branch.

This still touches your working tree, but minimally so. It doesn't roll back the tree all the way to state of the original master just to bring in the development changes once again. Some may not care, but for others it may be important.


A lot of times you're coming from the branch you would like to merge the current branch into. In that case you could do:

git co - && git merge @{-1}

for example:

git checkout somebranch      // (while on master)

// add some commits

git co - && git merge @{-1}  // will merge somebranch into master

My solution is similar to the other answers, with the following differences:

  • the function is split into multiple lines for readability
  • the function calls set -ex so each command is printed and if command fails function exits right away
  • alias passes its arguments except the first (target branch) to git merge
  • the function includes a null command : git merge which makes sure tab-completion works with some shell setups (e.g. gitfast from oh-my-zsh)
[alias]
  merge-to = "!f() { : git merge ; \
      set -ex ; \
      local this=$(git rev-parse --abbrev-ref HEAD) ; \
      local target=$1 ; \
      shift ; \
      git checkout $target ; \
      git merge $this \"$@\" ; \
      git checkout $this ; \
    } ; f"

참고URL : https://stackoverflow.com/questions/3672073/how-to-merge-the-current-branch-into-another-branch

반응형