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
  • 2,370
  • 3
  • 16
  • 27

3 Answers3

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
  • 7,344
  • 36
  • 42
  • 54
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
  • 55,965
  • 20
  • 120
  • 179
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
  • 1,047
  • 3
  • 11
  • 24
  • 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 Mar 01 '19 at 11:13
  • @insoftservice Based on my example, all commits will include `testfile` since it was present in the first commit. – Kris Harper Mar 01 '19 at 14:10
  • 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 Mar 01 '19 at 15:49
  • @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 Mar 01 '19 at 19:55
  • 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 Mar 02 '19 at 19:41
  • 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 Mar 02 '19 at 19:46