暂时禁用分支中的功能

时间:2019-04-25 13:36:27

标签: git

我遇到这样的情况,我需要在分支中停用某个功能,对其进行调整,然后在以后再次激活。

我的初始情况:

$ git log
14c9dcd (HEAD -> master) feature 3
af64271 feature 2
da0507a feature 1
  • 14c9dcd-创建的file3.txt
  • af64271-编辑了file1.txt;创建的file2.txt
  • da0507a-添加file1.txt

现在,我需要恢复af64271分支上的master提交,并使代码更改可用,以便稍后取回(进行一些更改)。

我试图做:

$ git checkout -b save_feature_2
    $ git checkout master
    $ git log --oneline   
        14c9dcd (HEAD -> master, save_feature_2) feature 3
        af64271 feature 2
        da0507a feature 1

#Reverting the "feature 2" commit changes
$ git revert af64271
    [master 8fcee28] Revert "feature 2"
    2 files changed, 1 insertion(+), 2 deletions(-)
     delete mode 100644 file2.txt

# Checking the differences
$ git diff save_feature_2
    diff --git a/file1.txt b/file1.txt
    index ba36cc9..8e90503 100644
    --- a/file1.txt
    +++ b/file1.txt
    @@ -1,2 +1 @@
    -<somecode here>
    -<more code here>
    \ No newline at end of file
    +<somecode here>
    \ No newline at end of file
    diff --git a/file2.txt b/file2.txt
    deleted file mode 100644
    index e69de29..0000000

# Checking log on master
$ git log --oneline
    8fcee28 (HEAD -> master) Revert "feature 2"
    14c9dcd (save_feature_2) feature 3
    af64271 feature 2
    da0507a feature 1

# Checking the save_feature_2 log branch
$ git checkout save_feature_2
$ git log --oneline
    14c9dcd (HEAD -> save_feature_2) feature 3
    af64271 feature 2
    da0507a feature 1

# Making same improviments on "feature 2" addind some code just on "file3.txt"
$ git log --oneline
    c4ff7f2 (HEAD -> save_feature_2) improvements on feature 2
    14c9dcd feature 3
    af64271 feature 2
    da0507a feature 1

# Now i need to reactivate the "feature 2" on master
$ git checkout master
$ git diff save_feature_2
    diff --git a/file1.txt b/file1.txt
    index ba36cc9..8e90503 100644
    --- a/file1.txt
    +++ b/file1.txt
    @@ -1,2 +1 @@
    -<somecode here>
    -<more code here>
    \ No newline at end of file
    +<somecode here>
    \ No newline at end of file
    diff --git a/file2.txt b/file2.txt
    deleted file mode 100644
    index e69de29..0000000
    diff --git a/file3.txt b/file3.txt
    index f70ef26..e69de29 100644
    --- a/file3.txt
    +++ b/file3.txt
    @@ -1 +0,0 @@
    -<improvements on feature 2>
    \ No newline at end of file

$ git merge save_feature_2
    Merge made by the 'recursive' strategy.
     file3.txt | 1 +
     1 file changed, 1 insertion(+)

$ git log --oneline
b10eabc (HEAD -> master) Merge branch 'save_feature_2' - Reactivating the feature 2
c4ff7f2 (save_feature_2) improvements on feature 2
8fcee28 Revert "feature 2"
14c9dcd feature 3
af64271 feature 2
da0507a feature 1

# At this point the "file2.txt" was no merged!

$ ls (master)
file1.txt  file3.txt
$ git checkout save_feature_2
$ ls (save_feature_2)
file1.txt  file2.txt  file3.txt

您可以看到file2.txt没有合并!

1 个答案:

答案 0 :(得分:0)

分支save_feature_2应包含 revert 提交,另外还应包含 revert提交的恢复。

当您将此分支合并回master时,将再次激活该功能。

更具体地说:

  1. 最初,您的分支master的提交为FEATUREFEATURE feature 提交的哈希值)。
  2. 您要还原FEATURE并在REVERT_OF_FEATURE分支上创建master(这是git revert FEATURE生成的提交的哈希值)
  3. 您创建一个名为FEATURE_BRANCH的分支。 (git co -b FEATURE_BRANCH)并在此新分支上,还原还原提交git revert REVERT_OF_FEATURE
  4. 您继续使用master(该功能现在不存在)和FEATURE_BRANCH(此处又存在该功能,您可以继续进行该工作)
  5. 最后,您将FEATURE_BRANCH合并回master,并获得了原始功能和master上的其他更改。

您遇到的问题是,当将分支save_feature_2合并回master时,发生的情况是save_feature_2分支中所有已提交的更改(尚未)已更改包含在目标分支(master)上。但是原始的feature提交已经存在,因此不包括那些更改。但是,如果在该功能分支上包含 revert commit revert commit ,您将实现所需的功能。

如果您想按现状解决问题,只需将masterFEATURE_BRANCH合并(在那里将获得REVERT_OF_FEATURE提交),然后在此处还原{ {1}}提交。

尽管如此,我还是觉得功能标记很不错,就像@crashmstr在评论中已经提到的那样。