In rebase, one can pass the `--autostash` option to cause the worktree
to be automatically stashed before continuing with the rebase. This
option is missing in merge, however.
Implement the `--autostash` option and corresponding `merge.autoStash`
option in merge which stashes before merging and then pops after.
This option is useful when a developer has some local changes on a topic
branch but they realize that their work depends on another branch.
Previously, they had to run something like
git fetch ...
git stash push
git merge FETCH_HEAD
git stash pop
but now, that is reduced to
git fetch ...
git merge --autostash FETCH_HEAD
When an autostash is generated, it is automatically reapplied to the
worktree only in three explicit situations:
1. An incomplete merge is commit using `git commit`.
2. A merge completes successfully.
3. A merge is aborted using `git merge --abort`.
In all other situations where the merge state is removed using
remove_merge_branch_state() such as aborting a merge via
`git reset --hard`, the autostash is saved into the stash reflog
instead keeping the worktree clean.
Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Suggested-by: Alban Gruin <alban.gruin@gmail.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Denton Liu committedApr 7, 2020 at 10:28 UTCa03b55530ab844974b7058042a015fcfcd9e7a53
12 files changed+214-6
Documentation/config/merge.txt
+10
index 6a313937f8..cb2ed58907 100644--- a/Documentation/config/merge.txt+++ b/Documentation/config/merge.txt@@ -70,6 +70,16 @@ merge.stat:: Whether to print the diffstat between ORIG_HEAD and the merge result at the end of the merge. True by default.+merge.autoStash::+ When set to true, automatically create a temporary stash entry+ before the operation begins, and apply it after the operation+ ends. This means that you can run merge on a dirty worktree.+ However, use with care: the final stash application after a+ successful merge might result in non-trivial conflicts.+ This option can be overridden by the `--no-autostash` and+ `--autostash` options of linkgit:git-merge[1].+ Defaults to false.+ merge.tool:: Controls which merge tool is used by linkgit:git-mergetool[1]. The list below shows the valid built-in values.
Documentation/git-merge.txt
+8-3
index 092529c619..ec06b2f8c2 100644--- a/Documentation/git-merge.txt+++ b/Documentation/git-merge.txt@@ -94,7 +94,8 @@ will be appended to the specified message. --abort:: Abort the current conflict resolution process, and- try to reconstruct the pre-merge state.+ try to reconstruct the pre-merge state. If an autostash entry is+ present, apply it to the worktree. + If there were uncommitted worktree changes present when the merge started, 'git merge --abort' will in some cases be unable to@@ -102,11 +103,15 @@ reconstruct these changes. It is therefore recommended to always commit or stash your changes before running 'git merge'. + 'git merge --abort' is equivalent to 'git reset --merge' when-`MERGE_HEAD` is present.+`MERGE_HEAD` is present unless `MERGE_AUTOSTASH` is also present in+which case 'git merge --abort' applies the stash entry to the worktree+whereas 'git reset --merge' will save the stashed changes in the stash+reflog. --quit:: Forget about the current merge in progress. Leave the index- and the working tree as-is.+ and the working tree as-is. If `MERGE_AUTOSTASH` is present, the+ stash entry will be saved to the stash reflog. --continue:: After a 'git merge' stops due to conflicts you can conclude the
Documentation/merge-options.txt
+8
index 40dc4f5e8c..3985e6d4a9 100644--- a/Documentation/merge-options.txt+++ b/Documentation/merge-options.txt@@ -155,6 +155,14 @@ ifndef::git-pull[] Note that not all merge strategies may support progress reporting.+--autostash::+--no-autostash::+ Automatically create a temporary stash entry before the operation+ begins, and apply it after the operation ends. This means+ that you can run the operation on a dirty worktree. However, use+ with care: the final stash application after a successful+ merge might result in non-trivial conflicts.+ endif::git-pull[] --allow-unrelated-histories::