7

I'm working on a project (no teammates, only me) using git. I have been doing commits, merging and pushing them to remote (all disordered with the hope that i learn how to rebase later and fix the history) now I want to rebase all the changes that are already in remote (only 1 master branch only 1 user), by rebase I mean that I want to squash and rename some commits.

I was using the commands they suggest in this link... How to squash commits in git after they have been pushed? , but the problem is that I get some errors and I dont understand them, here are the screenshots showing the problem:

image - git cmd rebasing error

image - part of visual history tree

===========================ERROR AS TEXT================================

command used:

git rebase -i origin/master~2 master

(it should show only 2 commits but for some reason it shows more than 2, like 40+)

Error message:

interactive rebase in progress; onto 9bdd944
Last commands done (15 commands done):
   pick 7af3656 "commit message"
   pick 7355eff "another commit message"
Next commands to do (110 remaining commands):
   pick fc10330 "another commit message"
   pick f22d8c0 "another commit message"
You are currently rebasing branch 'master' on '9bdd944'.

nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyways, use:

   git commit --allow-empty

Otherwise, please use 'git reset'
Could not apply 7355eff... "another commit message"

===========================ERROR AS TEXT================================

It looks like a mess, any tip that help me to solve this problem would be appreciated, I am new using git and i bet this will be easy for some advance user.

Community
  • 1
  • 1
lbtg
  • 81
  • 1
  • 1
  • 4
  • please post the errors as text. not everybody can read your images... – umläute Nov 07 '16 at 22:50
  • It looks like you picked "Pick" instead of "Squash" from the image you posted... If you just use "Pick" for everything, it just reapplies the current commits... Instead edit each line and set the action to "s" or "squash". – jessehouwing Nov 07 '16 at 22:54
  • @umläute sorry i didn't know that, i will post as text now..... – lbtg Nov 07 '16 at 23:03
  • @jessehouwing It supposed to show me only 2 commits, but instead it shows like 40+ commits to rebase... so i just made 1 squash to test, but i get this error, in new projects i can squash easily, cause i rebase in local branch only, but this is the first git project and i dont know what i have been done so far. – lbtg Nov 07 '16 at 23:05
  • Is one of those 2 commits a merge? Cause then it shows the commits that are part of that merge as well. – jessehouwing Nov 07 '16 at 23:29
  • yes, i used to do a lot of merge then i pushed to remote since i didnt know how to rebase. – lbtg Nov 08 '16 at 00:18

1 Answers1

9

Another way you can easily do this is to issue:

git reset --soft d0b8a84d
git commit -m "squashed everything after d0b8a84d"
git push -f

This will first reset the history back to the mentioned commit and all changes that are essentially "undone", but they're still stored on the staging area.

You then commit all of these changes at once with a new commit.

And then you push these back to your remote. It will require a force, since you're rewriting already published history.


Git rebase can also be done, but the above is much easier if all you want to do is compress all your commits after a certain point into a single commit. The rebase will pull in all commits in case of a merge, which may make stuff hard.

Interactive rebase is more powerful if you want to combine specific commits or change the commit message of an old commit etc. But in your case, the above method is soooo much easier.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Yes, all i want to do is to squash and rename commits, my history has like 200 commits and up to 3 merges i think..... i will try what you suggest, hope it works or i will have to repush everything as a single commit, thank you, ill post the results later – lbtg Nov 08 '16 at 00:21
  • I already tried it, works good for the first group of commits, but if i wanna do it again for the second group then i lose the commit message of the first group. – lbtg Nov 08 '16 at 13:18
  • Yeah, in that case you will need to use rebase. And when rebasing your merge, you'll need to redo the actual merges when you rebase. It can be pretty frustrating when needing to clean up a lot of data. – jessehouwing Nov 08 '16 at 16:39
  • Easy option and it works if you really want to clean up in a simple way – Tore Aurstad Jun 20 '23 at 17:26