-1

I was working on the wrong branch called wrong_branch and I committed and tried to push my new files to this wrong branch. However, I got an error: current wrong_branch branchhas no upstream branch - that's when I realised that I was on the wrong branch.

Then I made a number of mistakes:

  1. I tried to checkout the branch I should be working on called should_be_branch. Realised my new files weren't there obviously
  2. Then, I checked out wrong_branch to see my new files.
  3. Then I tried to use the trick: git stash > git checkout should_be_branch > git stash apply. However, the new files did not get copied over to should_be_branch
  4. Then I tried to check out wrong_branchbut I am getting an error: example_file.py: needs mergeerror: you need to resolve your current index first

I am afraid if I do anything more that I will lose my new files.

Can you please help me retrieve them?

thor
  • 281
  • 1
  • 6
  • 13
  • Everythings fine, You haven't lost any data. You've made commits to `wrong_branch`, right? And the latest commits are the ones you need on the branch `should_be_branch`, right? So using `stash` won't work because it stashes your current unstaged files. So answer my 2 questions and we'll take it from there. – clamentjohn Jul 02 '21 at 12:41
  • Thanks. 1) Yes I have committed to `wrong_branch` 2) Yes latest commits are the ones you need on the branch `should_be_branch` – thor Jul 02 '21 at 13:04

1 Answers1

2

Since you already have commits on wrong_branch your commits are safe. Now you simply need to move those commits from wrong_branch to should_be_branch.

Here is an example scenario

  1. You have 2 commits in wrong_branch but you should have made those commits in should_be_branch. New commits are marked as x.
o----o----o----o----o----o                <-- should_be_branch
           \
            \--o--------------x1----x2    <-- wrong_branch (HEAD)
  1. You can cherry-pick those commits (x). This way you are not deleting the commit x in wrong_branch, but copying the commit to the should_be_branch.
git checkout should_be_branch
git cherry-pick -x <commit id of x1>

o----o----o----o----o----o----x1          <-- should_be_branch (HEAD)
           \
            \--o--------------x1----x2    <-- wrong_branch

But you may have conflicts. In this case the files will have git conflict markers like these

<<<<<<< HEAD:file.txt
Hello world
=======
Goodbye
>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt

What you have to do here is to make the appropriate changes to the files. Refer to resolving a commit.

  1. Do the same for the next commit too x2
$ git cherry-pick -x <commit id of x2>

o----o----o----o----o----o----x1----x2    <-- should_be_branch (HEAD)
           \
            \--o--------------x1----x2    <-- wrong_branch

If you have merge conflicts, resolve them

  1. Delete the two commits x1 and x2 from wrong_branch
git checkout wrong_branch
git reset --hard HEAD~1 # delete the last commit
# git reset --hard HEAD~2 # delete the last 2 commits

o----o----o----o----o----o----x1----x2    <-- should_be_branch
           \
            \--o                          <-- wrong_branch (HEAD)

Read more on deleting here - Link

clamentjohn
  • 3,417
  • 2
  • 18
  • 42
  • Note: I used `cherry-pick` so that it is easier for you to see commits being copied. And you only need to delete the other commits if you are happy with what you have in `should_be_branch` – clamentjohn Jul 02 '21 at 13:33
  • Thanks. First I need to resolve example_file.py: needs merge error: you need to resolve your current index first. What do you suggest for this? – thor Jul 02 '21 at 13:48
  • 1
    @thor you have a merge error because you `git stash apply`'ed what ever you had in your stash. See what are those changes. Identify if they are changes you need (maybe these are uncommited, but important, changes). And if not needed abort the merge `git merge --abort` – clamentjohn Jul 02 '21 at 13:54
  • I did step 2 in your answer. I got : On branch should_be_branch The previous cherry-pick is now empty, possibly due to conflict resolution. If you wish to commit it anyway, use: git commit --allow-empty Otherwise, please use 'git cherry-pick --skip' Your branch is up to date with 'origin/FPNs'. You are currently cherry-picking commit 17ce3c3. (all conflicts fixed: run "git cherry-pick --continue") (use "git cherry-pick --skip" to skip this patch) (use "git cherry-pick --abort" to cancel the cherry-pick operation) nothing to commit, working tree clean Should I except this? – thor Jul 02 '21 at 14:17