git forward merging a couple of commits only

38 Views Asked by At

I have a repo in which there are branches main (local and remote), develop (local and remote) and feature1 (only local)

Between the main and develop there are some commits, but nothing complicated only some commits ahead.

Obviously I can forward merge main with develop but I don't wish to do that

On the other hand, I want to merge some of the first commits separating develop from main into main.

I have always merged between branches, but can I merge also based on the commit ID?

Or should I go to the commit I want to merge, create a temporal branch there and forward main to that branch?

2

There are 2 best solutions below

0
Romain Valeri On BEST ANSWER

(Answering to that part specifically)

I have always merged between branches, but can I merge also based on the commit ID?

You can indeed merge commits, which is what git ultimately does behind the scenes when you merge a branch.

Let's say you have

-- A -- B -- C <<< main
              \
               D -- E -- F <<< develop

You can, from your main branch, git merge D (by its commit hash directly, but you could also point to it by creating a temp branch, a tag, using a relative ref, anything goes) and it would then fast-forward your main branch to

-- A -- B -- C -- D <<< main
                   \
                    E -- F <<< develop

And of course, at this point you might either want to merge F (which would implicitly merge E and F) or just E for the moment if needed.

0
Sha Md. Nayeem On

What I have understood is that you want to merge some commits from the develop branch into the main branch without merging all the changes from develop Right?

If this is the case then you can use the git cherry-pick command and follow the below process:

  1. From the develop branch, use the git log to view the commit history and find the commit IDs.
  2. Then, switch to the main branch using the git checkout main command
  3. Use this git cherry-pick <commit_ID> git command with the specific commit ID you want to merge from the develop branch to the main branch.
  4. If necessary, after cherry-picking all the necessary commits, you can push all the changes to the remote main branch.

You can find more essential git commands here.