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

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 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:

  • get changes newer than a given revision with git show
  • Include last commit index in project
  • Git clone is incredibly slow on my Mac. How can I fix that?
  • Git SVN fetch nothing occasionally
  • When to use a bare Git repository?
  • What is the effect of git commit without the -a option?
  • 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?

  • Is there a way to change the file path for an Rstudio project?
  • How can I make git ignore future revisions to a file?
  • How can I get git to use graphical diff locally and textual diff over ssh?
  • Git hooks for symlink convertion on Windows
  • Mark a file in the GIT repo as temporarily ignored
  • How can I view multiple git diffs side by side in vim
  • 3 Solutions collect form web for “Git rebase (Merge Squash) my feature branch onto another branch”

    All you have to do is:

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

    As the docs for git merge --squash say:

    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).

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

    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!

    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 Baby is a git and github fan, let's start git clone.