-2

Let say that from the main branch I created a new branch feature/navbar. After the last commit on the feature/navbar, I push it, and then I merge it to the main branch. The next step is that now I want to work on the footer element. I'm coding and coding, but before I commit it and add the changes, I realise, that I didn't make a new branch feature/footer, but I'm still on feature/navbar. So my code for footer is on the feature/navbar branch. How can I bring the not committed changes from the feature/navbar to a brand new branch called feature/footer?

venndi
  • 161
  • 9
  • Does this answer your question? [Git create branch from range of previous commits?](https://stackoverflow.com/questions/9853681/git-create-branch-from-range-of-previous-commits) – blami Apr 27 '23 at 11:50

2 Answers2

3

Just switch branches. Your uncommitted "changes" will come along with you.

git switch -c feature/footer origin/main

As long as this would not harm your "changes", this will just work.

If it would harm your "changes", Git will object. In that case, make a branch, commit, and rebase:

git switch -c feature/footer
git add .
git commit -mfooter
git rebase --onto origin/main @~1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    I use the term "changes" in scare-quotes because you used it — but Git is not about "changes". It's true that `git status` reports these files as modified, but that is a conclusion that it draws based on the visible evidence, and is provided as a sort of convenience to you. You might want to learn more about what Git actually is, and in particular about the Three Worlds of Git: the repo (commits), the index, and your working tree (the stuff you can actually see). Try my https://www.biteinteractive.com/picturing-git-conceptions-and-misconceptions/ – matt Apr 27 '23 at 13:53
0

If you haven't added your new changes - just create a new branch now, then add it. If you have already added your changes to the main branch by mistake - just do a soft reset

1. go back 1 commit:

git reset --soft HEAD~1

2. Remove from staging

git reset .

Now the files are ready to re-commit into a branch of your choosing.

Steve Tomlin
  • 3,391
  • 3
  • 31
  • 63