Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I restrict git status to regular files in the current directory only?

Tags:

I would like to see the status of the current directory. Because there are lots of changes in sub-directories, which I do not want to see, the following command doesn't do the trick:

git status .

Is there any way of getting this kind of report, short of grepping the output of git status?

like image 552
blueFast Avatar asked Mar 18 '15 14:03

blueFast


People also ask

What is the command to get the current status of the git repository?

The git log command is Git's basic tool for exploring a repository's history. It's what you use when you need to find a specific version of a project or figure out what changes will be introduced by merging in a feature branch.

What is the difference between git diff and git status?

The main difference between the commands is that git diff is specially aimed at comparisons, and it's very powerful at that: It can compare commits, branches, a single file across revisions or branches, etc. On the other hand, git status is specifically for the status of the working tree.

What is git clean command?

Summary. To recap, git clean is a convenience method for deleting untracked files in a repo's working directory. Untracked files are those that are in the repo's directory but have not yet been added to the repo's index with git add .

What is git status porcelain?

Porcelain Format Version 1 Version 1 porcelain format is similar to the short format, but is guaranteed not to change in a backwards-incompatible way between Git versions or based on user configuration. This makes it ideal for parsing by scripts.


2 Answers

Use git-status -- <pathspec>...

The synopsis of the git-status man page tells you that you can filter by paths:

git status [<options>...] [--] [<pathspec>...]

Therefore, all you have to do is get a list of paths corresponding to the regular (non-directory) files in the current directory, and pass that to git-status.

There is one gotcha: because git status reports about the whole repository if passed an empty <pathspec>... argument, you need to check whether the list is empty or not.

Shell script

Here is a small shell script that does what you want.

#!/bin/sh

# git-status-dot
#
# Show the status of non-directory files (if any) in the working directory
#
# To make a Git alias called 'status-dot' out of this script,
# put the latter on your search path, make it executable, and run
#
#   git config --global alias.status-dot '! git-status-dot'

# Because GIt aliases are run from the top-level directory of the repo,
# we need to change directory back to $GIT_PREFIX.
[ "$GIT_PREFIX" != "" ] && cd "$GIT_PREFIX"

# List Non-Directory Files in the Current Directory
lsnondirdot=$(ls -ap | grep -v /)

# If "lsnondirdot" is not empty, pass its value to "git status".
if [ -n "$lsnondirdot" ]
then
    git status -- $lsnondirdot
else
    printf "No non-directory files in the working directory\n"
fi

exit $?

For more details about why the GIT_PREFIX shenanigans are required, see git aliases operate in the wrong directory.

The script is available at Jubobs/git-aliases on GitHub.

Make a Git alias out of it

For convenience, you can create a Git alias that calls the script; make sure the script is on your path, though.

git config --global alias.statusdot '!sh git-status-dot.sh'

Toy example

Here is a toy example demonstrating how to use the resulting alias and what it does.

# initialize a repo
$ mkdir testgit
$ cd testgit
$ git init

# create two files
$ mkdir foo
$ touch foo/foo.txt
$ touch bar.txt

# good old git status reports that subdir/test1.txt is untracked... 
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    bar.txt
    foo/

nothing added to commit but untracked files present (use "git add" to track)
# ... whereas our new alias, git status-dot, only cares
# about regular files in the current directory
$ git status-dot
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    bar.txt

nothing added to commit but untracked files present (use "git add" to track)

# and if we delete README.md ...
$ rm README.md
# ... good old git status still bother us about /subdir ...
$ git status
Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    foo/

nothing added to commit but untracked files present (use "git add" to track)
# ... whereas git statusdot doesn't
$ git status-dot
No non-directory files in the working directory
$
like image 129
jub0bs Avatar answered Oct 13 '22 11:10

jub0bs


Maybe there's a more proper way to do this, but you can simply

find . -maxdepth 1 -type f -print0 | xargs -0 git status

Of course, this fails if there's no regular files on the current directory. In that case, you can use the extra ugly version

find . -maxdepth 1 -type f -print0 | xargs -0 git status nonexistentfile

And, no, I'm not going to address the case where you have a file named nonexistentfile.

like image 23
loopbackbee Avatar answered Oct 13 '22 09:10

loopbackbee