IT TIP

git는 두 분기 간의 diff에서 커밋을 만듭니다.

itqueen 2020. 12. 11. 21:09
반응형

git는 두 분기 간의 diff에서 커밋을 만듭니다.


비슷한 역사가 거의 없지만 서로 관련이있는 두 가지가 있습니다.

나는 하나의 git 커밋 에서 그 둘 사이의 변경을 원합니다 .

파일이 삭제되고 해당 패치 사이에 생성되었으며 패치에이를 반영하고 싶습니다.

즉 : 다음 항목은 작동하지 않습니다.

git diff branch_a branch_b -- > patchfile
git checkout branch_b
git apply patchfile # deletes and adds are ignored
git commit # we miss the deletes

이를 수행하는 간단한 방법은 다음과 같습니다.

  • branch_a ( git branch tmp branch_a && git checkout tmp) 에서 분기 tmp 생성 및 체크 아웃
  • git reset --soft branch_b
  • git commit

그 커밋에는 모든 차이가 있어야합니다.


두 개의 지점이있는 경우 :

  1. has-changes
  2. needs-changes

변경 사항을에서 has-changes이동하려면 needs-changes다음을 수행하십시오.

git checkout -b deleteme has-changes # Create temporary branch to build commit on
git reset --soft needs-changes       # Move diff into index
git commit                           # Create the diff patch commit
git checkout needs-changes
git cherry-pick deleteme             # Apply the diff to the needs-changes
git branch -D deleteme               # Delete the temporary branch

has-changes수정 되는 것에 신경 쓰지 않거나 다른 방식으로 보존하려는 경우 다음과 같이 약간 단순화 할 수 있습니다.

git checkout has-changes
git reset --soft needs-changes
git commit
git checkout needs-changes
git cherry-pick has-changes

모든 것은 git reset --soft branch_bbranch_a를 기반으로 임시 브랜치의 상단으로 내려 가고 그 결과는 branch_b에 다시 커밋됩니다.

다음은 프로세스를 단계별로 안내합니다.

#Start out on the branch with the code we want
git checkout branch_a

#create tmp branch same as branch_a (so that we don't change our local branch_a state during the operation)
git branch tmp

#working directory has all the code that we want, on tmp branch
git checkout tmp

# Change the branch head to the branch we want to be on. All the delta
# between the current source code and branch_b is now staged for commit
git reset --soft branch_b

# Move away from tmp, so our commit will go directly to branch_b
git checkout branch_b

# Now you can examine the proposed commit
git status

# Add the delta we want to the branch
git commit

# Sanity check that the branches have the same content now (should return an empty line)
git diff branch_A..branch_b

# Remove tmp, we don't need it anymore
git branch -D tmp

참고 URL : https://stackoverflow.com/questions/17324645/git-create-commit-from-diff-between-two-branches

반응형