Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to git stash pop with dry run?

Tags:

git

Does git stash pop blindly overwrites the local changes or merges them with the local changes?

If it merges, there could be merge conflicts. Is there any way to know in advance, that there would be merge conflicts or not by using --dry-run or some other method?

I could find --dry-run with git fetch and git push but not with git stash pop.

EDIT explaining the problem specifically after few answers -

I have a situation, where I have a git stash that is around a couple of days old. The local branch has been modifed "after" the git stash was done. That means, there is a difference between the local and the stashed files ( all files are tracked and there are no untracked files in either stash or local branch ) .

The problem is that, I guess, once I do a git stash apply, the local changes will be in a conflict state in case of a complicated merge operation and hence cannot be reversed ( as there is no undo of the merge operation ). git checkout wont help, because there were local changes in the local branch. Is my understanding correct?

I wanted to do a dry-run before to see the results before doing a git stash apply. So, before I do anything and cry later, I wanted to find an elegant solution to this problem.

like image 750
infoclogged Avatar asked Jan 03 '18 12:01

infoclogged


2 Answers

is it possible to git stash pop with dry run?

I am not sure about dry run, but you may apply the stash instead of poping it (to keep a copy of the stash, in case the changes do not fit, and you want to apply them later again):

git stash apply

Then watch the results, if you do not like them, just checkout the changes to a previous state, or resolve the conflicts to ours. Otherwise, it they the changes were ok, resolve them to theirs.

git stash apply does not overwrite untracked files (it will faild to apply until you commit the uncommited files). In other words, git stash pop/apply is a safe process, and will not cause any loss of data.

Update according to the changes in the question:

I guess, once I do a git stash apply, the local changes will be in a conflict state.

No, that will not happen, but if you commited the changes. I.e., the local changes will not be overwritten or lost in anyway. Git will complain that your changes must be commited before you apply the stash. See this, here I tried to apply stash where one of my changes are not commited (and they will be affected by the stash data):

error: Your local changes to the following files would be overwritten by merge:
file.txt
Please commit your changes or stash them before you merge.
Aborting

When you commit them, yes you will be able to apply the stash, here you may get conflicts.


as there is no undo of the merge operation

Sure there is, just revert the merge commit: git revert --no-commit <MergeCommitHash>.

Again, I confirm that git stash apply is a safe process. It will refuse to overwrite any uncommited changes (i.e., you are safe from losing any data), and in case you merged, and the results are not fine, just revert the merge commit.

If any thing is still unclear, I will try to help you it it.

like image 143
Mohammed Noureldin Avatar answered Nov 15 '22 18:11

Mohammed Noureldin


Git will usually try to protect your local changes whenever possible. This applies to both checking out changes and also to applying stashes. If you attempt to apply a stash that would affect changes in files that have local uncommited changes, you will get the following error message:

error: Your local changes to the following files would be overwritten by merge:
        conflicting-file.ext
Please commit your changes or stash them before you merge.
Aborting

So you can safely try to apply the stash and see if it would succeed or not.

That being said, when there are no local uncommited changes, applying stashes will attempt to merge the changes with the current version. While doing this, merge conflicts may occur which you will then have to resolve manually.

Usually, this is not a problem but there is one problem when using git stash pop for this: git stash pop will apply the stash, possibly causing a merge conflict, and then delete the stash. If you then have problems resolving the merge conflict, you might want to be able to look back at the original stash which you then cannot do.

So I would always advise you to use git stash apply instead, which will just apply the stash but keep it around. And then, when the stash is successfully applied and you have resolved any possible merge conflict successfully, you can remove the stash using git stash drop. So this is a safe alternative to git stash pop.

like image 24
poke Avatar answered Nov 15 '22 18:11

poke