简体   繁体   中英

How to change 6 commits into 2 from feature branch to main branch

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.

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

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>

  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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM