推送到远程时如何避免来自 Git pull 的合并提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30052104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to avoid merge commits from Git pull when pushing to remote
提问by jazz199
I have a repository and some local changes to commit. Before committing, I pulled the changes onto my local using Egit in Eclipse.
我有一个存储库和一些要提交的本地更改。在提交之前,我使用 Eclipse 中的 Egit 将更改拉到我的本地。
It creates a merge commit and I submit my commit over it.
它创建一个合并提交,然后我提交我的提交。
Now when I am trying to push to origin, it is showing that it will push my commit as well as merge commit. But ideally, merge commit should not be a part of remote repository.
现在,当我尝试推送到原点时,它表明它将推送我的提交以及合并提交。但理想情况下,合并提交不应该是远程存储库的一部分。
How to avoid this?
如何避免这种情况?
回答by Mohanraj
Use rebase option whenever you pull from remote repository. Please follow the below steps,
每当您从远程存储库中提取时,请使用 rebase 选项。请按照以下步骤操作,
- Commit your changes - It will create a new commit in your local.
- Now do
git pull --rebase <remote-name> <branch-name>
. - Basically the rebase take out your commits that you committed on the current branch HEAD as a patch. Then it will apply all the remote commits on top of HEAD and then applies your commits on top of it.
- So best practice is to commit changes then pull remote commits by using rebase option.
- 提交您的更改 - 它将在您的本地创建一个新的提交。
- 现在做
git pull --rebase <remote-name> <branch-name>
。 - 基本上,rebase 会将您在当前分支 HEAD 上提交的提交作为补丁取出。然后它将在 HEAD 之上应用所有远程提交,然后在其之上应用您的提交。
- 因此,最佳实践是提交更改,然后使用 rebase 选项拉取远程提交。
回答by Gayan Weerakutti
When you have uncommitted changes, you can do,
当您有未提交的更改时,您可以这样做,
git stash
git pull --rebase <remote> <branch>
git stash pop
回答by CodeKid
You can run
你可以跑
git config --global branch.autosetuprebase always
to make git pull --rebase
the default behaviour for git pull.
使 gitpull --rebase
成为 git pull 的默认行为。
回答by choroba
The usual strategy is to work on a branch. When the remote master changes, pull the changes to master and instead of merging, rebasethe branch.
通常的策略是在一个分支上工作。当远程 master 更改时,将更改拉到 master 上,而不是合并,rebase分支。
See Git Rebaseat Atlassian.
请参阅Atlassian 上的Git Rebase。