0

I'm new to git so this might sound like a silly question. I have a master branch and a feature branch.

A->B->C->D(HEAD of master)

From C I created a feature branch C->M->N->O->P->Q->R(Head of feature branch)

Now i need to merge my feature branch to the master branch but i want that the six commits that i made in my feature branch don't reflect in my master instead there should only be two commits. How can we do so?

I want the changes of both the branches to be in my master branch.

  • 1
    Possible duplicate of [Squash my last X commits together using Git](https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git) – AElMehdi Oct 15 '19 at 14:59

2 Answers2

1

Step 1: Rebase your master

Checkout Feature branch and after your checkout run: git pull --rebase origin master

If merges are necessary, perform them, commit and run git rebase --continue

Your Feature branch will look like this: C-D-M-N-O-P-Q-R

Step 2: Squash

Run: git rebase -i HEAD~6 6 is because you want to squash the last 6 commits.

First commit is "pick" All the other are "s" (for squash)

Step 3: Push into repo

Perform git push -f on the feature branch to push it and the new history into the repo.

Force-push is necessary due to squashing and you rewriting the git history.

Step 4: Merge into master

Checkout your master branch and run git merge <feature-branch>

Community
  • 1
  • 1
almac777
  • 501
  • 1
  • 8
  • 23
0
  1. Checkout feature branch with git check feature_branch
  2. Squash C->M->N->O->P->Q->R to C->S with git rebase -i C or git rebase -i HEAD~6
  3. Do rebase on master with git rebase master
  4. Checkout master with git checkout master
  5. Merge feature branch to master with git merge feature_branch

Note:

(If you've push feature_branch to remote, you also have to do git push -f on feature_branch to overwrite the remote feature_branch)

Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295