Nice programing

커밋 순서 변경

nicepro 2020. 10. 12. 08:11
반응형

커밋 순서 변경


현재 브랜치에서 작업 중이며 일부 커밋을 다른 브랜치에 병합하고 싶습니다.

    a-b-c-d-e-f-g (branchA)
   /
--o-x-x-x-x-x-x-x-x-x-x (master)
   \
    x-x-x-x-x (branchB)

(문자는 커밋을 나타내며 "x"는 관련없는 커밋입니다.)

그러나 일부 커밋을 풀링하는 것이 좋은 생각이라는 것을 알았습니다. 커밋 a, d, e 및 g를 하나의 패치로 "연결"하고 마스터에 커밋하고 싶습니다. 커밋 b와 f는 branchB에 대한 하나의 커밋으로 이동해야합니다. 그것을 달성하는 좋은 'git'-ish 방법이 있습니까?


찾고있는 명령은 git rebase특히 -i/--interactive옵션입니다.

나는 당신이 브랜치 A에 커밋 c를 남기고 싶다고 가정하고 병합이 간단하기 때문에 다른 커밋을 병합하는 대신 다른 브랜치로 옮기고 싶다는 의미입니다. 분기 A를 조작하여 시작하겠습니다.

git rebase -i <SHA1 of commit a>^ branchA

^이전 커밋 수단이 때문에,이 명령은 "A"로 기본 전에 커밋 사용하여 지점 A를 리베이스 말한다. Git은이 범위의 커밋 목록을 제공합니다. 순서를 바꾸고 git에게 적절한 항목을 스쿼시하도록 지시하십시오.

pick c ...
pick a ...
squash d ...
squash e ...
squash g ...
pick b
squash f

이제 히스토리는 다음과 같아야합니다.

    c - [a+d+e+g] - [b+f] (branchA)
   /
--o-x-x-x-x-x-x-x-x-x-x (master)

이제 branchB에 대해 새로 스 쿼싱 된 커밋 b + f를 가져옵니다.

git checkout branchB
git cherry-pick branchA  # cherry-pick one commit, the tip of branchA

마스터의 경우 a + d + e + g에 대해서도 동일합니다.

git checkout master
git cherry-pick branchA^

마지막으로 branchA를 업데이트하여 c를 가리 킵니다.

git branch -f branchA branchA^^

이제 다음이 필요합니다.

    c (branch A) - [a+d+e+g] - [b+f] (dangling commits)
   /
--o-x-x-x-x-x-x-x-x-x-x-[a+d+e+g] (master)
   \
    x-x-x-x-x-[b+f] (branchB)

분기간에 이동하려는 커밋이 여러 개있는 경우 rebase를 다시 사용할 수 있습니다 (비대화 형).

# create a temporary branch
git branch fromAtoB branchA
# move branchA back two commits
git branch -f branchA branchA~2
# rebase those two commits onto branchB
git rebase --onto branchB branchA fromAtoB
# merge (fast-forward) these into branchB
git checkout branchB
git merge fromAtoB
# clean up
git branch -d fromAtoB

마지막으로 고지 사항 : 일부가 더 이상 깔끔하게 적용되지 않도록 커밋을 재정렬하는 것이 가능합니다. 이것은 잘못된 순서를 선택했기 때문일 수 있습니다 (패치 된 기능을 소개하는 커밋 전에 패치를 배치). 이 경우 rebase ( git rebase --abort) 를 중단 할 수 있습니다. 그렇지 않으면 (병합 충돌과 마찬가지로) 지능적으로 충돌을 수정하고 수정 사항을 추가 한 다음 실행 git rebase --continue하여 계속 진행해야합니다. 이러한 지침은 충돌이 발생할 때 인쇄되는 오류 메시지에서도 제공됩니다.


마지막 두 커밋 만 재정렬 하려면 https://stackoverflow.com/a/33388211/338581git reorder 별칭 을 사용할 수 있습니다 .


git rebase --interactive 원하는 명령입니다.

예:

현재 상태는 다음과 같습니다.

bernt@le3180:~/src/stackoverflow/reordering_of_commits
$ git status
On branch master
nothing to commit, working tree clean
bernt@le3180:~/src/stackoverflow/reordering_of_commits
$ git log --oneline
a6e3c6a (HEAD -> master) Fourth commit
9a24b81 Third commit
7bdfb68 Second commit
186d1e0 First commit

I want to reorder commits 9a24b81 (Third commit) and 7bdfb68 (Second commit). To do this, I first find the commit before the first commit we want to change. This is commit 186d1e0 (First commit).

The command to execute is git rebase --interactive COMMIT-BEFORE-FIRST-COMMIT-WE-WANT-TO-CHANGE, in this case:

bernt@le3180:~/src/stackoverflow/reordering_of_commits
$ git rebase --interactive 186d1e0

This opens up a file in the default editor with the following contents:

pick 7bdfb68 Second commit
pick 9a24b81 Third commit
pick a6e3c6a Fourth commit

# Rebase 186d1e0..a6e3c6a onto 186d1e0 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#

Note that the ordering of commits in the file is opposite the one used for git log. In git log, the most recent commit is at the top. In this file, the most recent commit is at the bottom.

As the comment at the bottom of the file explains there are various things I can do, like squashing, dropping and reordering commits. To re-order commits, I edit the file so it looks like this (the bottom comments not displayed):

pick 9a24b81 Third commit
pick 7bdfb68 Second commit
pick a6e3c6a Fourth commit

The pick command at the start of each line means "use (i.e. include) this commit" and when I save the temp file with the rebase commands and exit the editor, git will execute the commands and update the repository and working directory:

$ git rebase --interactive 186d1e0
Successfully rebased and updated refs/heads/master.
bernt@le3180:~/src/stackoverflow/reordering_of_commits
$ git log --oneline
ba48fc9 (HEAD -> master) Fourth commit
119639c Second commit
9716581 Third commit
186d1e0 First commit

Note the rewritten commit history.

Links:


git rebase is what you want. Check out the --interactive option.


git rebase -i HEAD~3

Where 3 is the number of commits that need reordering (source).

This will open vi, listing commits from oldest (top) to newest (bottom).
Now reorder the lines, save, and exit the editor.

ddkP will move current line up (source)
ddp will move current line down

참고URL : https://stackoverflow.com/questions/2740537/reordering-of-commits

반응형