0

How to make x-branch

feature/name          D - E - F
                  /
develop A - B - C 

look like this

feature/name      C - D - E - F
              /
develop A - B

and move C to be the first commit on y-branch

EDIT Sorry for the typo. Here is a brief scenario to what I need

Here is the situation, I forgot to create a new feature branch from the develop and commit changes to develop and then released that I am on develop so I created feature branch and continued the work. So I want to reset the develop branch and keep all the work on feature branch

2 Answers2

1

If I understand your diagram correctly, you want to move your develop to point to B instead of C.

To do this locally:

  1. git checkout B
  2. git branch -D develop
  3. git checkout -b develop

Of course, if you want to apply the same changes to a remote repo, you'll also need to delete the remote develop, (git push --delete) and recreate it (git push).

Update: minor note:

Judging from the comments and the other answer, it seems there's a misunderstanding of how git branches work. To put it simply the branch feature/name does NOT consist of commits D, E, and F. It consists of all 6 commits A, B, C, D, E, and F. See https://git-scm.com/book/en/v1/Git-Branching-What-a-Branch-Is or other examples for more thorough explanations.

rkta
  • 3,959
  • 7
  • 25
  • 37
junvar
  • 11,151
  • 2
  • 30
  • 46
  • How mistakes are contagious :-) Did *you* also make a typo? Because it would be y-branch to modify, x is not to change here... then again, question is awfully unclear, and OP does not bother to answer or clarify... – Romain Valeri Oct 17 '18 at 15:35
  • @MohamedAbuGalala Fair enough, now you're here :-) – Romain Valeri Oct 17 '18 at 16:04
  • No problem I post from mobile so it's hard a little – Mohamed Abu Galala Oct 17 '18 at 16:07
  • seems `y-branch` (top-branch in graph) is unchanged, and `x-branch` (bottom branch in graph) is modified. I recall pre-edit they were both labeled x-branch in the bottom graph, so I went by the labels of the top-graph. let me know if my interpretation of the graphs is incorrect. – junvar Oct 17 '18 at 16:12
  • Yea you did not understand the question well as I made a typo mistake. Sorry for that – Mohamed Abu Galala Oct 17 '18 at 16:15
  • after ur edit, my answer still holds. i've updated branch names to match ur new branch names for clarity, but the answer is the same. 1. Delete old develop branch, 2. checkout the last commit u want on ur develop branch, 3. create new develop branch – junvar Oct 17 '18 at 16:19
  • I don't want to touch history on develop branch. – Mohamed Abu Galala Oct 17 '18 at 16:24
  • 1
    u want to not exclude commit `C` from the develop branch. that's what this does. other commits will retain their same sha numbers and history is unchanged (other than removing C from develop branch.) See https://git-scm.com/book/en/v1/Git-Branching-What-a-Branch-Is for clarification. – junvar Oct 17 '18 at 17:09
  • This link help me more to understand how git works but I really don't understand how your solution helps me, but as you say and demonstrated in the link this means branch feature/name is consist of all commits. But this doesn't make any sense, cause I can push to master only branch feature/name and keep commit C on my local, so where commit C live if I clone the project again? It's not on develop sure but I can't find it on git log so how branch feature/name have commit C? – Mohamed Abu Galala Oct 17 '18 at 19:34
  • I will try this in a simple repo and inform if any updates – Mohamed Abu Galala Oct 17 '18 at 19:36
  • Hey, I tried both on a demo git repo with a readme.MD file only and both works correctly, but your solution is better because it solves the problem directly without any workarounds. Great thanks for both of you, I'm glad, this question made me understand Git better than I can imagine – Mohamed Abu Galala Oct 20 '18 at 00:36
1

What you can do is create a new branch feature/name2 from commit B from develop branch. Then copy all commits to it and delete the old feature/name branch.

See if this helps.

# Go to commit B
git checkout <B commit id>

# Create a new branch from Commit B
git checkout -b "feature/name2"

# Copy all commit (D to F) to this new branch.
git cherry-pick <C commit id>
git cherry-pick <D commit id>
git cherry-pick <E commit id>
git cherry-pick <F commit id>

#If you want you can delete the old branch using (use with caution)
git branch -D feature/name

# To delete commit C from develop branch
git checkout develop
git reset HEAD~     

From remote you can delete the old feature branch after pushing this new branch. You can rename this branch to feature/name also after deleting both local and remote feature/name branch.

Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33
  • Maybe this is the best answer I can think of. If there a better way will be great – Mohamed Abu Galala Oct 17 '18 at 16:49
  • Sure !! I would also love to know. – Rishabh Agarwal Oct 17 '18 at 16:50
  • But I think cherry-pick is copying the reference of the commit not a full copy I guess – Mohamed Abu Galala Oct 17 '18 at 16:51
  • It will copy the changes. Commit ids will be different of source and copied commit. – Rishabh Agarwal Oct 17 '18 at 16:52
  • https://stackoverflow.com/questions/9339429/what-does-cherry-picking-a-commit-with-git-mean – Rishabh Agarwal Oct 17 '18 at 16:52
  • how does this help? it does nothing but clone feature/name to feature/name2. They r still the same exact branch, and develop branch still includes commit C. – junvar Oct 17 '18 at 17:11
  • it will copy commit C in new branch feature/name2. now if you want you can delete commit C from develop branch – Rishabh Agarwal Oct 17 '18 at 17:14
  • commit C is already in feature/name. How r feature/name2 and feature/name any different? why couldn't u just delete commit C from develop branch before u created the clone branch feature/name2? – junvar Oct 17 '18 at 18:11
  • In feature/name Commit C is common between development and feature branch. In feature/name2 Commit B is common between development and feature branch. – Rishabh Agarwal Oct 17 '18 at 18:33
  • commit B is in all 3 branches, feature/name, feature/name2, and develop. commit C is in both feature branches, but not in the develop branch. so my question still stands, why did u clone feature/name renamed to feature/name2? just because because u didn't cherry-pick B into feature/name2 doesn't mean it's excluded from feature/name2. – junvar Oct 17 '18 at 18:46
  • I will try this in a simple repo and inform if any updates – Mohamed Abu Galala Oct 17 '18 at 19:35
  • @MohamedAbuGalala did you try this out ? – Rishabh Agarwal Oct 19 '18 at 15:42
  • Hey, I tried both on a demo git repo with a readme.MD file only and both works correctly, but @junvar solution is better because it solves the problem directly without any workarounds. Great thanks for both of you, I'm glad, this question made me understand Git better than I can imagine – Mohamed Abu Galala Oct 20 '18 at 00:36