簡體   English   中英

Git合並而不刪除當前分支中的文件

[英]Git merge without deleting files in current branch

我是git的新手,學習如何使用它將其集成到我的工作流程中。 當前,這是我的分支模型(基於此站點): https : //nvie.com/posts/a-successful-git-branching-model/

兩個永久分支:代表當前生產分支的主分支。 一個dev分支,我想它的名字說得很好:-)

我使用修補程序分支來解決問題,該修補程序是從母版創建的。 所以:我想修復此修補程序分支上的錯誤,並將其合並到master和dev中,以使用修復程序更新我的永久分支(Master和Dev)。

問題是:在Dev分支上,我創建了僅在此分支上的新文件(尚未在master上)。 跟蹤這些文件是因為在此分支上進行了提交以包括這些新文件。 當我將Hotfix合並到dev中時,合並會刪除Dev上不在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,所有不在修補程序上的文件都被刪除。 如果我不進行快進合並,也是如此...也許我做錯了什么?

我試圖將文件放在.gitignore中,ti在這種情況下不執行任何操作。

預先感謝 !

您的假設是不正確的。 當合並一個不了解文件的分支時,不會刪除在一個分支中引入的文件。 合並將合並更改,並且不會覆蓋它們。

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

最后,您的master分支將包含文件file_only_on_masterfix (以及以前可能存在的其他文件)。

  1. 未創建您的合並提交,因為它是快速轉發的。
A -- B -- C

假設您的主服務器位於A且您的修補程序位於C。如果您在master分支上git merge hotfix ,它將選擇將主服務器快速轉發到C而不是創建新的提交。

  1. 因此,您需要強制git首先創建合並提交。

    合並修補程序以強制合並提交創建時,可以添加--no-ff參數。 之后,分支將如下所示:

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

...但是文件仍將被刪除。

  1. 您可以通過強制退出編輯器來修改合並提交。

    因此,您想修改合並提交M,而不是接受git的選擇。 為此,您可以崩潰退出合並編輯器-對於vim,它是:cq

    如果您異常退出編輯器,則HEAD將位於A處,但B,C處的更改將應用​​並分段。

    因此,您可以git reset ,然后git checkout它們,例如,在您的情況下git reset TG_anc/ && git checkout TG_anc/

    之后,您可以git merge --continue來表示git,您已經完成修改並願意繼續合並過程。

例:

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