我遇到这样的情况,我需要在分支中停用某个功能,对其进行调整,然后在以后再次激活。
我的初始情况:
$ git log
14c9dcd (HEAD -> master) feature 3
af64271 feature 2
da0507a feature 1
现在,我需要恢复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没有合并!
答案 0 :(得分:0)
分支save_feature_2
应包含 revert 提交,另外还应包含 revert提交的恢复。
当您将此分支合并回master
时,将再次激活该功能。
更具体地说:
master
的提交为FEATURE
(FEATURE
是 feature 提交的哈希值)。FEATURE
并在REVERT_OF_FEATURE
分支上创建master
(这是git revert FEATURE
生成的提交的哈希值)FEATURE_BRANCH
的分支。 (git co -b FEATURE_BRANCH
)并在此新分支上,还原还原提交(git revert REVERT_OF_FEATURE
)master
(该功能现在不存在)和FEATURE_BRANCH
(此处又存在该功能,您可以继续进行该工作)FEATURE_BRANCH
合并回master
,并获得了原始功能和master
上的其他更改。您遇到的问题是,当将分支save_feature_2
合并回master时,发生的情况是save_feature_2
分支中所有已提交的更改(尚未)已更改包含在目标分支(master
)上。但是原始的feature
提交已经存在,因此不包括那些更改。但是,如果在该功能分支上包含 revert commit 和 revert commit ,您将实现所需的功能。
如果您想按现状解决问题,只需将master
与FEATURE_BRANCH
合并(在那里将获得REVERT_OF_FEATURE
提交),然后在此处还原{ {1}}提交。
尽管如此,我还是觉得功能标记很不错,就像@crashmstr在评论中已经提到的那样。