Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does git merge reapply commits onto another commit?

I'm 15 and have just started using source control systems to manage my code a little better, so I'm a little new to this stuff. Right now I have this repository:

[a]---[b]---[d]---[f] master
        \
         \
          [c]---[e]   develop

I want to wind up here:

[a]---[b]---[d]---[f]---[g]  master
        \               /
         \             /
          [c]---[e]---/      develop

where g is equivalent to performing commits [c] and [e] on [f]. This is git checkout master; git merge develop, right?

like image 557
wxyz Avatar asked Jun 12 '10 17:06

wxyz


People also ask

Does merging create a new commit?

Merge branchesGit creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

Does git merge Keep commits?

The old commits will exist until they are garbage collected, but will not be reachable unless you look at the reflog.) Executing git checkout master; git merge --ff-only branch-b will now fast-forward your changes into master, thereby giving you a linear history.

Does squash and merge create a new commit?

Squash merging is a merge option that allows you to condense the Git history of topic branches when you complete a pull request. Instead of each commit on the topic branch being added to the history of the default branch, a squash merge adds all the file changes to a single new commit on the default branch.

What happens when you revert a merge commit?

REVERTING THE MERGE COMMIT To revert the changes brought in by the feature branch, revert the commit with respect to the second parent (1484b1a). This will revert all the changes made by the second branch (feature) on master. The resulting tree will behave as the branch feature was never merged to master.


2 Answers

The picture you drew looks like the result of git merge, but your description of what you want to happen sounds like git rebase.

Borrowing from the "Rebasing" chapter of the git community-book, if you're on the mywork branch and say

$ git merge origin

you'll get

git merge history

Think of a merge as cramming together two different snapshots: in this case C4 and C6 came together to make C7.

Rebase creates a tree that looks just like C7, but its history looks completely different. Say instead of the merge, you gave the command

$ git rebase origin

you'd get

alt text

When you find yourself wishing, 'I really wish I'd created the mywork branch at C4 instead of C2,' git rebase is the djinni that will grant it.

You could also think of rebase as cutting a branch out of your history and transplanting it on a different point. Don't miss the subtle change from C5 and C6 to C5' and C6'. Although the trees will look the same, they'll have different parents, thus changing their git identities.

like image 182
Greg Bacon Avatar answered Sep 19 '22 17:09

Greg Bacon


You want git checkout master; git merge develop.

This is because git merge takes the name of a branch that you want to merge into the currently checked out branch. Thus, you check out your desired destination branch (master) and then merge the other branch into it (develop).

If you take a look in the first "Description" section on the git-merge man page, you'll see a diagram almost identical to yours (albeit flipped vertically) that describes this.

like image 24
Amber Avatar answered Sep 23 '22 17:09

Amber