Reputation: 301
To work on an issue related to a feature currently under development, a collaborator on a project created a new issue-1 branch off of the feature branch using the GitHub Desktop application. Then they added a bunch of commits to the issue-1 branch. Something went wrong when the branch was created, and now the commit history doesn’t look right. The diagrams below should help explain what was expected and what actually happened.
0) Before the new branch:
A - B - C - D (feature)
1) Expected history:
E - F - G (issue-1)
/
A - B - C - D (feature)
2) Actual history:
A - B - C - D - E - F - G (issue-1)
^
(feature)
3) Adding commits to the feature branch yields a tree like this:
H - I (feature)
/
A - B - C - D - E - F - G (issue-1)
4) instead of like this (which is what we want):
E - F - G (issue-1)
/
A - B - C - D - H - I (feature)
In the history, the original commits A-B-C-D now appear to be associated with both the issue-1 branch and the feature branch. The commits E-F-G appear to be on only the issue-1 branch (which is correct).
I would like to get the repo from state 2 to state 1 (above). Since commits E-F-G are already on their own branch, I think all that needs to happen is that the earlier commits need to be dissociated from the issue-1 branch. What is the proper series of git commands to restore the original state of commits A-B-C-D to be on only the feature branch and not both feature and issue-1?
Apologies if this is a duplicate. I couldn’t find a solution to this problem, but I might have been searching for the wrong thing.
Upvotes: 2
Views: 765
Reputation: 16268
From graph theory point of view, snippets 3 and 4 are the same - they differ just in how you draw them visually. So, you're fine and nothing needs to be changed.
When you create new branch from an existing one, the new one automatically "inherits" all the commits from the existing one at the moment of creation - it is the way Git works.
Upvotes: 0
Reputation: 923
A branch in git is just a pointer to a particular commit in a repository's history. So when you created the (issue-1) branch off of (feature) branch, it was pointing to the same commit (D). As you continued to make commits, you were moving your (issue-1) pointer to reference those new commits (E), (F), and (G), while your feature branch remained pointing at commit (D).
When you checked out your repository to the feature branch (which was pointing to commit D) and continued to make new commits to that, you were moving the pointer of (feature) to commit (H) and (I). Both branches have the same history up until commit (D) which is where they diverged.
So in summary there is nothing wrong with your commit history and you should not need to revise it at all.
See https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell for more detail about git branches.
Upvotes: 2