reddit / emacs

Magit automation for commit + squash and commit + rebase + push

jcarloz · 2022-03-09 00:04
As part of my workflow I have 2 scenarios I do most of the time when using magit: \- Commit, rebase from master then force push to my own branch \- Commit then squash the new commit with the previous commit, rebase from master, force push to my own branch I wonder if there is a way to automate these scenarios with magit to then use just one key combination?
AndreaSomePostfix · 2022-03-09 00:11
If I were you, I would take your list of action, translate them to the magit elisp functions that make those possible and wrap them in an interactive elisp function. I think the first step "Commit" is the only interactive bit. Worst case scenario you can do this by scripting it using git directly and calling from a (shell-command "git ..") form (to have it in an interactive Emacs command)
tech_addictede · 2022-03-09 00:14
If you manage to automate the rebase from master and force push thing please post it here.
jcarloz · 2022-03-09 00:22
I'll have a look into that, I don't mind having to type the commit message if needed even if it's discarded later during squash.
7890yuiop · 2022-03-09 01:48
> Commit then squash the new commit with the previous commit To cover the low-hanging fruit: that's what `git commit --amend` is for. `c a` in Magit. `git commit --amend --no-edit` does the same without asking you to edit the commit message.
jcarloz · 2022-03-09 02:32
That is a good point, one less step. Now just need to combine the amend with the rebase master and push to my branch to automate the workflow.
nv-elisp · 2022-03-09 03:15
I believe magit even has a shorthand for a commit amendment while reusing the same message: "commit extend"
7890yuiop · 2022-03-09 03:42
Trivially, how about: (defun my-commit-squash-rebase-force-push () (interactive) (magit-shell-command-topdir "git commit --amend --no-edit && git rebase master && git push origin --force-with-lease"))
rickardsundin · 2022-03-09 07:06
Here are some default key sequences I use frequently in my workflow, that seems to overlap a bit with what you are asking for: - `f a` Fetch changes from remote on all branches - `r u` Rebase my current branch with the upstream branch (you can set that to point to master) I often do this at the start of a session or after a break. - `c e` Add my staged changes to the latest commit on my current branch - `P - f p` Force-push (with lease) my current branch I do this when I make small incremental changes but don’t want to add an extra commit.
Tiv0w · 2022-03-09 08:13
Exactly! Bound to `c e`. I use it every time I commit too quickly and then realize there's a typo or small mistake to correct.
jcarloz · 2022-03-10 05:41
Yes, what I currently do is similar, what I'm trying to do is press just one key to do everything instead of the various sequences.
crawsphi · 2022-03-10 23:34
If you have this operation already defined as a git alias, you can invoke _that_ from magit with `(magit-run-git "name_of_alias")`. Which you could wrap in a Lisp command.
Hooxen · 2022-03-11 12:13
Nice “f a” sounds better than having to switch to master and back to your branch - unless there’s too many remote branches and one only wants to fetch master...for “r u” do you set upstream to origin/master or master? I often rebase on local master with “r e” (e for elsewhere) and pick “master” from the options that gives me