Why did Git create a merge commit with no file changes?

。_饼干妹妹 提交于 2019-12-17 22:38:12

问题


I am collaboratively working on a project with someone, so we decided to use git. Unfortunately, we frequently code in locations with no internet, so we end up with something like this:

origin/master: A---B---C
                        \
mylocalmaster:           D---E---F
                        \
hismaster:               G---H---I

Now, say he pushes his commits and gets this:

origin/master: A---B---C---G---H---I
                        \
master (local):          D---E---F

All I want to do is push my commits to get this in both my local repo and the online one:

A---B---C---D---E---F---G---H---I

It seems to work when I do git push, but the trouble arises when I do git fetch and then git merge. All I'm trying to do is get his commits into my local repo, but I end up with a merge commit saying something like Merge remote-tracking branch 'origin/master' as its message.

I don't want to have this pointless commit, since there is no conflicting code in our commits. We are working on completely different files, so there's no reason to have this commit. How can I prevent git from creating this merge commit?


回答1:


You can omit creating merge commits, by using rebase instead of merge.

As @Dougal said, if you do git fetch, you can execute git rebase afterwards to change the base of your changes to the fetched HEAD.

Usually you create those unwanted merge commits, by pulling from remote repository. In that case you can add --rebase option:

git pull --rebase

or add the proper option to Git config file (locally):

git config branch.<branch-name-here>.rebase true

or for all the new repositories and branches:

git config branch.autosetuprebase always --global

However, rebasing creates cleaner, more linear history it is good to create merge commits, where there are massive changes in both branches (use git merge to do so).




回答2:


Use git rebase (after a git fetch) to make your commits apply against his rather than against the previous master. That is, to go to ABCGHIDEF in your example. (You can't do ABCDEFGHI without having to do a push -f, because ABCGHI is already in origin/master and you'd have to override that.)



来源:https://stackoverflow.com/questions/10157702/why-did-git-create-a-merge-commit-with-no-file-changes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!