Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please commit your changes or stash them before you merge

Tags:

git

I want to upgrade, but I need to commit, but when I do git commit, I get:

Your branch is behind 'origin/develop' by 20 commits, and can be fast-forwarded.

(use "git pull" to update your local branch)

I don't want to lose my local changes

like image 699
Самир Шахмурадлы Avatar asked Feb 18 '19 11:02

Самир Шахмурадлы


2 Answers

Use these terminal commands from your project directory.

Stash your local changes using git stash. This will save your local changes, after the last commit in your local, to a stack.

Pull changes from remote using git pull or git pull <remote-name> <branch-name> if you are pulling from branch other than master. This will pull the commits from the remote branch to local that you don't have.

Pop back your changes from stash using git stash pop. This will apply back the uncommitted changes. This may result in merge conflicts in your code.

You can commit the changes after resolving the conflicts.

You could also pull changes without stashing, but that may too result in merge conflicts, which you have to resolve.

like image 152
uneq95 Avatar answered Sep 28 '22 17:09

uneq95


Please commit your changes or stash them before you merge

You won't see that message anymore with Git 2.27 (Q2 2020), because "git merge" learned the "--autostash" option and the merge.autoStash new setting.

See commit d9f15d3, commit f8a1785, commit a03b555, commit 804fe31, commit 12b6e13, commit 0dd562e, commit 0816f1d, commit 9bb3dea, commit 4d4bc15, commit b309a97, commit f213f06, commit 86ed00a, commit facca7f, commit be1bb60, commit efcf6cf, commit c20de8b, commit bfa50c2, commit 3442c3d, commit 5b2f6d9 (07 Apr 2020), commit 65c425a (04 Apr 2020), and commit fd6852c, commit 805d9ea (21 Mar 2020) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit bf10200, 29 Apr 2020)

merge: teach --autostash option

Helped-by: Phillip Wood
Suggested-by: Alban Gruin
Signed-off-by: Denton Liu

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.

And:

pull: pass --autostash to merge

Signed-off-by: Denton Liu

Before, --autostash only worked with git pull --rebase. However, in the last patch, merge learned --autostash as well so there's no reason why we should have this restriction anymore. Teach pull to pass --autostash to merge, just like it did for rebase.

So in your case:

git config --global merge.autostash true
git pull

And you won't lose your local changes!


The local changes stashed by "git merge --autostash"(man) were lost when the merge failed in certain ways, which has been corrected with Git 2.33 (Q3 2021).

See commit e082631, commit 12510bd, commit fd441eb, commit 9938f30 (23 Jul 2021) by Philippe Blain (phil-blain).
(Merged by Junio C Hamano -- gitster -- in commit 5fef3b1, 04 Aug 2021)

Documentation: define 'MERGE_AUTOSTASH'

Signed-off-by: Philippe Blain

The documentation for 'git merge --abort'(man) and 'git merge --quit'(man) both mention the special ref 'MERGE_AUTOSTASH', but this ref is not formally defined anywhere.
Mention it in the description of the '--autostash' option for 'git merge'(man).

merge-options now includes in its man page:

--autostash::

--no-autostash::

Automatically create a temporary stash entry before the operation begins, record it in the special ref MERGE_AUTOSTASH and apply it after the operation ends.
This means that you can run the operation on a dirty worktree.

This is now more rebust:

merge: apply autostash if fast-forward fails

Signed-off-by: Philippe Blain

Since 'git merge'(man) learned '--autostash' in a03b555 ("merge: teach --autostash option", 2020-04-07, Git v2.27.0-rc0 -- merge listed in batch #5), 'cmd_merge', in the fast-forward case, calls 'create_autostash' before calling 'checkout_fast_forward' if '--autostash' is given.

However, if 'checkout_fast_forward' fails, the autostash is not applied to the working tree, nor saved in the stash list, since the code simply calls 'goto done'.

Be more helpful to the user by applying the autostash in that case.

An easy way to test a failing fast-forward is when we are merging a branch that has a tracked file that conflicts with an untracked file in the working tree.

And:

merge: apply autostash if merge strategy fails

Signed-off-by: Philippe Blain

Since 'git merge'(man) learned '--autostash' in a03b555 ("merge: teach --autostash option", 2020-04-07, Git v2.27.0-rc0 -- merge listed in batch #5), 'cmd_merge', once it is determined that we have to create a merge commit, calls 'create_autostash' if '--autostash' is given.

As explained in a03b555, and made more abvious by the tests added in that commit, the autostash is then applied if the merge succeeds, either directly or by committing (after conflict resolution or if '--no-commit' was given), or if the merge is aborted with 'git merge --abort'(man).
In some other cases, like the user calling 'git reset --merge'(man) or 'git merge --quit'(man), the autostash is not applied, but saved in the stash list.

However, there exists a scenario that creates an autostash but does not apply nor save it to the stash list: if the chosen merge strategy completely fails to handle the merge, i.e.
'try_merge_strategy' returns 2.

Apply the autostash in that case also.
An easy way to test that is to try to merge more than two commits but explicitly ask for the 'recursive' merge strategy.

like image 40
VonC Avatar answered Sep 28 '22 18:09

VonC