简体   繁体   English

Git合并而不删除当前分支中的文件

[英]Git merge without deleting files in current branch

I'm new to git and learning how to use it to integrate it in my workflow. 我是git的新手,学习如何使用它将其集成到我的工作流程中。 Currently, here is my branching model ( based on this site ): https://nvie.com/posts/a-successful-git-branching-model/ 当前,这是我的分支模型(基于此站点): https : //nvie.com/posts/a-successful-git-branching-model/

Two permanent branches : A master branch that represents the current production branch. 两个永久分支:代表当前生产分支的主分支。 A dev branch, well I think its name speak well :-) 一个dev分支,我想它的名字说得很好:-)

I use hotfix branche to solve issues, hotfix is created from master. 我使用修补程序分支来解决问题,该修补程序是从母版创建的。 So : I want to fix the bug on the hotfix branch, and merge it into master and into dev to have my permanent branches (Master and Dev) updated with the fix. 所以:我想修复此修补程序分支上的错误,并将其合并到master和dev中,以使用修复程序更新我的永久分支(Master和Dev)。

Problem is : on Dev branch, I create new files that are only on this branch (and not yet on master). 问题是:在Dev分支上,我创建了仅在此分支上的新文件(尚未在master上)。 These files are tracked because commits are made on this branch to include these new files. 跟踪这些文件是因为在此分支上进行了提交以包括这些新文件。 When I merge Hotfix into dev, the merge delete the new files on Dev that weren't on Hotfix... 当我将Hotfix合并到dev中时,合并会删除Dev上不在Hotfix上的新文件...

So my question : Is there a way to merge without deleting files in Dev that are not yet on Hotfix ? 所以我的问题是:有没有一种方法可以合并而不删除尚未在Hotfix上的Dev文件?

git status
On branch hotfix
nothing to commit, working tree clean



git checkout dev
Switched to branch 'dev'
Your branch is behind 'origin/dev' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)


git merge hotfix
Updating b34835d..458a2cf
Fast-forward
  98 files changed, 1 insertion(+), 47534 deletions(-)


delete mode 100644 TG_anc/.htaccess
 delete mode 100644 TG_anc/Controleur.php
 delete mode 100644 TG_anc/Controleur/Controleur_Ajax.php
 delete mode 100644 TG_anc/Controleur/Controleur_Avertissements.php
 delete mode 100644 TG_anc/Controleur/Controleur_Commande.php
 delete mode 100644 TG_anc/Controleur/Controleur_Intervenants.php
 delete mode 100644 TG_anc/Model/Model.php

..etc, all my files that are not on hotfix are deleted. ..etc,所有不在修补程序上的文件都被删除。 It's the same if I make a no fast forward merge... Maybe I am doing something wrong ? 如果我不进行快进合并,也是如此...也许我做错了什么?

I have tried to put the files in the .gitignore, ti does nothing for this case. 我试图将文件放在.gitignore中,ti在这种情况下不执行任何操作。

Thank in advance ! 预先感谢 !

Your assumption is incorrect. 您的假设是不正确的。 Files introduced in one branch will not be deleted when merging another branch that does not have knowledge of the files. 当合并一个不了解文件的分支时,不会删除在一个分支中引入的文件。 Merging merges changes, and is not overwriting them. 合并将合并更改,并且不会覆盖它们。

git checkout master
git branch hotfix
>file_only_on_master echo jska13
git add file_only_on_master
git commit -m 'adding file to master'
git checkout hotfix
>fix echo 'fix me'
git add fix
git commit -m 'applied very import hotfix'
git checkout master
git merge hotfix

In the end, your master branch will contain both files file_only_on_master and fix (and possibly others which have existed before). 最后,您的master分支将包含文件file_only_on_masterfix (以及以前可能存在的其他文件)。

  1. Your merge commit wasn't created because it is fast-forwarded. 未创建您的合并提交,因为它是快速转发的。
A -- B -- C

Let's say your master was at A and your hotfix was at C. if you git merge hotfix on master branch, it would choose to fast-forward master to C instead of creating a new commit. 假设您的主服务器位于A且您的修补程序位于C。如果您在master分支上git merge hotfix ,它将选择将主服务器快速转发到C而不是创建新的提交。

  1. So you need to force git to create merge commit first. 因此,您需要强制git首先创建合并提交。

    You can add --no-ff parameter when merging hotfix to force merge commit creation. 合并修补程序以强制合并提交创建时,可以添加--no-ff参数。 After that, the branch would look like this: 之后,分支将如下所示:

  /-- -- -- M  <-master
 /         /
A -- B -- C    <-hotfix

... but the files would be still deleted. ...但是文件仍将被删除。

  1. You can modify merge commit by force-quitting editor. 您可以通过强制退出编辑器来修改合并提交。

    So you want to modify your merge commit M, instead of accepting git's selection. 因此,您想修改合并提交M,而不是接受git的选择。 To achieve this, you can crash-exit your merge editor - in case of vim, it's :cq . 为此,您可以崩溃退出合并编辑器-对于vim,它是:cq

    If you quit the editor abnormally, the HEAD would be at A but the changes from B, C are applied&staged. 如果您异常退出编辑器,则HEAD将位于A处,但B,C处的更改将应用​​并分段。

    So you can git reset and then git checkout them, for example in your case git reset TG_anc/ && git checkout TG_anc/ . 因此,您可以git reset ,然后git checkout它们,例如,在您的情况下git reset TG_anc/ && git checkout TG_anc/

    After that you can git merge --continue to indicate git that you are done with modification willing to continue merge process. 之后,您可以git merge --continue来表示git,您已经完成修改并愿意继续合并过程。

Example: 例:

SHELL$ git init

SHELL$ touch ./a

SHELL$ git add ./a

SHELL$ git commit -m "A"
[master (root-commit) 63dd4c0] A
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a

SHELL$ git checkout -b hotfix
Switched to a new branch 'hotfix'

SHELL$ git rm a
rm 'a'

SHELL$ git commit -m "B"
[hotfix 105e68f] B
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 a

SHELL$ git checkout master  
Switched to branch 'master'

# Vim editor will show up after this, crash-exit by esc - :cq .
SHELL$ git merge hotfix  --no-ff
Removing a
hint: Waiting for your editor to close the file... error: There was a problem with the editor 'vi'.
Not committing merge; use 'git commit' to complete the merge.

SHELL$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
    deleted:    a

SHELL$ git reset -- a && git checkout -- a
Unstaged changes after reset:
D   a

SHELL$ git merge --continue
[master b769fdf] Merge branch 'hotfix'

SHELL$ git log --oneline --graph 
*   b769fdf (HEAD -> master) Merge branch 'hotfix'
|\  
| * 105e68f (hotfix) B
|/  
* 63dd4c0 A

SHELL$ ls
a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM