diff --git a/src/plugins/git/branchdialog.cpp b/src/plugins/git/branchdialog.cpp
index d6f7bc209652a257f9c1542a356ad30fe0016c7b..28e72fc8381766d28d3eedb7937b348441fce157 100644
--- a/src/plugins/git/branchdialog.cpp
+++ b/src/plugins/git/branchdialog.cpp
@@ -255,14 +255,9 @@ void BranchDialog::merge()
     QTC_CHECK(idx != m_model->currentBranch());            // otherwise the button would not be enabled!
 
     const QString branch = m_model->branchName(idx);
-    GitClient *gitClient = GitPlugin::instance()->gitClient();
-    QString stashMessage;
-
-    if (gitClient->gitStatus(m_repository, StatusMode(NoUntracked | NoSubmodules)) == GitClient::StatusChanged)
-        gitClient->ensureStash(m_repository, QLatin1String("merge"), false, &stashMessage);
-
-    if (gitClient->synchronousMerge(m_repository, branch) && !stashMessage.isEmpty())
-        gitClient->stashPop(m_repository, stashMessage);
+    GitClient::StashGuard stashGuard(m_repository, QLatin1String("merge"), false);
+    if (!GitPlugin::instance()->gitClient()->synchronousMerge(m_repository, branch))
+        stashGuard.preventPop();
 }
 
 void BranchDialog::rebase()
@@ -272,14 +267,9 @@ void BranchDialog::rebase()
     QTC_CHECK(idx != m_model->currentBranch());            // otherwise the button would not be enabled!
 
     const QString baseBranch = m_model->branchName(idx);
-    GitClient *gitClient = GitPlugin::instance()->gitClient();
-    QString stashMessage;
-
-    if (gitClient->gitStatus(m_repository, StatusMode(NoUntracked | NoSubmodules)) == GitClient::StatusChanged)
-        gitClient->ensureStash(m_repository, QLatin1String("rebase"), false, &stashMessage);
-
-    if (gitClient->synchronousRebase(m_repository, baseBranch) && !stashMessage.isEmpty())
-        gitClient->stashPop(m_repository, stashMessage);
+    GitClient::StashGuard stashGuard(m_repository, QLatin1String("rebase"), false);
+    if (!GitPlugin::instance()->gitClient()->synchronousRebase(m_repository, baseBranch))
+        stashGuard.preventPop();
 }
 
 void BranchDialog::changeEvent(QEvent *e)
diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp
index d6a130950864cec4274ceb7ed553029d99c4ee64..283b52a66a76e43e3977fb5bc2b4dddd008181ae 100644
--- a/src/plugins/git/gitclient.cpp
+++ b/src/plugins/git/gitclient.cpp
@@ -2530,13 +2530,13 @@ unsigned GitClient::synchronousGitVersion(QString *errorMessage) const
     return version(major, minor, patch);
 }
 
-GitClient::StashGuard::StashGuard(const QString &workingDirectory, const QString &keyword) :
+GitClient::StashGuard::StashGuard(const QString &workingDirectory, const QString &keyword, bool askUser) :
     pop(true),
     workingDir(workingDirectory)
 {
     client = GitPlugin::instance()->gitClient();
     QString errorMessage;
-    stashResult = client->ensureStash(workingDir, keyword, true, &message, &errorMessage);
+    stashResult = client->ensureStash(workingDir, keyword, askUser, &message, &errorMessage);
     if (stashResult == GitClient::StashFailed)
         VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
 }
diff --git a/src/plugins/git/gitclient.h b/src/plugins/git/gitclient.h
index 9f77b0501b03e981d989b20754bdb7ef5659fc5a..9c3e0bb2c756d4611f21bd594e95e28baca95720 100644
--- a/src/plugins/git/gitclient.h
+++ b/src/plugins/git/gitclient.h
@@ -88,7 +88,7 @@ public:
     class StashGuard
     {
     public:
-        StashGuard(const QString &workingDirectory, const QString &keyword);
+        StashGuard(const QString &workingDirectory, const QString &keyword, bool askUser = true);
         ~StashGuard();
 
         void preventPop();