All blogs
/Home
/Hashnode Archive
/Moving a Commit to a New Branch without Losing Progress! ๐Ÿ’ป๐Ÿ”ง
JavaScriptSeptember 6, 2024

Moving a Commit to a New Branch without Losing Progress! ๐Ÿ’ป๐Ÿ”ง

Sometimes we realize after committing that the changes belong on a different branch. Instead of manually copying files, here's a clean, simple way to "move" your last commit to a new branch! ๐Ÿ‘‡ 1๏ธโƒฃ Reset the Last Commit (Keep Changes Staged) git rese...

Open on Hashnode

Sometimes we realize after committing that the changes belong on a different branch. Instead of manually copying files, here's a clean, simple way to "move" your last commit to a new branch! ๐Ÿ‘‡

1๏ธโƒฃ Reset the Last Commit (Keep Changes Staged)

git reset --soft HEAD~1

This removes your last commit but keeps the changes in your staging area.

2๏ธโƒฃ Stash the Changes

git stash

Temporarily save the changes so you can apply them later.

3๏ธโƒฃ Create or Switch to a New Branch

 git checkout -b new-branch-name

4๏ธโƒฃ Reapply the Stashed Changes

git stash pop

Your changes are back and ready to be committed on the new branch.

๐Ÿ’ก It's a great trick for keeping your branches clean and making sure commits are organized. No lost work, and no confusion! ๐ŸŽ‰

#DevTips #GitTips #SoftwareDevelopment #CleanCode #VersionControl #FrontendEngineering #BackendEngineering