If I run this command:
$ git status
I get:
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
This is pretty difficult to parse.
But what would be really nice is --json output, in another world, I'd love to see:
$ git status --json
and get this:
{
"currentBranch": "master",
"remoteTrackingBranch": "origin/master",
"isUpToDateWithRemote": true,
"workingDirectoryClean": true
}
is there some tool in the NPM ecosystem that can parse Git output into JSON? What is the best way to parse the output from git status
, etc?
To check the status, open the git bash, and run the status command on your desired directory.
The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.
Git commands are a distributed version control system for tracking changes in any set of files. They were originally designed for coordinating work among programmers who were operating source codes during software development.
There are three commands with similar names: git reset , git restore and git revert .
This is no JSON, but git status
has a --porcelain
option:
Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across Git versions and regardless of user configuration.
See porcelain format v1 and v2:
Version 2 format adds more detailed information about the state of the worktree and changed items. Version 2 also defines an extensible set of easy to parse optional headers.
Header lines start with "#" and are added in response to specific command line arguments. Parsers should ignore headers they don’t recognize.
vonc@voncvb C:\test
> git status --porcelain=v2 --branch
# branch.oid a4a9ae9616e5f1da136a3ff717e722d055ca9aa7
# branch.head master
# branch.upstream origin/master
1 .M N... 100644 100644 100644 67f7a2a439ffb9dd18dd65bb6fd296f8c16c55b3 67f7a2a439ffb9dd18dd65bb6fd296f8c16c55b3 test/file1.txt
1 .M N... 100644 100644 100644 d59cac0c8acf674ba3316944451dcbec3e6ec3d7 d59cac0c8acf674ba3316944451dcbec3e6ec3d7 test/file2.txt
See as an example robertgzr/porcelain
, which parses git status --porcelain=v2 --branch
and outputs nicely formatted strings for your shell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With