A branch in Git is a label that can be freely moved. It points to a commit. Each commit points to its parent commit(s). When you create a branch and don't specify its starting point, it will be created pointing to HEAD
, which is your currently checked-out commit/branch.
Assuming you already had commits made commits while working on your current branch: when you create a new branch, these commits are also reachable from the new branch. As long as they are not merged into your target branch, they will show up as "commits to be merged" (in GitHub PRs or with gitk target..yourbranch
).
Visualized:
A-B-C master, origin/master
\
D-E-F old-branch
\
G new-branch
What's the way out? You can either re-create the branch and its changes on top of the target branch (probably origin/master
) or you can rebase (which will rewrite history, make sure you understand the implications before doing this):
git rebase --onto origin/master oldbranch newbranch
Your commit graph after rebasing:
G' new-branch
/
A-B-C master, origin/master
\
D-E-F old-branch
The old commit from new-branch (G
) still exists, but is no longer reachable via named refs (branches or tags). G'
is a new, independent commit (which can be seen from its commit id/hash) with the same (or very similar) patch as the old G
(i.e. git diff "G'^" "G'"
is the same as git diff G^ G
).
Any commits reachable from old-branch
or master
will not be affected by the rebase-operations. (Remember: Git commits are immutable and can never change. You could only make them unreachable).