Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7594657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:28:30+00:00 2026-05-30T21:28:30+00:00

I want to post process the output of git log and have been playing

  • 0

I want to post process the output of git log and have been playing with the --pretty settings. When I e.g. do

--pretty=format:'{"sha":"%h","message":"%B","author":"%aN <%aE>","commit":"%cE","date":"%cD"}

I get some JSON-like output; when I put in a { or } or even a " into the commit message this messes up my output.

Is there a way to tell git log to escape those chars e.g. by prepending a \?

There are two similar questions Git log output to XML, JSON or YAML and Git log output preferably as XML, but they both do not address the escaping of the special chars (e.g. if in the XML case I put <foo> in my commit message, the resulting XML will be broken).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:28:31+00:00Added an answer on May 30, 2026 at 9:28 pm

    Escaping strings isn’t Git’s job; git log doesn’t have anything that’ll help you do that. To achieve what you’re after, you’ll need something like sed to do the string editing for you.

    Try this (should work in most shells, but I’ve only checked in Cygwin bash):

    function escape_chars {
        sed -r 's/(\{\}")/\\\1/g'
    }
    function format {
        sha=$(git log -n1 --pretty=format:%h $1 | escape_chars)
        message=$(git log -n1 --pretty=format:%B $1 | escape_chars)
        author=$(git log -n1 --pretty=format:'%aN <%aE>' $1 | escape_chars)
        commit=$(git log -n1 --pretty=format:%cE $1 | escape_chars)
        date=$(git log -n1 --pretty=format:%cD $1 | escape_chars)
        echo "{\"sha\":\"$sha\",\"message\":\"$message\",\"author\":\"$author\",\"commit\":\"$commit\",\"date\":\"$date\"}"
    }
    
    for hash in $(git rev-list)
    do
      format $hash
    done
    

    The above will escape { and } and not \, although from JSON.org both \{ and \} are invalid escapes; only \ and " need to be escaped. (Replace the sed expression with sed -r 's/("\\)/\\\1/g' for true JSON output.)

    I’ve also left the “commit” value as it is in your example, although the %cE command actually gives the commiter’s email address; I’m not sure if that’s what you intended.

    (This now includes a correct but rejected edit by macrobug. Thanks!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to post a message on the user's facebook wall using access token.
I am installing static file using CMake's INSTALL command. I want to post process
I want to post a form on html and process the variables with asp.net
I want to post a forma poupup window. Problem is, the form posts successfully
I want to post some data to a file via ajax when somebody clicks
I want to post in memory some child rows, and then conditionally post them,
I want to post some data via javascript to another domain. Something like: http://www.othersite.com/submitfunnyname?name=blah
I want to post some values from a simple HTML form, validate them with
I want to post on twitter wall but don't want that user can edit
I simply want to post an image and comment from my own site to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.