Interactive rebase after merging/other commits interleaving mine

My typical git workflow is to make a feature branch off our master branch, make small commits, while periodically merging from master along the way to keep up.

I then open a pull request on github and when it's accepted I'll delete the branch. I'd like to interactively rebase more but when I do I'll encounter merge commits and other people's commits interleaved with my own. I'm not sure how/if I can squash those commits or group/squash mine alone?

For instance, my git log for my branch might look like this when it comes time to rebase:

merge commit
someone else's commit
one of my commits
another one of my commits
another merge commit
another person's commit
one of my commits, the first one after branching

I'm having trouble with what's possible and what's not when doing an interactive rebase and squashing and reordering commits. Given the above workflow, what would you recommend?


The process we use in my office is to rebase to the master branch (or whatever branch you expect to merge into) before merging your pull request. This ensures that your commits are ordered on top of all the other commits currently in master , which eliminates the interleaving of your commits with others:

> git fetch upstream master
> git rebase upstream/master

I'm not sure how/if I can squash those commits or group/squash mine alone?

Squash only your commits not others!!

Recently, I faced same problem, I had 41 commits on my pull requests, my branch had a merge conflict and my pull request was stale. I had to deal with some of the problems that you are facing now. I will only touch something what I have implemented.

one of my commits
another one of my commits
one of my commits, the first one after branching

I suggest squash only your commits (all of them if you want to).

For instance, if you have 3 commits, you can squash them and make into one using interactive rebase. See below command:

$ git rebase -i HEAD~n

For example, you want to squash 3 commits:

$ git rebase -i HEAD~3

Now, you see an interactive rebase interface, where you can write reword/pick on first commit and squash on rest of them. Please see this video for better understanding.

Squashing commits

merge commit
someone else's commit
another merge commit
another person's commit 

Rebase to other (someone else) commits

This is the common problem everyone faces when using git. Imagine you are working on your local repo. After working for a while, you made some commits, now you want to push them to original remote repository. Cool, you did this git push which says merge commit problem.

You know someone else might have also committed to the same remote repo which is updated now. Since your local repo is not updated to the remote one, there is merge commit problem . Now what if we clear this problem? Can we update our local machine's repo to your original remote one? Is it possible?

Yes, it is possible, you can do it by rebasing. How?

When you do:

git pull --rebase

What's happening here? Git will rewind (undo) all of your local commits, pull down the remote commits then replay your local commits on top of the newly pulled remote commits. If any conflicts arise that git can't handle you'll be given the opportunity to manually merge the commits then simply run git rebase --continue to carry on replaying your local commits.

See this blog post for more details

If you want to merge your branch to master?

Best (and safest) way to merge a git branch into master

链接地址: http://www.djcxy.com/p/49304.html

上一篇: 我如何将一个分支合并到github中的master?

下一篇: 合并后的交互式重新分区/其他提交交织我的