Rubin Yoo
Rubin Yoo

Reputation: 2720

How does git merging work?

I want to understand what git looks for when merging with two different code.

When does it delete code or insert

So if I have code

Foo.java (this is my code)

class Foo {
     void hello(){}
     void bye(){}
     void gone(){}
}

Foo.java (the code being fetched and merged)

class Foo{
     void hello(){}
     void wait(){}
     void bye(){}
}

If I merge the above code, will this

  1. Does it delete gone and bye method?
  2. If there was changes were made inside hello method, does it delete my 'hello' and rewrites it with the fetched hello?

Upvotes: 0

Views: 217

Answers (1)

Maccath
Maccath

Reputation: 3956

Firstly, it won't delete bye, it will insert wait() above it. It won't delete gone() unless it existed in the previous revision and had been deleted in the successive revision without you having touched it in between. If you had added gone() yourself (i.e. git received no indication that this should be deleted), then gone() will be found at the bottom of the merged file. Like this:

class Foo{
     void hello(){}
     void wait(){}
     void bye(){}
     void gone(){}
}

Secondly, it wouldn't delete your hello, it would just put the changes made into the hello() that already exists.

Remember, if it can't be merged, it will throw a conflict, and you will have to merge it yourself. It doesn't just delete/add things that don't make sense without human input.

Upvotes: 3

Related Questions