Git stash pop only if successfully hidden

Part of my workflow involves a lot of things:

  • git change shifts
  • git pull
  • Pop Hidden Changes
  • run mergetool to resolve conflicts

I am trying to write a script to perform all of these actions at once, so I can just call it from the terminal.

#!/bin/bash

# First stash our local changes
git stash

# Then git pull to update our repo
git pull

# Pop the stash
git stash pop

# Launch mergetool if necessary
git mergetool

The problem I am facing is that if I run this randomly and there are no changes to stash, git stash popsome (usually super old) stash is applied. What I want to do is run git stash poponly if I actually hid something. Is there any way to do this?

+4
source share
5 answers

, , , , , . -, , :

git fetch <remote> (e.g. git fetch origin)

, rebase :

git rebase <remote>/<branch> (e.g. git rebase origin/master)

, .

, git pull -no-commit:

git pull --no-commit

, .

+3

Xavier Álvarez codeWizard , , git stash . , git fetch git rebase (. Xavier), , rebase --autostash, , , git pull script. 1

, , . . , git stash save "force", git commit --allow-empty, . 2 , git stash save . , git stash save , , , . , . : git rev-parse SHA-1s "", git stash .

git rev-parse SHA-1:

$ git rev-parse refs/remotes/origin/master
2635c2b8bfc9aec07b7f023d8e3b3d02df715344

- , refs, SHA-1. : refs/heads/branch. , : refs/tags/tag, , , , origin/master, refs/remotes/origin/master.

stash script refs/stash, git rev-parse refs/stash. 3 git stash save, git stash save. , git stash save stash stash.

, ( , ), git rev-parse SHA-1:

$ git rev-parse refs/stash
fatal: ambiguous argument 'refs/stash': unknown revision or path not in
the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

, git rev-parse -q --verify refs/stash, , , script, :

oldsha=$(git rev-parse -q --verify refs/stash)
git stash -q save  # add options as desired here
newsha=$(git rev-parse -q --verify refs/stash)
if [ "$oldsha" = "$newsha" ]; then
    made_stash_entry=false
else
    made_stash_entry=true
fi
... all of your other code goes here ...
if $made_stash_entry; then git stash pop; fi

1 git pull git fetch, git merge, , , git fetch, - git rebase. , , , .

2 create store: , SHA-1, stash- , , . git, , , (, , stash, , , , git).

3 , git rev-parse stash stash. : ( --, ), , git , , , , .

+5

git? , stash. , . bool, pop, .

+1

git stash save <messsage> , , .

, , , , , .

One line:

t=timestamp-$(date +%s); r=$(git stash save $t); v=$(echo $r|grep $t); if [ "$v" ]; then git stash list; echo "SAVED! NOW REMOVING..."; git stash drop stash@{0}; else echo "Nothing to Stash!"; fi; echo "Stashes: "; git stash list; echo "Done!"

Expanded:

# unique timestamp
t=timestamp-$(date +%s) 

# stash with message
r=$(git stash save $t)

# check if the value exists
v=$(echo $r|grep $t)

# if the message is found...
if [ "$v" ] then 

  # DEBUG: Before
  git stash list
  echo "SAVED! NOW REMOVING..."

  # remove last stash
  git stash drop stash@{0}
else 
  echo "Nothing to Stash!"
fi

# DEBUG: after
echo "Stash List: "
git stash list
echo "Done!"
+1
source

you should try to avoid covering up if you do a lot of things like a script, instead just pass in your code and then pull on your changes.

You can also write an alias instead of a script:

git config --global alias.<your alias> "git add. && git commit &1 && git pull"

The above command will accept the commit message as a parameter and make git add, commit and pull

0
source

Source: https://habr.com/ru/post/1618758/


All Articles