Squash Commits in Git Desktop
Asked Answered
A

3

7

I'm trying to squash 6 commits into a single commit using a branch that I already pushed to GitHub.

I tried to do this via command line, but I keep receiving no-op instead of the commits for that branch.

Is there a way to do this via GitHub desktop that might make more sense?

Please provide step by step if possible;

I'm new to using GitHub for collaborative purposes and my knowledge doesn't span very far past git add, commit, and push.

Asco answered 28/12, 2015 at 19:3 Comment(0)
H
6

In order to do a git squash follow those steps:

// X is the number of commits you wish to squash, in your case 6
git rebase -i HEAD~X

Once you squash your commits - choose the s for squash = it will combine all the commits into a single commit.

enter image description here


You also have the --root flag in case you need it

try: git rebase -i --root

--root

Rebase all commits reachable from <branch>, instead of limiting them with
an <upstream>.

This allows you to rebase the root commit(s) on a branch.  
When used with --onto, it will skip changes already contained in `<newbase>`   
(instead of `<upstream>`) whereas without --onto it will operate on every 
change. When used together with both --onto and --preserve-merges, all root 
commits will be rewritten to have `<newbase>` as parent instead.`
Hauteloire answered 28/12, 2015 at 19:8 Comment(8)
Maybe this will sound silly, but when I try to type 's' in the command line on the vim screen, it just overwrites the first 'p' in 'pick'. How do I actually pass it the command? I also tried :s but it yields the message "E33: No previous substitute regular expression"Asco
You cant squash the first one, if you want to squash it set the X to 7Hauteloire
So if I change "pick" to "squash" for the 5 commits after the first one, how do I "save" that with vim?Asco
yep. exactly. squash the first X and then save. you will see rebase message in the consoleHauteloire
I don't think you understood my question. I'm trying to squash the commits... what exactly do I need to do to accomplish that? Vim keeps giving me the error message listed above when I try to saveAsco
I ended up having to use 's' instead of 'squash'. Not sure why that fixed the problem. Marking the questions as answered, but I've got one more roadbloack. What's the easiest way to push this squash to GitHub?Asco
you need to overwrite the code in github: git push -f origin <branch> the -f is to force the update of the branch since you did a rebaseHauteloire
I'm getting "cannot squash without previous commit" whatever this means.. I want to squash all commits in that list and set new commit messages, how can I do that?Interstice
U
8

Is there a way to do this via GitHub desktop that might make more sense?

Starting with version 2.9 there is now a way to squash commits in GitHub Desktop.

Here are 2 of the convenient ways to do this.

  1. Select multiple commits in "History" tab (using Ctrl or Shift keys), right click on one of the selected commits and select "Squash 2 commits...".

enter image description here

  1. When merging a branch into another branch click on the arrow on the right to submit button and select "Squash and merge".

enter image description here

Unpopular answered 16/6, 2021 at 15:30 Comment(0)
H
6

In order to do a git squash follow those steps:

// X is the number of commits you wish to squash, in your case 6
git rebase -i HEAD~X

Once you squash your commits - choose the s for squash = it will combine all the commits into a single commit.

enter image description here


You also have the --root flag in case you need it

try: git rebase -i --root

--root

Rebase all commits reachable from <branch>, instead of limiting them with
an <upstream>.

This allows you to rebase the root commit(s) on a branch.  
When used with --onto, it will skip changes already contained in `<newbase>`   
(instead of `<upstream>`) whereas without --onto it will operate on every 
change. When used together with both --onto and --preserve-merges, all root 
commits will be rewritten to have `<newbase>` as parent instead.`
Hauteloire answered 28/12, 2015 at 19:8 Comment(8)
Maybe this will sound silly, but when I try to type 's' in the command line on the vim screen, it just overwrites the first 'p' in 'pick'. How do I actually pass it the command? I also tried :s but it yields the message "E33: No previous substitute regular expression"Asco
You cant squash the first one, if you want to squash it set the X to 7Hauteloire
So if I change "pick" to "squash" for the 5 commits after the first one, how do I "save" that with vim?Asco
yep. exactly. squash the first X and then save. you will see rebase message in the consoleHauteloire
I don't think you understood my question. I'm trying to squash the commits... what exactly do I need to do to accomplish that? Vim keeps giving me the error message listed above when I try to saveAsco
I ended up having to use 's' instead of 'squash'. Not sure why that fixed the problem. Marking the questions as answered, but I've got one more roadbloack. What's the easiest way to push this squash to GitHub?Asco
you need to overwrite the code in github: git push -f origin <branch> the -f is to force the update of the branch since you did a rebaseHauteloire
I'm getting "cannot squash without previous commit" whatever this means.. I want to squash all commits in that list and set new commit messages, how can I do that?Interstice
B
2

This post from Thoughtbot might help you get through it via command line. It's probably better to know exactly what you're doing than to rely on a GUI tool for Git. It will benefit you in the long run.

https://thoughtbot.com/blog/git-interactive-rebase-squash-amend-rewriting-history

Beheld answered 28/12, 2015 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.