Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: revise the last commit that changed the files in the index #66

Open
chris-morgan opened this issue May 29, 2020 · 7 comments
Open

Comments

@chris-morgan
Copy link
Contributor

I often want to revise the last commit that changed the file or files in the index.

I had been doing this manually until today, but I finally got fed up with it enough to make a simple alias.

As an alias (add to .gitconfig):

[alias]
	revise-most-recent-commit-that-touched-file = "!f() { if [ $# -eq 0 ]; then REV=\"$(git status --porcelain --untracked-files=no | sed '/^ /d;s/^.. //' | xargs -n1 git rev-list -1 HEAD --)\"; NUM_REVS=\"$(echo \"$REV\" | wc -l)\"; if [ $NUM_REVS -ne 1 ]; then >&2 echo Files in the index were not all last modified in the same commit; exit 1; fi; else REV=\"$(git rev-list -1 HEAD -- \"$1\")\"; shift; fi; echo git revise \"$REV\" \"$@\"; }; f"

Reformatted as a standalone script:

#!/bin/bash
if [ $# -eq 0 ]; then
	REV="$(
		git status --porcelain --untracked-files=no |
			sed '/^ /d;s/^.. //' |
			xargs -n1 git rev-list -1 HEAD --
	)"
	NUM_REVS="$(echo "$REV" | wc -l)"
	if [ $NUM_REVS -ne 1 ]; then
		>&2 echo Files in the index were not all last modified in the same commit
		exit 1
	fi
else
	REV="$(git rev-list -1 HEAD -- "$1")"
	shift
fi
git revise "$REV" "$@"

This script is not robust (I doubt it handles things like uncommitted files properly), but it’s good enough for me.

I have created this issue because I think this would be useful functionality to upstream into git-revise, e.g. git revise --last-commit-for-file=… (with the files defaulting to all the files in the index).

A better and safer version of this would allow users to specify an acceptable range of commits to revise (I’ve hardcoded HEAD here, but things like @{u}.. and master.. would be useful).

Another power-user feature would be to be able to revise different commits for each file in the index (rather than just bailing as I’ve written here). The alternative is adding each file and revising, one by one (or by as many files as are in each commit, anyway). This functionality would definitely save me time occasionally.

@Manishearth
Copy link
Collaborator

This is basically git absorb, I've wanted that functionality as well

@Manishearth
Copy link
Collaborator

I think a good path forward here would be to make PRs to https://github.com/tummychow/git-absorb to make it possible to use to implement this functionality in git-revise (i.e. git-revise queries git-absorb to figure out what changes need to be made)

@alerque
Copy link
Contributor

alerque commented May 29, 2020

I'd never head of git absorb, thanks for the tip! Until now my workflow has been to git add -p, add every hunk one at a time using a homegrown commit script that marks it as a fixup for the last commit that touched those line numbers, then rebasing the whole thing manually.

Looking over the usage, wouldn't it make more sense to setup git absorb to call-out to git revise than the other way around?

@Manishearth
Copy link
Collaborator

That can also work

@odnoletkov
Copy link
Contributor

I use git absorb && git revise --autosquash all the time – I don't see how this can be improved

@Manishearth
Copy link
Collaborator

Manishearth commented May 30, 2020

Oh, git absorb sets up fixup commits? Neat!

I was under the impression it directly did the rebase

@krobelus
Copy link
Contributor

git autofixup is another tool that's very similar to git absorb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants