TheRealFakeNews
TheRealFakeNews

Reputation: 8153

How to find the last commit that was not done by me?

I want to squash my commits using git rebase -i HEAD~n, but in order to do so, I typically have to do git log and manually count all the number of commits up to the next commit that was not made by me. In other words I would see something like this

commit 89073409kiejroijer
Author: AlanH
commit 89073409kiejroijer12903
Author: AlanH
...
commit 89073409kiejroijer83
Author: AlanH
commit 890789754239kldjrjsafd
Author: JohnSmith

So I would count all the ones before John's commit. Then do git rebase -i HEAD~n

Is there a way to just do this in one step so that I don't have to count my commits?

Upvotes: 2

Views: 150

Answers (5)

afar
afar

Reputation: 1

prahlad venkata's answer won't work anymore with Git 2.35 (Q1 2022)+.

Valid solution adapted from https://stackoverflow.com/a/20359396/6309 :

git log --perl-regexp --author='^((?!AdamH).*)$' --pretty=format:'%H' -n1

Outputs the commit ID. Then rebase onto that commit ID with:

git rebase -i <commit-id>

See VonC's comment for further information: https://stackoverflow.com/a/70644305/20686377

Upvotes: 0

prahlad venkata
prahlad venkata

Reputation: 371

git log --author="Adam" --invert-grep -n1

gives the first commit that didn't match Author="Adam"

use that <commit-id> to rebase

git rebase -i <commit-id>

Upvotes: 2

jthill
jthill

Reputation: 60305

No need to count, just use the commit id you want, in your example you can see it's 890789754239kldjrjsafd.

To find it programmatically the searches in the other answers will work, the easiest search I can find is

git log --format=%h\ %an|awk '!/ Your Name/{print $1;exit}'

Upvotes: 1

thedeliciousmuffin
thedeliciousmuffin

Reputation: 814

How about:

git log --format='%H %an' | grep -v Adam | cut -d ' ' -f1 | xargs -n1 git log -1

Where Adam is name of the author i.e. you

Upvotes: 0

0xTang
0xTang

Reputation: 886

use below shell can fix your issue.

git log | cat | grep Author | awk -F: "{print $1}" | grep -v "yourname" | wc -l

this command counts the commit numbers before your last commit.

then use git rebase -i HEAD~n

or use this in one command.

git log | cat | grep Author | awk -F: "{print $1}" | grep -v "yourname" | wc -l | xargs -I {} git rebase -i HEAD~{}

hope this help you.

Upvotes: 0

Related Questions