简体   繁体   中英

How to merge two commits from different branch?

let say i have a commit 1 which is already present in branch A. I have a new commit 2 in branch B, i want to move this commit 2 to branch A and merge with commit 1.

(replace refs below with actual commit hashes and branch names)

to move commit-2 to branch branch-A :

git rebase --onto branch-A commit-2^ commit-2

to squash commit-2 into commit-1 :

  1. Use interactive rebase:

     git rebase -i commit-1 branch-A
  2. In the interactive rebase commit list, move the line for commit-2 so that it immediately follows commit-1 .

  3. On the line for commit-2 , replace the word pick with either squash or fixup . From the git help rebase :

    If you want to fold two or more commits into one, replace the command "pick" for the second and subsequent commits with "squash" or "fixup". If the commits had different authors, the folded commit will be attributed to the author of the first commit. The suggested commit message for the folded commit is the concatenation of the first commit's message with those identified by "squash" commands, omitting the messages of commits identified by "fixup" commands, unless "fixup -c" is used. In that case the suggested commit message is only the message of the "fixup -c" commit, and an editor is opened allowing you to edit the message. The contents (patch) of the "fixup -c" commit are still incorporated into the folded commit. If there is more than one "fixup -c" commit, the message from the final one is used. You can also use "fixup -C" to get the same behavior as "fixup -c" except without opening an editor.

  4. Save and close the rebase commit list. git rebase will now apply the commits in the order you saw in the list. You will have to deal with any merge conflicts.

git rebase -i is one of the most powerful and useful commands in git. Learn it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM