Git merge, which branch merged in the other?

0

Lets say I have two branch like this:

o---o---o---o---o master
\
 o---o---o feature

If I run the following commands:

$ git checkout master
$ git merge feature

what will happen?

o---o---o---o---o master                     o---o---o---o---o master
\              /                or this       \               \   
 o---o---o---- feature                         o---o---o-------o feature  

NoNameProvided

Posted 2015-07-07T13:06:24.803

Reputation: 1 996

Answers

2

git merge branch_name merges branch_name into your current branch. In your case the result will be

o---o---o---o---o master
\              /
 o---o---o---- feature

And a merge commit will be added to master.

gronostaj

Posted 2015-07-07T13:06:24.803

Reputation: 33 047

2

The former, per the git-merge manual:

Description

Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch.

[...]

Assume the... current branch is "master"... Then "git merge topic" will replay the changes made on the topic branch since it diverged from master until its current commit on top of master, and record the result in a new commit along with the names of the two parent commits and a log message from the user describing the changes.

Or, preserving the lovely formatting of http://git-scm.com:

git merge info

bertieb

Posted 2015-07-07T13:06:24.803

Reputation: 6 181

1

The feature branch will be merged into master. You can test this out quickly in a temporary git repo.

> git init
> echo test > testfile
> cat testfile
test
> git add testfile
> git commit -m "First commit"
> git checkout -b feature
> echo test2 >> testfile
> cat testfile
test
test2
> git commit -am "Second commit"
> git checkout master
> cat testfile
test
> git merge feature
> cat testfile
test
test2

Kris Harper

Posted 2015-07-07T13:06:24.803

Reputation: 819

but if we create a branch feature2 and commit and push file testfile2 and later try to merge it with master it merges testfile of feature and testfile2 of feature2 same goes in push. May i know the reason or how to avoid it – insoftservice – 2019-03-01T11:13:35.753

@insoftservice Based on my example, all commits will include testfile since it was present in the first commit. – Kris Harper – 2019-03-01T14:10:49.503

thx for your comments. If u create new branch say feature2 with new file say testfile2 commit it and push in feature2. but when u merge it to master it merges testfile2 and even testfile – insoftservice – 2019-03-01T15:49:05.287

@insoftservice I'm not really sure what you're saying. Are you saying that master will have both testfile and testfile2? That's the expected behavior, since testfile was part of the first commit. – Kris Harper – 2019-03-01T19:55:35.370

yes testfile has to be present in master as it was part of first branch but in my case it shows that it is part of second branch also and if there is third commit with file3 it would show file,file2 and file3 as part of third branch that's my issue. Hope i was able to clarify my problem – insoftservice – 2019-03-02T19:41:26.880

Have created new question for the same If you could please show me my mistake i would really grateful to you .https://superuser.com/questions/1410441/git-merge-to-branch-merges-files-from-previous-merged-branches?noredirect=1#comment2127896_1410441

– insoftservice – 2019-03-02T19:46:24.840