简体   繁体   中英

How to merge changes from feature branch to main branch

A lot of work was done on a feature branch, and during this time, there were multiple commits on the branch but also merge commits with changes from the main branch.

Now the feature branch has commits like this:

  • Work 6 (done on the feature branch)
  • Work 5 (done on the feature branch)
  • Work D (done on the main branch)
  • Work C (done on the main branch)
  • Merge branch 'main' into 'feature'
  • Work 3 (done on the feature branch)
  • Work B (done on the main branch)
  • Work A (done on the main branch)
  • Merge branch 'main' into 'feature'
  • Work 2 (done on the feature branch)
  • Work 1 (done on the feature branch)

Now there is a pull request created on the GitHub repo, but it's hard to follow through the all the changes and approve it.

What I would like is to squash commits made on the feature branch, such that I will have just one nice commit which I need to review.

How can I do that?

I've read about squashing changes and rebasing, but I need some help. Can I just call rebase -i [commitnumber] where commit number is of Work 1 ? I used this on feature branches and it worked great but not when merge commits exist.

Use git merge --squash

That's the simplest way. Go to your main branch and merge the feature branch using the --squash option.

git checkout main
git merge --squash feature

Use git rebase -i

This would be what you suggested. Before rebasing make sure you have a backup copy of your feature branch. On your feature branch invoke git branch :

git checkout feature
git branch feature_backup

Now check the log for the SHA hash of the commit where the development of your feature has started. If this was a long time ago, you can use this command to see where the feature branch splitted from the main branch:

git merge-base main feature

Then use git rebase :

git rebase bc4b30e

Proceed like it is described here . You're not supposed to give the number of commits you want to squash but to give the SHA hash of the first commit you want to squash. You can use HEAD~n if that was n commits ago.

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