changeset 126:fa4a1d038ad8

Moving change from the middle of branch to top of parent branch in Git.
author Oleksandr Gavenko <gavenkoa@gmail.com>
date Thu, 13 Apr 2017 01:16:30 +0300
parents c110ed1b22ec
children 9d8a9871dd51
files 066f2b1f-f14d-4613-8719-bb5828a8b89a/index.rst
diffstat 1 files changed, 109 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/066f2b1f-f14d-4613-8719-bb5828a8b89a/index.rst	Thu Apr 13 01:16:30 2017 +0300
@@ -0,0 +1,109 @@
+
+Moving change from the middle of branch to top of parent branch in Git
+======================================================================
+:created: 2017-04-13
+:updated: 2017-04-13
+:tags: git
+
+During development in feature-branch I made change that is unrelated to feature and I want to
+integrate it before feature will be completed.
+
+Suppose you have change ``F`` in the middle of development branch that you want to publish
+(say on ``master``)::
+
+  master          dev
+    |              |
+    v              v
+    X -> A -> F -> B
+
+As always in rewriting history let's assume that changes within ``master..dev`` is not
+published.
+
+First strategy is to move ``F`` to top of ``dev`` with::
+
+  $ git co dev
+  $ EDITOR=emacs  git rebase -i F^1
+
+where you should rearrange ``pick`` lines in text editor. Move first line, that represent
+``fix`` because of the way we specify revisions: ``fix^1``, to the end, save editing and
+exit editor. Now we should have::
+
+  master          dev
+    |              |
+    v              v
+    X -> A -> B -> F
+
+Create named reference to ``F`` and move ``dev`` behind ``F``::
+
+  $ git branch fix
+  $ git reset --hard fix^1
+
+Now we have::
+
+  master     dev  fix
+    |         |    |
+    v         v    v
+    X -> A -> B -> F
+
+Now with ``--onto`` option we can split linear history into two branches::
+
+  $ git co master
+  $ git rebase --onto master dev fix
+
+After that we would have::
+
+  master fix
+    |     |
+    v     v   dev
+    X  -> F    |
+      \        v
+       -> A -> B
+
+Fast-forward ``master`` on top of ``fix``::
+
+  $ git co master
+  $ git merge fix
+
+We would have::
+
+  master fix
+       | |
+       v v  dev
+  X  -> F    |
+    \        v
+     -> A -> B
+
+Get rid of temporary name ``fix`` and rebase ``dev`` on top of ``F``::
+
+  $ git branch -d fix
+  $ git co dev
+  $ git rebase master
+
+====
+
+Alternative way is to move ``F`` to the bottom and move ``master`` pointer::
+
+  $ git co dev
+  $ EDITOR=emacs  git rebase -i master
+
+After rearranging ``pick`` lines in text editor by moving ``F`` to the top ``F`` will be
+at the bottom of changes::
+
+  master          dev
+    |              |
+    v              v
+    X -> F -> A -> B
+
+Fast-forward ``master`` to ``F``::
+
+  $ git co master
+  $ git merge F
+
+That's all!
+
+====
+
+Finally we can publish our fix::
+
+  $ git push origin master
+