Check if Git has changes programmatically

Casebash

I want to write a script that allows me to easily change branches keeping my current changes.

git stash
go checkout main
git stash pop

The problem is if there are currently no changes in the repository. Then stash pop will pop off a previous stash. What is the easiest way to check if there are changes to pop?

torek

git stash is actually a shell (plain sh, not bash) script, so you can look at what it does to decide whether to make a stash:

no_changes () {
        git diff-index --quiet --cached HEAD --ignore-submodules -- &&
        git diff-files --quiet --ignore-submodules &&
        (test -z "$untracked" || test -z "$(untracked_files)")
}

untracked_files () {
        excl_opt=--exclude-standard
        test "$untracked" = "all" && excl_opt=
        git ls-files -o -z $excl_opt
}

You can therefore repeat these tests to see if git stash will do anything—but that's actually not the best option:

  • If git stash will do something, first it repeats these tests (and they say there is something to stash), which makes them happen twice in that case.
  • If the "right" set of tests ever change, you can mis-predict git stash's action and get into trouble.

Clearly, what you want is to:

  1. Run git stash, and keep track of whether this did anything.
  2. Run git checkout on the target branch.
  3. If git stash stashed something, run git stash pop (or perhaps --apply --keep-index first, to see if that will do the trick, and resort to applying without --keep-index only if required, etc.).

The tricky part is that first step. It turns out to be easy, you just need to know how git stash works in, um, extensive detail, plus some other bits about git's "plumbing commands". You simply grab the previous stash stack top if any before the git stash save step, and then grab the new top if any, and compare:

old_stash=$(git rev-parse -q --verify refs/stash)
git stash save [additional arguments here if desired, e.g., -q]
new_stash=$(git rev-parse -q --verify refs/stash)

If the two refs name different commits, or the old ref is empty and the new is not empty, then git stash pushed a new stash on the stack. If the two refs are both empty, or the two refs name the same commit, then git stash did nothing:

if [ "$old_stash" = "$new_stash" ]; then ...; else ...; fi

or, in your case, perhaps something more like:

[ "$old_stash" != "$new_stash" ] && git stash pop

(these tests make use of the fact that the two empty strings are equal to each other, and not-equal to any non-empty string, in terms of the result from /bin/[ and the shell's built-in copy thereof).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check if Git has changes programmatically

From Dev

How to check if a Git Repo has uncommitted changes by gitpython

From Dev

Check git pull changes

From Dev

Check if user has permision to truncate tables programmatically

From Dev

Check programmatically if device has NFC reader

From Dev

How to check if an NSDocument has unsaved changes?

From Dev

How to check if an NSDocument has unsaved changes?

From Dev

Git: Check out a branch keeping uncommitted changes

From Dev

How to check all changes in project under git?

From Dev

git rebase --continue: how to suppress the "no changes" check

From Dev

check if changes are staged for a new GIT repository

From Dev

How to check all changes in project under git?

From Dev

git rebase --continue: how to suppress the "no changes" check

From Dev

When to programmatically check for theme changes in Windows Phone 8.1

From Dev

Git - remove unstaged changes to a file that has staged changes

From Dev

Git - remove unstaged changes to a file that has staged changes

From Dev

Git proper way to commit changes when server has changes

From Dev

How to check if UILabel has attributedText or normal text programmatically?

From Dev

How to check if GIT has fully cloned a repository?

From Dev

What is the quickest way to check if an svn working copy has changes?

From Dev

Git: Warn when switching to a branch that has stashed changes

From Dev

Import using GIT-TF - item already has pending changes

From Dev

Import using GIT-TF - item already has pending changes

From Dev

Is there a git gui tool to check and commit changes in cloud9 editor?

From Dev

How to use git to check if local folder contains changes

From Dev

What is the fastest way to check recursively for changes in a Git working copy with jgit?

From Dev

Check to see if there are uncommited changes in any branch of git repo

From Dev

responding to dbCheckbox changes programmatically

From Dev

check if my commit has 'import pdb' in emacs/git?

Related Related

  1. 1

    Check if Git has changes programmatically

  2. 2

    How to check if a Git Repo has uncommitted changes by gitpython

  3. 3

    Check git pull changes

  4. 4

    Check if user has permision to truncate tables programmatically

  5. 5

    Check programmatically if device has NFC reader

  6. 6

    How to check if an NSDocument has unsaved changes?

  7. 7

    How to check if an NSDocument has unsaved changes?

  8. 8

    Git: Check out a branch keeping uncommitted changes

  9. 9

    How to check all changes in project under git?

  10. 10

    git rebase --continue: how to suppress the "no changes" check

  11. 11

    check if changes are staged for a new GIT repository

  12. 12

    How to check all changes in project under git?

  13. 13

    git rebase --continue: how to suppress the "no changes" check

  14. 14

    When to programmatically check for theme changes in Windows Phone 8.1

  15. 15

    Git - remove unstaged changes to a file that has staged changes

  16. 16

    Git - remove unstaged changes to a file that has staged changes

  17. 17

    Git proper way to commit changes when server has changes

  18. 18

    How to check if UILabel has attributedText or normal text programmatically?

  19. 19

    How to check if GIT has fully cloned a repository?

  20. 20

    What is the quickest way to check if an svn working copy has changes?

  21. 21

    Git: Warn when switching to a branch that has stashed changes

  22. 22

    Import using GIT-TF - item already has pending changes

  23. 23

    Import using GIT-TF - item already has pending changes

  24. 24

    Is there a git gui tool to check and commit changes in cloud9 editor?

  25. 25

    How to use git to check if local folder contains changes

  26. 26

    What is the fastest way to check recursively for changes in a Git working copy with jgit?

  27. 27

    Check to see if there are uncommited changes in any branch of git repo

  28. 28

    responding to dbCheckbox changes programmatically

  29. 29

    check if my commit has 'import pdb' in emacs/git?

HotTag

Archive