0

I want to squash the last 7 commits to 1 commit for specific branch (all the commits were on the same branch the_branch ) and before doing that I want to verify that these are the needed steps

git rebase -i HEAD~7
git commit -m “new commit”
git push origin the_branch

Are these the necessary steps or should I add something ?

Squash my last X commits together using Git

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Jenny M
  • 923
  • 1
  • 14
  • 37
  • 1
    After choosing to interactively rebase last 7 commits, you have to choose what to do with them (pick, delete, squash, ...). There is one step missing in your approach. What did you choose? – Croolman Aug 08 '19 at 06:23
  • 1
    Possible duplicate of [Squash my last X commits together using Git](https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git) – Chris Maes Aug 08 '19 at 07:22
  • As a general piece of advice, don't try queuing up multiple commands like this. Do the rebase+squash and then check what state you're in before deciding what to do next. Many of the problems I see people having with git stem from them pasting commands without knowing their initial state, and not reading the output. – Useless Aug 08 '19 at 07:33

4 Answers4

2

yes the answer you are referring to is correct, the steps you are proposing to do are not correct:

git rebase -i HEAD~7

now you need to change the first word of all lines except the first one from pick to squash

after this you don't need to create a new commit.

If you had pushed your branch before the squashing of the commits, you will however need to force push your branch since you rewrote history

git push --force-with-lease origin the_branch

If you had not pushed before, normal push will suffice:

git push origin the_branch
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • 1
    The force is only necessary if the previous HEAD had been pushed. If you were 7 commits ahead of origin in the first place, no force is required. – Useless Aug 08 '19 at 07:27
  • Thanks, should I use it this "git push --force-with-lease origin the_branch" as is ? – Jenny M Aug 08 '19 at 07:28
  • When I run `git rebase -i HEAD~6` I got list of the commits with texts? what should I do now? – Jenny M Aug 08 '19 at 07:32
  • from my answer: `# now you need to change the first word of all lines except the first one from pick to squash` – Chris Maes Aug 08 '19 at 09:08
2

I would do a soft reset and live happily ever after:

git checkout --detach
git reset --soft HEAD~7 # move branch pointer 7 revisions back, _DO NOT_ tough my working tree.... all changes between HEAD~7 and the tip of the branch are saved in index, ready to be committed
git commit -m "Blah blah"
# if you like the result
git branch -f my-branch
git checkout my-branch
git push -f origin my-branch # force-push as needed
eftshift0
  • 26,375
  • 3
  • 36
  • 60
2

I think it's easier to use git reset with the soft option.

git reset --soft HEAD~7

If you are not sure about the HEAD~7, do git log --oneline, copy de hash of the 8th commit, and:

git reset --soft <hash_your_commit>

Now, all your files you have modified during the last 7 commits are in the staging area and ready for commit:

git commit -m "7 commits squashed into 1"

Finally, to that commit to the remote, you'll have to use the -f or --force option if one or more of the 7 commits were already on the remote. That way, those commits will disappear from the remote as well.

// if none of the 7 commits are on the remote
git push origin branch
//else
git push -f origin branch
mfnx
  • 2,894
  • 1
  • 12
  • 28
0
  • git rebase from the master
  • git fetch and git log are just to make sure you are updated and you can see the log to make sure your rebase worked correctly.
  • after you do a git rebase -i origin/master in the vi editor use the option fixup to squash all commits which you don't want enter image description here
  • If there are any conflicts use git add . and git rebase --continue otherwise you can ignore this step.
  • Just check logs to make sure your commits are at correct place using git log this step is not needed its optional.

  • Lastly you need do a force push using git push -f

    git fetch
    git log
    git rebase -i origin/master
    git status
    git add .
    git rebase --continue
    git log
    git push -f
    
amittn
  • 2,249
  • 1
  • 12
  • 19
  • It isn't possible to list the exact commands OP needs to execute because it depends on his situation. For instance, your commands rebase on top of master, whereas OP wants to rebase the last 7 commits on his branch. Given that situation, it is unlikely rebasing will stop and thus the add and continue part of your commands are unlikely to be needed. Additionally, there might be good reasons why a pre-push hook wants to prevent the push, uncritically suggesting to use `--no-verify` can be harmful. – Lasse V. Karlsen Aug 08 '19 at 07:20
  • after it open the vim ui I got 5 commits that I want and one I don’t want , should I mark all I want with s and the one that I don’t want with d ? after then should I click `esc` ? and then push it ? – Jenny M Aug 08 '19 at 07:46
  • @JennyM option drop will remove the commit and squash will meld your existing commit into the previous commit note that this will open a new view which will show you the 2 commit messages and would ask you to either give your preference of message. Even if you don't do any thing here and just esc and write changes (using esc :x ) you will see your squashed commit is meld into the previous with message shown as a sub part of the previous message To save and exit a vi editor just esc and :x would do – amittn Aug 08 '19 at 08:36