Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git, squashing commits on the fly

Tags:

git

rebase

squash

I have a local git repo, one remote repo. I made 5 commits to local repo since last pull from remote.

Now I need to push to remote. And I need to squash my last 5 commits.

I can do it by git rebase -i HEAD~5

But it's not good. I don't want to change anything in my local repo. I want to push one squashed commit to remote and leave 5 unchanged commits in my local repo.

Any ideas?

UPD: what about if I have my local repo with 5 commits. and I need to create new remote repo to share my code with others. How to squash commits for this new repo?

like image 685
Victor Mezrin Avatar asked Dec 02 '12 11:12

Victor Mezrin


People also ask

Is squashing commits a good idea?

As a general rule, when merging a pull request from a feature branch with a messy commit history, you should squash your commits. There are exceptions, but in most cases, squashing results in a cleaner Git history that's easier for the team to read.

How do you squash commits in a pull request?

When you select the Squash and merge option on a pull request on GitHub.com, the pull request's commits are squashed into a single commit. Instead of seeing all of a contributor's individual commits from a topic branch, the commits are combined into one commit and merged into the default branch.

What happens when you squash a commit?

Squashing is a way to rewrite your commit history; this action helps to clean up and simplify your commit history before sharing your work with team members. Squashing a commit in Git means that you are taking the changes from one commit and adding them to the Parent Commit.


1 Answers

If you just want to keep your five commits for reference, maybe you should work in a branch with them.

  1. git branch new-branch master

  2. Do your commits. Since you have done this, just reset head of master:

    git reset --hard HEAD~5

  3. git merge --squash master new-branch

  4. git push

You will end up with squashed commit on master and origin/master and 5 commits on new-branch.

like image 183
Titas Avatar answered Oct 18 '22 08:10

Titas