2

I'm frequently squashing all the commits on a feature branch before committing to master. Here's what I do currently:

git checkout master
git merge --squash {branch}
git commit -m "{feature_summary}"

Is there an git alias I can use to do this? The aliases I'm familiar with (e.g. stashes = stash list or uncommit = reset --soft HEAD~1) don't store variables, and in this case the second command needs to know which branch we came from at the beginning of the commands, so I imagine has to store this information.

Ideally I'd like a git alias such as git squash which does all those commands. I'm fine with either expecting a string as the commit message, or opening an editor to enter the message.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
Maximilian
  • 7,512
  • 3
  • 50
  • 63
  • xargs can be used for that. However, I am too bad at its syntax, so I'm just commenting, not answering. Sorry about that. – KUSH42 Jul 03 '20 at 15:07

3 Answers3

2

You can use the @{-1} construct which means "last checked out branch", and pass a parameter to an ad-hoc bash function :

# ms for merge-squash but call it as you prefer
git config --global alias.ms '!f() { git checkout master && git merge --squash @{-1} && git commit -m "$1"; }; f'

then an example use could be, from your feature branch :

git ms "My commit message"
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 1
    @msanford Ha! Glad to see you here too. – Romain Valeri Jul 03 '20 at 15:13
  • This looks perfect, but I'm getting `fatal: bad alias.squash-master string: unclosed quote` based on `squash-master = '!f() { git checkout master && git merge --squash @{-1} && git commit -m "$1"; }; f ` Am I doing something wrong? – Maximilian Jul 03 '20 at 15:21
  • @Maximilian Strange... checking at the moment, I'll edit as soon as I know. – Romain Valeri Jul 03 '20 at 15:24
  • 1
    I think it was a quote issue in the shell command vs adding to a file. I cut out the function and so this works now and is possibly more compressed? `squash-master = "!git checkout master && git merge --squash @{-1} && git commit -m"` – Maximilian Jul 03 '20 at 15:29
1

You can create an alias yourself:

[alias]
    sf = !git checkout master && git merge --squash "$1" && git commit -m "$2"

You can run it with

git sf <branch> "<message>"
msanford
  • 11,803
  • 11
  • 66
  • 93
0

If you have in your PATH a program called git-command, git will use that program when you call git command [args].

So you can add the following script to your PATH, name it git-squash and make it executable.

#!/bin/bash
branch="${1}"
shift
git checkout master \
&& git merge --squash "${branch}" \
&& git commit "${@}"
xihh
  • 169
  • 2
  • 12