How to squash commits on Bitbucket after they have been pushed?
Rebasing is easy to do with SourceTree, in sourcetree select the commit that you based your work on, then press the right mouse button.
Then a menu will popup, select "interactively rebase children of "
This will open a screen where we can select what to do with every commit. Since we want to squash every commit together, we click the top commit and select the button "Squash with previous" at the bottom of the screen. Sourcetree will update the top screen in response to the actions you did so you can see its doing what you want.
Before:
After:
Usually, you also want to change the message of the commit when you squash them together, double click on the message to change it. If you are done with the changes, press "ok" at the right bottom corner. Source Tree will now rebase the history for you.
Finished result:
Now you can force push your branch to your own fork of the other project and try to make another pull request.
There is a simpler way.
If you are absolutely certain that no one will ever use that specific branch you can do the following:
- Create a local branch that has everyone's commits.
- do git log on the local branch to determine the hash of the non-branch commit preceding the first branch commit.
- Do:
git reset --soft <hash>
- Do: git commit to create the single commit message that you want.
- Do:
git rebase -i
to move your local branch to the tip of master. - Fix any merge conflicts.
- git push your changes to your remote branch used for the PR. Use -f for force push if it already exists.
Use git rebase
!
Bitbucket use the Git software.
alex said it already in his post: Use the
git rebase -i ourbranchname
command!
Commands
Use these commands in the terminal, command prompt or in your cmd:
git rebase -i yourbranchname
- A file in a editor program will be automatcally open.
- Change all commits from "
pick
" to "fixup
" but one commit must have "pick
"! - Save the file and close.
git push -f origin yourbranchname
Helpful resources
- http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html
- https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History
- http://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit
Found a nice summary of the posted answers. This one is a bit more clear: squash pushed commits. It will require creating a second branch upon the creation of the fork. This second branch can have as many pushes to the remote server as needed.
Create a new personal branch that will be squashed.
# Start with the existing personal branch that contains all of your commits. $ git checkout {ExistingBranchName} # Create a new personal branch that will be squashed. $ git checkout -b {BranchName}
Identify the first commit where your personal branch diverged from an existing CEF branch.
# Replace {BranchName} with your new branch name. # Replace "master" with a different CEF branch as appropriate # (e.g. "2272", "2171", etc). $ git merge-base {BranchName} master
Start an interactive rebase using the commit hash returned from step 2.
$ git rebase --interactive {hash}
This will launch a text editor with a list of all commits in your personal branch. It should look something like this:
pick 59d2a23 Initial implementation pick 752ae4c Add more features pick cd302a3 Fix something pick 5410de3 Fix something else
Change all but the first line to say
squash
instead ofpick
. The contents should now look like this:pick 59d2a23 Initial implementation squash 752ae4c Add more features squash cd302a3 Fix something squash 5410de3 Fix something else
Save the changes and close the file (Can be done by pressing
esc
and type::wq
.A new file will now open containing the commit messages from all of the commits. Reword the commit message then save the changes and close the file.
Push the modifications to your personal remote repository.
# If the branch has already been pushed to the remote repository # you will need to add the `--force` argument. git push origin {BranchName} # or git push origin {BranchName} --force