Git: how to move changes to another branch in 3 simple steps

med bouzid
2 min readJun 4, 2021

--

Note: if you already did git add . then you can easily undo it using git reset (this will reset all the added files)

if you already committed the changes locally, then the “Already Committed changes locally” at the bottom of this article

Sometimes you start working in your code then you you realize that you are in the wrong branch, and the code you added should be in another one.

Let’s suppose you’re in “master” branch but you want the changes to be in “features” branch, what to do?

1 ) use git stash to put these changes off for a while.

git stash

2 ) go to your branch where you want to move the changes:

git checkout features

3 ) recover the changes that you stashed before and put them in the current branch:

git stash pop

Congrats! your changes has moved from “master” to “features” and you are ready to do git add . and git commit -m 'message'

Already Committed changes locally?

Recently I was in a situation where I committed the changes locally but not pushed to the remote repo, after some research I found this useful answer in stackoverflow (credit to the author) which I am going to put it here for easy access

1) Create new branch with your changes.

git checkout -b mybranch

2) (Optional) Push new branch code on remote server.

git push origin mybranch

3) Checkout back to master branch.

git checkout master

4) Reset master branch code with remote server and remove local commit.

git reset --hard origin/master

--

--

med bouzid

Project engineer, Serial entrepreneur, Life student, Author since birth.