0

I've changed two files on my local repo, in a new branch (I'll call it "new_branch"). I've commit the changes and pushed to remote repo. Then, I wanted to merge theses changes into master branch. And I've done:

$ git checkout master
$ git merge new_branch

But not only the two files are added to master, but other configuration files have been added too. I have just done:

$ git add file1
$ git add file2
$ git commit -m "changes file1 and file2"
$ git push -u origin --all

Why are the other files added too? and how I undo this merge?

This is the output of merge:

Updating b57febc..5a967d5
Fast-forward
.gitignore                                       |    6 +-
app/config/parameters.yml                        |    2 +-
src/MSD/HomeBundle/Controller/HomeController.php |  421 +++++++++++-----------
src/MSD/HomeBundle/Entity/Imagen.php             |  298 +++++++++++++++
web/bundles/msdhome/js/acercade.js               |    2 +-
5 files changed, 506 insertions(+), 223 deletions(-)
create mode 100644 src/MSD/HomeBundle/Entity/Imagen.php

I just want to change:

src/MSD/HomeBundle/Controller/HomeController.php
src/MSD/HomeBundle/Entity/Imagen.php  

"git log --graph --decorate --pretty=oneline --abbrev-commit --all" output:

* 5a967d5 (HEAD, origin/mejoras_contralador, mejoras_contralador, master) Controlador mo
* 081224a Cambios en controlador y clase Imagen
* a74e337 añadir directorio vendor a .gitignore
* 42d3217 Primer commit
* b57febc (origin/v1, origin/master, origin/HEAD) version 1 definitiva
* 1d1c5f7 solucionado error de js en botones social media
* 2f40866 primer commit

Seems that .gitignore was added too (but not app/config/parameters.yml)

Manolo
  • 24,020
  • 20
  • 85
  • 130

2 Answers2

0

Haven't the changes to the configuration files been initially changed in new_branch, but in a commit older than its last one? Because merge applies every change since the divergence.

If you only want to apply the changes of 1 commit (like the last one for instance), you would want

git cherry-pick new_branch

And by the way, versioning configuration files is discouraged specifically for this reason. Or have a versioned one, but also a local, unversioned one that would be merged with the versioned one in your code so you can have default config values and also be able to override them with your local, unversioned configuration.

Attila Szeremi
  • 5,235
  • 5
  • 40
  • 64
0

The other files were likely added in a previous commit one the branch.

Provided that you have not pushed your master branch you can easily undo the merge with git reset --hard <sha of last commit on master> To find that commit, if you look at the merge commit message you will see:

Merge: 1234556 5423456

This shows the two latest commits in the branches that were merged. One is from master, and the other would be from new_branch. Reset your branch to the last commit on master.

WARNING This is changing history and should only be done if you have not pushed your branch.

If you have pushed the branch already then you will need to git revert HEAD~1 -m 1 per http://gitready.com/intermediate/2009/03/16/rolling-back-changes-with-revert.html

If you only want certain files from new_branch you have a couple of options:

1) You can git cherry-pick the commit. Please keep in mind that this will bring the entire commit. So if changes to files were made across multiple commits, you will only get what was changed in the commit that you are cherry-picking.

2) You can checkout the files from the other branch vie git checkout <branch-name> -- <files> then commit them in your branch. This is basically the same thing that cherry-pick is doing but you can then take only some of the files that were modified in a commit and you are being more selective about your changes.

Schleis
  • 41,516
  • 7
  • 68
  • 87