问题
I'm using Git and I'm trying to sync up my local branches with remote ones. I always keep a "master" and a "develop" branches in both local and remote repositories. When I create a new feature, I create a branch for it and name it like this: feature/my-new-feature-name.
When I finish working on the feature, I commit and push all changes to remote repository (I use Gitlab for my remote repo). Then I merge the feature branch into develop branch and delete the source branch (the feature branch) using GitLab interface. At this point, the feature was merged into develop branch and deleted only in remote repository.
After these steps, I wanna be able to sync my local branch to the state my remote branch is. That is, I wanna have my feature branch merged into develop and deleted from local repo.
To do this, I run the following commands:
git checkout develop
git pull
git branch -d feature/my-new-feature-name
When I do this, Git shows me this message:
error: The branch 'feature/my-new-feature-name' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature/my-new-feature-name'.
Of course I can just run the command using -D flag, but I wanna understand why Git says the branch is not merged.
I found several questions about this in Stack Overflow, but in all of them people suggest running this command in order to check the merged branches:
git branch --merged
When I run this command Git shows me only the develop branch, as if feature/my-new-feature-name was not merged. But in fact feature/my-new-feature-name was merged (in remote repo) and pulled to local one. I can also see my merge commit by running git log
. Why does Git doesn't detect my feature branch as merged into develop?
回答1:
Your local branch is lacking one commit when compared to develop, though : the merge commit that was made by your PR feature-branch > develop
.
Either you just disregard git's hint and go for your branch -D
as you considered, or you can also (even if just to try it out) now merge develop back into your feature-branch (fast-forward at this point) and branch -d
won't complain any more.
回答2:
The problem was to set the squash flag to true when merging. Git only detects the merge if you merge without squash.
来源:https://stackoverflow.com/questions/54349058/how-to-detect-merged-branches-that-were-deleted-in-remote