Squash commits on master but separate commits on feature branch?

Let’s say Im working on a feature, in a branch called foo_baz.

I commit multiple times as I work on the feature.

  • what's a git equivalent to 'p4 annotate -a'?
  • definition of gitk's “Mark Branch Sides” option
  • Does Git publicly expose my e-mail address?
  • Change Mod RewriteBase depending on domain/url
  • Git - master is ahead of origin/master - Don't want it to be
  • Git two competitive commits in different branches
  • I periodically push foo_baz, a remote-tracking branch, to a remote repo. IE origin/foo_baz.

    I progress to the point where I want to merge all commits from foo_baz into master and push to origin/master. Here’s the question:

    Can I merge foo_baz into master such that all commits are still separate in foo_baz‘s histroy, but that in master‘s history, they are all one, squashed commit?

    Edit:

    I’m a bit of a git beginner, so please forgive me if there’s something totally obvious that I’m missing. I dug around on the web but couldn’t find a direct answer to my question.

  • Keep both local and remote changes after forgetting to pull
  • How Should I use SCM?
  • Using visual studio 2015, how to Commit only one file?
  • What determines default branch after “git clone”?
  • git diff shows ^M with color ui true
  • Does git provide a URL scheme for directly referencing a version of a file?
  • 3 Solutions collect form web for “Squash commits on master but separate commits on feature branch?”

    You might indeed be looking for --squash (as in the other answers), but you might be looking for --no-ff instead.

    Here’s the difference between the two. Suppose you start with two commits:

    A --- B                <-- branch "master", when you start
    

    and then make two more on your new branch foo_baz:

    A --- B                <-- master
            \
              C --- D      <-- foo_baz
    

    Now (once back on master), you have these two options (among others). You can git merge --squash foo_baz && git commit which will give you the following:

    A --- B ----------- E  <-- master
            \
              C --- D      <-- foo_baz
    

    or you can git merge --no-ff:

    A --- B ----------- E  <-- "master" after merge --no-ff
            \         /
              C --- D      <-- foo_baz
    

    Both give you a new commit E—in the first case, from the separate git commit command—but the first one suppresses the multi-parent-ing action, so that in end, when you look back on this a year from now, it looks like you just copied all the changes from the branch. The tree (all the files you get when you git checkout the branches) will be the same in both D and E (in this particular case anyway, where there are no changes to B on master before the merge action), but they’re different commits.

    The difference from a straight git merge is that the latter will generally produce a “fast forward”, which looks like this:

    A --- B
            \
              C --- D      <-- foo_baz, master
    

    where the angled line can be straightened out, giving you a history where both branches look exactly the same.

    I think what you want is something like this:

    git merge --squash foo_baz
    git commit
    

    I think you’re looking for the --squash flag.

    Git Baby is a git and github fan, let's start git clone.