Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (29.3k points)

In Git, is there a way to merge all changes from one branch into another, but squash to a single commit at the same time?

I often work on a new feature in a separate branch and will regularly commit/push - mainly for backup or to transfer what I'm working on to another machine. Mostly those commits say "Feature xxx WIP" or something redundant.

Once that work is finished and I want to merge WIP branch back into master, I'd like to discard all those intermediate commits, and just a have a single clean commit.

Is there an easy way to do this?

Alternatively, how about a command that squashes all commits on a branch since the point where it was branched?

1 Answer

0 votes
by (50.2k points)

For this question you could do:

git merge --squash <feature branch> 

Then commit that using 

git commit

Here --squash will allow you to create a single commit on top of the current branch whose effect is the same as merging another branch. --no-squash will perform the merge and commit the result. This option can be used to override --squash

For more information: https://git-scm.com/docs/git-merge#Documentation/git-merge.txt---squash

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...