Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

60%
+1 −0
Q&A Linking documentation to Git commit comments

Identifying a commit A universal identifier for each commit is its SHA1 — a 40 digit hexadecimal number, like this one: 3e0b5fb09d70d0457d7e5ae7892504f6e1b45f33 This commit is from the Sphinx d...

posted 6y ago by Nick Volynkin‭  ·  last activity 4y ago by System‭

Answer
#3: Attribution notice added by user avatar System‭ · 2019-12-08T08:03:34Z (over 4 years ago)
Source: https://writers.stackexchange.com/a/33538
License name: CC BY-SA 3.0
License URL: https://creativecommons.org/licenses/by-sa/3.0/
#2: Initial revision by user avatar Nick Volynkin‭ · 2019-12-08T08:03:34Z (over 4 years ago)
# Identifying a commit

A universal identifier for each commit is its [SHA1](https://en.wikipedia.org/wiki/SHA-1) — a 40 digit hexadecimal number, like this one:

    3e0b5fb09d70d0457d7e5ae7892504f6e1b45f33

This commit is from the Sphinx documentation builder's repo. The comment says:

    Merge pull request #4556 from tk0miya/4543_autodoc_test_fails_with_py353
    
    Fix #4543: test for autodoc fails with python 3.5.3

A reasonable way to make a persistent link to a commit is to store its SHA1 and use it to access the commit through various interfaces — see examples below.

# Getting commit details

Say, you're programming a system that builds documentation. You want to programmatically get the commit's message and add it to the changelog.

## Command line and local repository

First option is when the code has filesystem access to the repository.

    $ git show -s --format=%B 3e0b5fb09d70d0457d7e5ae7892504f6e1b45f33
    
    Merge pull request #4556 from tk0miya/4543_autodoc_test_fails_with_py353
    
    Fix #4543: test for autodoc fails with python 3.5.3

There are various [`--format` options](https://www.git-scm.com/docs/git-log#_pretty_formats) to get the required commit details.

## API and remote repository

You can also use SHA1 to get the commit details from a remote repository. Say, you have a self-hosted GitLab installation and would like to get the commit's message via [GitLab API](https://gitlab.com/help/api/commits.md#get-a-single-commit). Here's what the GET request could look like:

    curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" \
    "https://gitlab.example.com/api/v4/projects/5/sphinx/commits/3e0b5fb09d70d0457d7e5ae7892504f6e1b45f33"

Response is a JSON with lots of commit details.

# Selecting commits

If you want to automatically select meaningful commits, you need some rule or convention for your team to mark such commits.

For example, many teams work with “git flow”&nbsp;or “GitHub/GitLab flow” methodologies, in which job is done in feature branches and then merged into a stable branch. Each merge results in a merge commit. If your team members write meaningful messages in merge commits, that's a good source for a changelog.

To get the merge commit messages, use `git log --merges`. In this example we get the hash (SHA1, `%H`), authoring date (`%ad`), and the commit message (`%B`), separated with newlines (`%n`):

    $ git log --format='%H%n%at%n%B' --merges   

You may want to pack the output in some convenient format, like JSON:

    $ git log --merges --format='{%n "hash": "%H",%n "timestamp": %at,%n "subject": "%s"%n}'
    {
      "hash":3e0b5fb09d70d0457d7e5ae7892504f6e1b45f33,
      "timestamp": 1517848081,
      "subject": "Merge pull request #4556 from tk0miya/4543_autodoc_test_fails_with_py353"
    }
    
    ...

The same list of [`--format` options](https://www.git-scm.com/docs/git-log#_pretty_formats) applies here.

* * *
#1: Imported from external source by user avatar System‭ · 2018-02-08T04:16:54Z (about 6 years ago)
Original score: 5