Git rebase (Merge Squash) 我的功能分支到另一个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9622131/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:09:37  来源:igfitidea点击:

Git rebase (Merge Squash) my feature branch onto another branch

gitgit-mergegit-squash

提问by John Hinnegan

I'm looking for a git command to help me with my feature branches when they're ready to go into Master. This git command would squash all my changes on my branch into a single commit on top of master. I do this today with:

我正在寻找一个 git 命令来帮助我处理我的功能分支,当他们准备好进入 Master 时。这个 git 命令会将我在我的分支上的所有更改压缩到 master 之上的单个提交中。我今天这样做:

git rebase origin/master
git rebase -i HEAD~4

Where 4 is the number of commits to squash. However, this requires me to know how many commits I have. I do this today by running:

其中 4 是压缩提交的数量。但是,这需要我知道我有多少提交。我今天通过运行来做到这一点:

git log HEAD...origin/master

and then counting the commits.

然后计算提交。

I feel as though there should be a better way to do this. Or is this how everyone else does it, too?

我觉得好像应该有更好的方法来做到这一点。或者其他人也是这样做的?

回答by eckes

All you have to do is:

您所要做的就是:

git checkout feature_branch
git rebase master
git checkout master
git merge --squash feature_branch

As the docsfor git merge --squashsay:

由于文档git merge --squash说:

Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit or move the HEAD, nor record $GIT_DIR/MERGE_HEAD to cause the next git commit command to create a merge commit. This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

生成工作树和索引状态,就好像真正的合并发生一样(合并信息除外),但不实际提交或移动 HEAD,也不记录 $GIT_DIR/MERGE_HEAD 以导致下一个 git commit 命令创建合并犯罪。这允许您在当前分支之上创建单个提交,其效果与合并另一个分支相同(如果是章鱼则更多)。

After that, you can git commityour changes which are already staged.

之后,您可以git commit进行已经上演的更改。

回答by rupakg

Here is what I do, gathered from a lot of experience working in larger teams:

这是我所做的,从在大型团队中工作的大量经验中收集而来:

# Get latest from master
git checkout master
git pull --rebase

# rebase master into your feature branch
git checkout feature/my-feature
git rebase master --preserve-merges

# merge feature into master
git checkout master

# DO ONLY ONE of the 2 options below
# if you only have one or (maybe) 2 commits in your feature branch
git merge feature/my-feature

# OR
# this forces an empty merge commit (useful if we need to roll things back)
git merge --no-ff feature/my-feature

# if you use Github, this should also close the pull request
git push origin master

Hope this helps!

希望这可以帮助!

回答by manojlds

I think you are looking for git merge --squash. It should bring in the commits from your feature branch into master and squashes them, so that you can create a single commit.

我想你正在寻找git merge --squash。它应该将来自您的功能分支的提交引入 master 并压缩它们,以便您可以创建单个提交。