IT

현재 지점에서 커밋 할 것이 없는지 확인하는 방법은 무엇입니까?

lottoking 2020. 6. 1. 08:10
반응형

현재 지점에서 커밋 할 것이 없는지 확인하는 방법은 무엇입니까?


목표는 쉘 명령으로 평가할 수있는 명확한 상태를 얻는 것입니다.

시도 git status했지만 커밋 할 항목이 있어도 항상 0을 반환합니다.

git status
echo $?  #this is always 0

아이디어가 있지만 오히려 나쁜 아이디어라고 생각합니다.

if [ git status | grep -i -c "[a-z]"> 2 ];
then
 code for change...
else
  code for nothing change...
fi

다른 방법?


다음 해결로 업데이트하십시오. Mark Longair의 게시물을 참조하십시오.

나는 이것을 시도했지만 문제를 일으킨다.

if [ -z $(git status --porcelain) ];
then
    echo "IT IS CLEAN"
else
    echo "PLEASE COMMIT YOUR CHANGE FIRST!!!"
    echo git status
fi

다음과 같은 오류가 발생합니다 [: ??: binary operator expected

이제 남자를보고 git diff를 시도하십시오.

==================== 내 희망에 대한 코드, 그리고 더 나은 답변을 희망하십시오

#if [ `git status | grep -i -c "$"` -lt 3 ];
# change to below code,although the above code is simple, but I think it is not strict logical
if [ `git diff --cached --exit-code HEAD^ > /dev/null && (git ls-files --other --exclude-standard --directory | grep -c -v '/$')` ];
then
        echo "PLEASE COMMIT YOUR CHANGE FIRST!!!"
    exit 1

else
    exit 0
fi

출력 git status --porcelain이 비어 있는지 테스트하는 대안 은 관심있는 각 조건을 개별적으로 테스트하는 것입니다. 예를 들어의 출력에 추적되지 않은 파일이있는 경우 항상 신경 쓰지 않을 수도 있습니다 git status.

예를 들어, 스테이지되지 않은 로컬 변경 사항이 있는지 확인하려면 다음의 리턴 코드를 볼 수 있습니다.

git diff --exit-code

준비되었지만 커밋되지 않은 변경 사항이 있는지 확인하려면 다음과 같은 반환 코드를 사용할 수 있습니다.

git diff --cached --exit-code

마지막으로, 작업 트리에 무시되지 않은 추적되지 않은 파일이 있는지 여부를 알고 싶다면 다음 명령의 출력이 비어 있는지 테스트 할 수 있습니다.

git ls-files --other --exclude-standard --directory

업데이트 : 아래에서 출력에서 ​​디렉토리를 제외하도록 해당 명령을 변경할 수 있는지 여부를 묻습니다. 를 추가하여 빈 디렉토리를 제외 할 수 --no-empty-directory있지만 해당 출력의 모든 디렉토리를 제외하려면 다음과 같이 출력을 필터링해야한다고 생각합니다.

git ls-files --other --exclude-standard --directory | egrep -v '/$'

-vegrep수단 만 출력 패턴과 일치하지 않는 선, 패턴은 A 라인으로 그 끝을 일치합니다 /.


의 반환 값은 커밋해야 할 수정 사항이없는 경우 git status종료 코드를 알려줍니다 git status.

보다 컴퓨터에서 읽을 수있는 git status출력 버전을 원한다면

git status --porcelain

이에 git status대한 자세한 정보 는 설명을 참조하십시오 .

샘플 사용 (스크립트는 단순히 git status --porcelain구문 분석이 필요없는 출력을 제공 하는지 테스트 ) :

if [ -n "$(git status --porcelain)" ]; then
  echo "there are changes";
else
  echo "no changes";
fi

테스트 할 문자열 (예 :의 출력)을 인용해야합니다 git status --porcelain. 테스트 구성에 대한 자세한 힌트는 Advanced Bash 스크립팅 안내서 (섹션 문자열 비교 )를 참조하십시오 .


당신이 나와 같다면 다음과 같은 것이 있는지 알고 싶습니다.

1) 기존 파일 변경 2) 새로 추가 된 파일 3) 삭제 된 파일

특히 4) 추적되지 않은 파일에 대해 알고 싶지 않습니다.

이것은해야합니다 :

git status --untracked-files=no --porcelain

Here's my bash code to exit the script if the repo is clean. It uses the short version of the untracked files option:

[[ -z $(git status -uno --porcelain) ]] && echo "this branch is clean, no need to push..." && kill -SIGINT $$;

It's possible to combine git status --porcelain with a simple grep to perform the test.

if git status --porcelain | grep .; then
    echo Repo is dirty
else
    echo Repo is clean
fi

I use this as a simple one-liner sometimes:

# pull from origin if our repo is clean
git status --porcelain | grep . || git pull origin master

Add -qs to your grep command to make it silent.


From the git source code there is a sh script which includes the following.

require_clean_work_tree () {
    git rev-parse --verify HEAD >/dev/null || exit 1
    git update-index -q --ignore-submodules --refresh
    err=0

    if ! git diff-files --quiet --ignore-submodules
    then
        echo >&2 "Cannot $1: You have unstaged changes."
        err=1
    fi

    if ! git diff-index --cached --quiet --ignore-submodules HEAD --
    then
        if [ $err = 0 ]
        then
            echo >&2 "Cannot $1: Your index contains uncommitted changes."
        else
            echo >&2 "Additionally, your index contains uncommitted changes."
        fi
        err=1
    fi

    if [ $err = 1 ]
    then
        test -n "$2" && echo >&2 "$2"
        exit 1
    fi
}

This sniplet shows how its possible to use git diff-files and git diff-index to find out if there are any changes to previously known files. It does not however allow you to find out if a new unknown file has been added to the working tree.


i'd do a test on this:

git diff --quiet --cached

or this to be explicit:

git diff --quiet --exit-code --cached

where:

--exit-code

Make the program exit with codes similar to diff(1). That is, it exits with 1 if there were differences and 0 means no differences.

--quiet

Disable all output of the program. Implies --exit-code


I'm a bit late in the discussion, but if it's only that you need to have an exit code of 0 if git status --porcelain returns nothing and != 0 else, try this:

exit $( git status --porcelain | wc -l )

This will make the number of lines be the exit code…


I'm using this in a script to have:

  • 0 when everything is clean
  • 1 when there is a diff or untracked files

    [ -z "$(git status --porcelain)" ]


Not pretty, but works:

git status | grep -qF 'working directory clean' || echo "DIRTY"

Not sure whether the message is locale dependent, so maybe put a LANG=C in front.

참고URL : https://stackoverflow.com/questions/5139290/how-to-check-if-theres-nothing-to-be-committed-in-the-current-branch

반응형