JamesHalsall
JamesHalsall

Reputation: 13485

Creating Git alias that gets the last commit

I'm wondering if there is any way to get the SHA1 of the last commit via a Git alias.

I have the following so far, but it throws an error saying:

Expansion of alias 'last-commit' failed; '9fa5c2c72e586ce825d54114532400d8cc56106f' is not a git command

The command I'm using to create the last-commit alias:

git config --global alias.last-commit `log -1 --pretty=format:%H`

I'm aware that git log -1 will give me the last commit information, but I want the last commit SHA1 on its own so I can use it with cat.

Any help is appreciated

Upvotes: 3

Views: 1243

Answers (2)

Mark Longair
Mark Longair

Reputation: 467261

You can do:

git rev-parse HEAD

... or as an alias:

$ git config --global alias.last-commit "rev-parse HEAD"
$ git last-commit
dc1ac14864ecb3dd27f934ba964b030cfedf234a

manojlds alludes to the quotes being the problem with your version - to expand on that slightly, backquotes run the command within them and substitute the standard output of that command into the command you're running. Since the command log probably doesn't exist, you'll see an error on standard error and the alias will be set to an empty string. Single or double quotes in your example would be fine.

Upvotes: 4

manojlds
manojlds

Reputation: 301167

Just use git rev-list -1 HEAD

For your alias using git log, use:

git config --global alias.last-commit "log -1 --pretty=format:%H"

Notice the quotes.

Upvotes: 3

Related Questions