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  
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.

| improve this answer | |
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

| improve this answer | |
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
| improve this answer | |
  • 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 1 '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 1 '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 1 '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 1 '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 2 '19 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.