I created a branch in git to work on my feature (branch called my_branch). I pushed my changes to the branch and then rebased the branch onto master. My company uses git rebase and not git merge.
I ran the following commands:
git checkout master
git rebase master my_branch
git checkout master (seems like I somehow got switched to my_branch so had to switch back to master)
git merge --ff-only my_branch
git log (made sure my code is now in master)
git push origin master
When I try to clean up and delete my local branch, it gives a warning that it's not yet merged to refs/remote/origin/my_branch.
# git branch -d my_branch
warning: not deleting branch 'my_branch' that is not yet merged to
'refs/remotes/origin/my_branch', even though it is merged to HEAD.
error: The branch 'my_branch' is not fully merged.
If you are sure you want to delete it, run 'git branch -D my_branch'.
(please forgive any typos from sanitizing the above message)
When I check our git repo thru my browser and check the branches, I can see my latest code/check-in in "my_branch". I can also see my code in the master branch too.
Why is git complaining that "my_branch" is not fully merged? Can I delete my local branch safely or is git pointing to changes in "my_branch" that will disappear once I delete it locally? If I delete the branch remotely too, will that affect anything?
CodePudding user response:
You're missing a step in the process: after you rebase your topic branch (my_branch
), you need to force-push it to update it on your remote.
- git checkout master
- git rebase master my_branch
- it's the expected behavior that this checks out
my_branch
in order to perform the rebase
- it's the expected behavior that this checks out
- git push --force-with-lease origin my_branch
- rebasing rewrites the history of
my_branch
, so pushing it requires a force push - I edited my answer to make this --force-with-lease instead of just
--force
.
- rebasing rewrites the history of
- git checkout master
- git merge --ff-only
- git push origin master
Git is complaining about the fact that my_branch
is "not fully merged" with its remote branch (origin/my_branch
), because the local and remote branches have diverged in history. It's not complaining about it being fully merged with master.
All that being said - you're actually fine to delete the branch, even with the warning. It won't have any negative effects. Git is just warning you that you're deleting a branch that's not up to date with its remote.