
git rev-list -count HEAD ^master counts the commits since you made your feature branch from the master, f.ex.Git reset -soft HEAD~$(git rev-list -count HEAD ^master) Īssuming you were branching from the master, you don't need to enter yourBranch into the reset step all the time: git checkout yourBranch $ git commit # all commit messages of yourBranch in one, really useful

when you do it, all commit messages of yourBranch will be gathered. This command doesn't do the "squashed" commit. Which will automatically rebase only your branch's commits onto the current latest master.Īnother simple way to do this: go on the origin branch and do a merge -squash. git reset -soft mainĪdd all of the changes in your git repo directory, to the new commit that is going to be created. This will reset all the extra commits in your feature_branch, but without changing any of your file changes locally. git checkout feature_branchĭo a soft reset of your origin/feature_branch with your local main branch (depending on your needs, you can reset with origin/main as well).

To copy it for my next time git reset $(git merge-base main $(git rev-parse -abbrev-ref HEAD))Ĭheckout the branch for which you would like to squash all the commits into one commit. Since my default branch is called main and my search had multi times brought me here: automatically uses the branch you are currently on.Īnd if you use that, you can also use an alias, as the command doesn't rely on the branch name. Note: git branch -show-current has been introduced with Git 2.22 (Q1 20219).ĮDIT: you will need to use git push -forceįor the reset, you can do git reset $(git merge-base master $(git rev-parse -abbrev-ref HEAD)) Note: finding that origin branch isn't easy/possible with Git (the visual way is often the easiest, as seen here). This isn't perfect as it implies you know from which branch "yourBranch" is coming from. Git reset $(git merge-base master $(git branch -show-current)) This is useful for cherry-picking commits to other branches or repositories.Another way to squash all your commits is to reset the index to master: git checkout yourBranch Squash and merge is OK for smaller changes, but it might be smarter to break large feature branches into multiple logical commits. Interactive rebase has a bit of a steeper learning curve, but with practice, it can work in all scenarios. In addition, rebase is the most flexible workflow – larger and more difficult merges can be tough to do with a squash and merge. Rebase retains a linear commit history, which is important for rollbacks. Squash + Merge acts like merge but creates a single new squashed commit that encompasses all commits in the feature branch. With the fast-forward-only flag ff-only, git will attempt to merge without a merge commit, but this isn't possible if the branches have diverged (i.e., there has been a commit to the parent branch that's not on the feature branch).


Merge will create a merge commit that joins two branches together. You may also have to force push changes (since you are rewriting history) if you have already pushed to a remote. This provides a linear history, meaning context is lost of where a feature branched off. Rebase rewrites history on top of a branch. Each has its quirks, so which one should you use?
Squash commits master git code#
When version controlling your code with git, there are generally three choices when merging feature branches into main.
