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?
Asked
Active
Viewed 36 times
2 Answers
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
-
1I 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