-2

I'm in project tree and I'm currently looking at the master branch.

I did some changes, not realizing that I'm not on the feature branch, tested them and now I need to merge thenm to the branch instead of the master.

However, it looks like merging is possible only to the current branch and not from the branch.

Is there a simple way to do that without reverting and re-applying the changes?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Igor
  • 5,620
  • 11
  • 51
  • 103
  • please clarify the question naming your branches and what's the situation you would like to achieve, ex. do you want to maintain the commits on master? did the "feature branch" exist with changes before you did your changes to master? Why a simple merge should not work? Which commands are you executing? why a simple `git checkout -b my-feature-branch` from the master branch is not enough? – verodigiorgio May 04 '21 at 22:08
  • It sounds to me like you are having Regret #3 as explained here: https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard/59675191#59675191 – matt May 04 '21 at 22:17
  • @verodigiorgio, I cqan't do `git checkout -b ticket_X` - branch already exist. I can't do `git checkout ticket_X` - `error: Your local changes to the following files would be overwritten by checkout:`. But I want my changes that I accidentally made on master, be merged into `ticket_X`. The branch `ticket_X` exist and it is clean - no changes there. Because `git merge` works other way around. – Igor May 04 '21 at 22:18
  • 1
    So in my scenario you would create a branch off master containing these commits and reset master. Now the commits are on the branch. OK but the branch should be off of feature. So rebase it. – matt May 04 '21 at 22:19

1 Answers1

0

If you didn't commit them yet, you can use the git stash command, switch to the correct branch, then un-stash them and commit in the correct place.

If you've already committed, you'll need to rearrange which branch points where, probably through a combination of git checkout -b, git reset --hard and git rebase -i commands. The precise details depend on where exactly you are and what you need.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17