0

I work in C# with Unity, and it often touches a lot of files that don't want to commit. My changes that I want to commit are almost always exclusively .cs files. I created a git alias to stage all of my .cs file changes while ignoring everything else. That alias worked, but I realized that after doing the add that I always end up double-checking the results by doing a git status, so I decided to combine the two into a single alias.

addcs = !git add ./\\*.cs && git status

This alias works, however, the git status always shows the results as if I was calling it from the parent branch.

So just doing a git status might show up like this:

On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: Scripts/Foo.cs 

Calling my alias, which calls a git status within it, shows up like this:

On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: ParentDirectory/Scripts/Foo.cs 

I tried an alternate alias, thinking that maybe the ./ within my alias might be somehow affecting the git status, but this alias doesn't behave any differently:

addcs = !git add '**/*.cs' && git status

I'm curious if anyone knows why the git status might be behaving this way, and if there's something I can change with my alias to avoid it.

1 Answer 1

0

You could add an unconditional refresh of the index between the addition and the status check with git update-index --really-refresh.

Like --refresh, but checks stat information unconditionally, without regard to the "assume unchanged" setting.

Your alias could be rewritten as follows:

git config --local alias.my-add '!sh -c "git add *.cs && git update-index --really-refresh && git status"' 
Sign up to request clarification or add additional context in comments.

6 Comments

I tried this and for all unstaged changes it lists the file and then says needs update afterward. It doesn't display any git status.
@NicFoster Weird... it worked for me i.sstatic.net/19gZ7hx3.png which Git version are you running? Mine is git version 2.45.2.windows.1
@dani-vta, I'm using 2.39.3 (Apple Git-146) on MacOS.
@NicFoster Have you tried updating? I've included a link with a screenshot in my previous comment to show you the behavior. On my version it works as expected.
I'll look into updating, is this functionality fairly new to git? I checked the docs and it should exist even in 2.38.x versions, which are slightly older than mine: git-scm.com/docs/git-update-index/2.38.0
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.