Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can git tell me which uncommitted files clash with the incoming changes?

Tags:

git

git-merge

Please, observe:

C:\Dayforce\test [master ↓2 +0 ~2 -0 !]> git pull
error: Your local changes to the following files would be overwritten by merge:
        2.txt
Please commit your changes or stash them before you merge.
Aborting
Updating 2dc8bd0..ea343f8
C:\Dayforce\test [master ↓2 +0 ~2 -0 !]>

Does git have a command that can tell me which uncommitted files cause the this error? I can see them displayed by git pull, but I really do not want to parse git pull output.

I am fully aware of pull.rebase and rebase.autostash config options, please do not bring them up.

EDIT 1

It is OK to execute git pull first. In fact, the logic to identify the problematic files will be done after git pull fails with this reason. The way I recognize it in Powershell is:

git pull
# Possible exit codes:
# 1 - either local changes or pull merge conflict (but the merge has not been started yet)
# 128 - a merge is in progress
if ($LASTEXITCODE)
{
    git merge HEAD 2> $null                      # Disambiguate the exit code
    if ($LASTEXITCODE -eq 128)
    {
        # Two options:
        #  - pull merge conflict
        #  - a merge is in progress
        git mergetool
    }
    else
    {
        throw "Cannot pull due to uncommitted changes"
    }
}

So, instead of aborting I would like to identify the problematic files and essentially replicate the rebase.autostash, but without rebase.

EDIT 2

I used to think that git pull outputs something like this in case of clashes with uncommitted changes:

C:\xyz\test [master ↓4 ↑1 +0 ~3 -0 !]> git pull
error: Your local changes to the following files would be overwritten by merge:
        2.txt
        a.txt
Please commit your changes or stash them before you merge.
Aborting
C:\xyz\test [master ↓4 ↑1 +0 ~3 -0 !]>

Which is easy to parse. But today, I got something different:

C:\xyz\test [master ↓4 ↑1 +0 ~2 -0 | +0 ~1 -0 !]> git pull
error: Your local changes to the following files would be overwritten by merge:
  1.txt a.txt
C:\xyz\test [master ↓4 ↑1 +0 ~2 -0 | +0 ~1 -0 !]>

I do not know if this has something to do with my Powershell console having gotten botched somehow or with some recent git update, which I had installed automatically without noticing it.

like image 468
mark Avatar asked Dec 08 '18 03:12

mark


People also ask

Can you git pull with uncommitted changes?

When your uncommitted changes are significant to you, there are two options. You can commit them and then perform git pull , or you can stash them.


1 Answers

I do not know if this has something to do with my Powershell console having gotten botched somehow or with some recent git update

Git 2.19 has recently change how a merge reports this error, with commit 9270239 (Jul. 2018)

This is based on merge-recursive.c#merge_trees, which keeps track of two trees in order to detect common files being overwritten.
The problems are:

  • once the pull command fails, that information is not kept around
  • any Git state parsing should be done through plumbing, not porcelain commands

In your case, since git pull has completed its fetched part, but not its merge part, you could do a git diff -–name-only HEAD origin/yourFetchedBranch in order to compare the working tree with what is fetched.

That should list files which differs, including your 2.txt.

would it not give me all the files that are going to be changed and not just the conflicting ones?

Yes, it would.
For each file, you would still need to check if it is part of the current list of modified (not staged or staged but not committed) files:

git ls-files --other --modified --exclude-standard
git diff --name-only --cached
like image 200
VonC Avatar answered Oct 01 '22 11:10

VonC