Skip to content

Instantly share code, notes, and snippets.

@tonylukasavage
Created May 29, 2013 18:18
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save tonylukasavage/5672490 to your computer and use it in GitHub Desktop.
Save tonylukasavage/5672490 to your computer and use it in GitHub Desktop.
Move all uncommitted changes to a new branch and revert the existing branch to HEAD. "master" has uncommitted changes. You decided you'd rather make those changes in "dev_branch". Here's how to move those uncommitted changes to "dev_branch" and then revert "master" to its last commit.
# get into the master branch
git checkout master
# create a new branch for the changes and check it out
git checkout -b dev_branch
# stash the changes until we revert master
git stash
# go back to master
git checkout master
# reset to the last commit
git reset --hard HEAD
# go back to dev_branch
git checkout dev_branch
# re-apply the stashed changes and you are good to go
git stash apply
@jessevdp
Copy link

👍

@sjbarlas
Copy link

Thanks, worked perfectly on sourcetree terminal too!

@jpwynn
Copy link

jpwynn commented Feb 19, 2018

huge help tonight, thank you for posting this!

@npadole20
Copy link

Thanks!

@saas-oxalide
Copy link

Works great. Thanks for sharing

@jfrank-summit
Copy link

Worked great! Thanks!

@SonGokussj4
Copy link

SonGokussj4 commented Sep 6, 2019

Thanks. I added dropping the stash on the end.

# delete stash
git stash drop

My whole bash function within .bashrc is like this:

function git_move_changes_new_branch {
    # Transfer current unsaved changes to new branch
    # Usage: (user@host) - (/my/project/path) [refactor] $ git_move_changes_new_branch newbranch

    usage="Usage: git_move_changes_new_branch new_branch_name

When you forget to make changes to new branch, this script will transfer unstaged changes
from CURRENT branch to the new branch, then resets the original.

positional arguments:
    new_branch_name             name of the new branch"

    if [[ "$1" == "-h" || "$1" == "--help" ]]; then
        echo "$usage"

    elif [[ -z "$1" ]]; then
        echo "[ ERROR ] You have to specify new branch name"
        echo
        echo "$usage"

    else
        base_branch=$(git symbolic-ref -q --short HEAD)  # current branch
        new_branch="$1"

        # create a new branch for the changes and check it out
        git checkout -b "$new_branch"

        # stash the changes until we revert "$base_branch"
        git stash

        # go back to "$base_branch"
        git checkout "$base_branch"

        # reset to the last commit
        git reset --hard HEAD

        # go back to "$new_branch"
        git checkout "$new_branch"

        # re-apply the stashed changes and you are good to go
        git stash apply

        # delete stash
        git stash drop
    fi
}

@chmaynard
Copy link

In addition to the above, my final step is to stage and commit all changes on the new branch.

@jhshah0111
Copy link

I don't know whether I am doing something wrong. But after "git stash apply" in the new branch, when I go back to my "base branch" which is master in my case, it brings all the changes back there. Any suggestion?

@mickro
Copy link

mickro commented May 26, 2021

# get into the master branch
git checkout master

# create a new branch for the changes and check it out
git checkout -b dev_branch

At this point you have a new branch with you changes uncommitted.

master is already at its HEAD state. And you new branch as well.

If you commit you changes at this step. Your changes are attached to your new branch. master stay unchanged.

What is the point doing the 4 last command?

# stash the changes until we revert master
git stash

# go back to master
git checkout master

# reset to the last commit
git reset --hard HEAD

# go back to dev_branch
git checkout dev_branch

# re-apply the stashed changes and you are good to go
git stash apply

... it does bring you back at step 2.

I don't know whether I am doing something wrong. But after "git stash apply" in the new branch, when I go back to my "base branch" which is master in my case, it brings all the changes back there. Any suggestion?

@jhshah0111 commit the change when you are in you new branch

@RitvikDayal
Copy link

Thankyou for saving my hours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment