Search code examples
javagit

Is Git Smart Enough to Merge After Refactoring


Assume I have a class.

package org.my.domain;

public class BestClassEver{}

My Workflow

Through some refactoring, I change this class's package.

package org.another.domain;

public class BestClassEver{}

I commit this to my local repository using git and push it to a remote repository.

git add .
git commit -m "Refactoring"
git push origin master

Another Developer's Workflow

Another developer modifies the class, without pulling my changes.

package org.my.domain;

public class BestClassEver{
    private String something;
}

Then commits and pushes to the remote repository

git add .
git commit -m "Some Changes"
git push origin master

Questions

  1. Will Git merge the other developer's changes into the class?
  2. If not, what happens?
  3. Is this workflow something that needs to be coordinated amongst the team?

Solution

    1. Git won't allow the other developer to push his changes without pulling.

    It will throw an error that both refs don't match and therefore his local branch needs to be updated with the remote refs.

    That's pretty much all there is to know about that. If there are changes in the remote repository, unless you do a forced push, git won't allow you to push changes if there are changes in the remote.

    EDIT

    Once he pulls, if there are any conflicts in the file, the developer will have to correct any conflicts, commit them and only then he will be able to push.

    If there are no conflicts, git will automatically merge those changes and the developer will be able to push after the pull.

    EDIT AGAIN

    I didn't realize that you were moving the file. In any case, running git status would give you an idea as to the state of your local repository after a pull. If there was any conflicts, you'd be able to correct them.

    NOTE

    On git rebase or git pull --rebase are usually used to give you a much cleaner commit history as they will pretty much will apply any local changes on top of any other changes that were pulled.

    On the other hand, git pull and git merge will usually make an extra commit to link the changes on the branches.

    For more information take a look at this guide Git Rebasing