15

I am using GitHub desktop and git command line (to basically stash my code when I need to make a commit on another branch). I use git stash save and save my code. I then update another branch and switch back to my testing branch which i update to master. I then unstash my code with git stash pop

This is the output after I ran git stash pop:

Auto-merging update_registration.php 
CONFLICT (content):
Merge conflict in update_registration.php 
The stash entry is kept in case you need it again.

But then I end up with

    <<<<<<< Updated upstream
    //Code and whatnot
    =======
    <<<<<<< Updated upstream
            
    >>>>>>> Stashed changes

in my files and it is quite frustrating truing to manage the code and delete all these lines from my code to upload it to the server.

Am I using git stash wrong?

Thanks

3
  • Actually you omitted the output of the command. Remember git stash pop does not restore the files you changed, but the changes you made. If the changes failed to apply (thus asking for messages), you need to merge them manually. That is what those ===, <<< and >>> lines tell you.
    – U. Windl
    Mar 26, 2019 at 22:16
  • @U.Windl So should I use git stash apply? Right I do want to just restore the changes that I made before I left. This is the output after I ran git stash pop: Auto-merging update_registration.php CONFLICT (content): Merge conflict in update_registration.php The stash entry is kept in case you need it again. So I just need to figure out how to merge manually then? Mar 26, 2019 at 22:25
  • 3
    (I added the command output from comment to the question, but formatting got lost; maybe fix it). Yes, you'll have to merge. I do it with my editor, searching for ==== lines. Then you have three choices: 1: Pick the variant above the line (between <<< and ===), 2: Pick the variant below the line (between === and >>>), 3: Pick the one that mostly fits, making changes. In any case remove all those <<<, >>> and === lines. There are merge tools, but that's a different topic. Best try to minimize the need for merges.
    – U. Windl
    Mar 26, 2019 at 22:33

2 Answers 2

13

What happened was this: You changed a part ("Updated upstream") while your changes were stashed that was also changed in the stashed part ("Stashed changes"). So when restoring the stashed changes, there was a merge conflict.

General advice: Do different things on different branches at different times, or do different things in different workspaces concurrently. So if you are implementing something on branch A and you need to do something different on the project, and you have uncommitted changes, you have two choices:

  1. Do it in the same workspace: Then do git stash saving your uncommitted changes, create a new branch (probably on master) with git checkout -b <new_branch> master or checkout an existing branch. Then hack along, committing changes, finally return to your work using git checkout A and git stash pop. Changes should apply cleanly then.

  2. Do it in a different workspace: Do git clone of your repository in a different directory, hacking and comitting there. If you want to continue on the other branch, just change directories.

In any case you'll have to merge branches in the end if you want to integrate your work. The better you organize your work, the more often you will pick option 1 from the list above.

1
  • 1
    actually, no. This is not only a result of "not organizing someone's work well". Github Desktop shows a nasty habit of messing the code like this EVEN when there was NO change in the code, just switching between branches. Even though this is not an expected or uniform behaviour of Github Desktop, it happens in once of XX cases, so one should be aware of this and backup the code that is stashed, bc. it messes it up quite badly ... Dec 23, 2022 at 19:07
0

This is not only a result of "not organizing someone's work well". Github Desktop shows a nasty habit of messing with the code like this EVEN when there was NO change in the code, just switching between branches. Even though this is not an expected or uniform behavior of GitHub Desktop, it happens in one of XX cases, so one should be aware of this and back up the code that is stashed, bc. it messes it up quite badly ...

1
  • 1
    It might be a bug in git, too. Recently I had a case where both alternatives were identical, so I wondered why git could not resolve that "conflict".
    – U. Windl
    Dec 30, 2022 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.