"git status" in short or short format, for example, "ls -1"?

I need to take a bunch of files that have been changed, and scp them into different fields for testing. I am having trouble figuring out how to make git status give me a list like ls -1 so that I can script to perform actions with minimal effort.

I have an existing script that does what I need using ls -1 . I am not a gifted script, so I do not want to modify the script. Instead, I would like the tool to change its output.

Obviously git status -1 not working. Format How can I get 'git status' to always use a short format , not compatible with my script. And git status --column produced the same result as below.

How do I change the git status list of modified files, one per line, only with the modified file in line?


 $ git status On branch master Your branch and 'origin/master' have diverged, and have 1 and 2 different commits each, respectively. (use "git pull" to merge the remote branch into yours) Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: cryptest.vcproj modified: dlltest.vcproj Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: adler32.cpp modified: algebra.cpp modified: algparam.cpp modified: asn.cpp modified: asn.h modified: authenc.cpp modified: authenc.h modified: basecode.cpp modified: cast.cpp modified: ccm.cpp modified: cmac.cpp modified: config.h modified: cryptdll.vcproj modified: cryptlib.cpp modified: cryptlib.h modified: cryptlib.vcproj modified: datatest.cpp modified: dlltest.cpp modified: eax.cpp modified: ec2n.cpp modified: eccrypto.cpp modified: ecp.cpp modified: emsa2.cpp modified: eprecomp.cpp modified: esign.cpp modified: files.cpp modified: filters.cpp modified: filters.h modified: fips140.cpp modified: fipsalgt.cpp modified: fltrimpl.h modified: gf2_32.cpp modified: gf2n.cpp modified: gf2n.h modified: gfpcrypt.cpp modified: gfpcrypt.h modified: hkdf.h modified: hmac.cpp modified: hrtimer.cpp modified: ida.cpp modified: idea.cpp modified: integer.cpp modified: iterhash.cpp modified: luc.h modified: misc.cpp modified: misc.h modified: modes.cpp modified: modes.h modified: nbtheory.cpp modified: network.cpp modified: oaep.cpp modified: panama.cpp modified: pkcspad.cpp modified: polynomi.cpp modified: pssr.cpp modified: pubkey.h modified: pwdbased.h modified: queue.cpp modified: rijndael.cpp modified: rsa.cpp modified: rw.cpp modified: salsa.cpp modified: seal.cpp modified: secblock.h modified: simple.h modified: smartptr.h modified: socketft.cpp modified: socketft.h modified: sosemanuk.cpp modified: strciphr.cpp modified: strciphr.h modified: test.cpp modified: validat1.cpp modified: validat2.cpp modified: vmac.cpp modified: wait.cpp modified: winpipes.cpp modified: winpipes.h modified: words.h modified: xtr.cpp modified: xtr.h modified: zdeflate.cpp modified: zinflate.cpp 
+5
source share
4 answers

git status --porcelain

This will be output in short format, but will be consistent across all versions of Git.

git-static documentation

Here is an excerpt from the link:

- china
Give the result in a simple script analysis format. This seems like a short exit, but will remain stable through Gitversion and regardless of user configuration.

+7
source

As Thomas suggested, the --porcelain option is what you want. But of course, in order to get the JUST file name of the modified files, you need to also analyze this output. For example, the output pipeline may work through a simple sed script:

 git status --porcelain | sed -ne '/^M */s///p' 

This script searches for M and spaces at the beginning of the line. If they are found, they are deleted and the line is printed. If they are not found (i.e. the Output is something other than the official M file), the output is not output.

The command line result above should be roughly equivalent to what you get from ls -1 - that is, a list of files, without any other information. Note that --porcelain terminates file names with NULL, so if the output does not work as you expect, you may need to process it in a script or in another channel. (Read about xargs(1) ).

+2
source

If you have unspecified changes, you can list all the affected files in a list with a single column like ls -1 with this command:

git ls-files --modified --others --exclude-standard --directory

The --modified parameter shows modified files, and the --others parameter shows other files without a trace, such as new files; --exclude-standard ignores excluded files, and --directory lists only the directory names, not the files inside them. See the documentation for more details.

Here is the command I use to quickly copy all changed and new files to my temporary directory, keeping the full path to the directory (dirty alternative to git stash ):

 git ls-files --modified --others --exclude-standard --directory | xargs -I {} cp --parents {} ~/temp 

If you have already added the files to git that you need to commit (for example, git add -A . ), You can use the command in the accepted answer and extract the second column to get your simple list of files:

git status --porcelain | awk '{ print $2 }'

If you want to filter the lines by git status, you can do something like this:

git status --porcelain | grep ^[AM] | awk '{ print $2 }'

This gives you a list containing only modified ( M ) and recently added ( A ) files.


Update:

  • Added @jotik suggestion.
+2
source

The following commands may be useful for a modified file list.

git diff --name-only

or

git diff --name-status

int displays status -name-status, status (A, M, D) and the file name are separated by a tab, you can analyze it with the cut .

If you can also get a list of files during configuration, you can use the `--cache 'option.

+1
source

All Articles