0

我在本地仓库中更改了两个文件,位于一个新分支中(我将其称为“new_branch”)。我已提交更改并推送到远程仓库。然后,我想将这些更改合并到主分支中。我已经完成了:

$ git checkout master
$ git merge new_branch

但是不仅这两个文件被添加到 master 中,而且还添加了其他配置文件。我刚刚做了:

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

为什么还要添加其他文件?以及如何撤消此合并?

这是合并的输出:

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

我只想改变:

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

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

* 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

似乎也添加了.gitignore(但不是app/config/parameters.yml)

4

2 回答 2

0

配置文件的更改最初不是在 中更改new_branch,而是在比上一次更旧的提交中更改?因为合并应用了自分歧以来的每一个变化。

如果您只想应用 1 次提交的更改(例如最后一次),您会想要

git cherry-pick new_branch

顺便说一句,出于这个原因,特别不鼓励版本控制配置文件。或者有一个版本化的,但也有一个本地的,未版本化的,它将与代码中的版本化的合并,这样您就可以拥有默认配置值,也可以用您的本地、未版本化的配置覆盖它们。

于 2013-09-10T13:20:51.033 回答
0

其他文件可能是在先前提交的分支中添加的。

假设您没有推送您的主分支,您可以轻松地撤消合并。git reset --hard <sha of last commit on master> 要找到该提交,如果您查看合并提交消息,您将看到:

合并:1234556 5423456

这显示了合并的分支中的两个最新提交。一个来自 master,另一个来自 new_branch。将您的分支重置为 master 上的最后一次提交。

警告 这是改变历史,只有在你没有推送你的分支时才应该这样做。

如果您已经推送了分支,那么您需要git revert HEAD~1 -m 1按照 http://gitready.com/intermediate/2009/03/16/rolling-back-changes-with-revert.html

如果您只想要 new_branch 中的某些文件,您有几个选择:

1)你可以git cherry-pick提交。请记住,这将带来整个提交。因此,如果在多个提交中对文件进行了更改,您将只会得到您正在挑选的提交中更改的内容。

2)您可以从另一个分支中签出文件,git checkout <branch-name> -- <files>然后将它们提交到您的分支中。这基本上与cherry-pick正在做的事情相同,但是您只能获取在提交中修改的一些文件,并且您对更改更有选择性。

于 2013-09-10T13:23:37.917 回答