GIT Switch branch without commiting or discarding current changes

Asked

Viewed 16,437 times

6

I need to submit my current changes to another branch.

When giving git branch-name checkout it asks me to commit or change the current changes, I don’t want any of those options.

How can I proceed?

1 answer

9


If you want to save the changes made, you can make a git stash and you’ll see something like:

$ git stash
Saved working directory and index state \
  "WIP on master: 049d078 added the index file"
HEAD is now at 049d078 added the index file
(To restore them type "git stash apply")

, this way will be made added in stack and you can make the git checkout hassle-free.

$ git status
# On branch master
nothing to commit, working directory clean

If you want to continue the changes saved on stash, typhoon git stash list and will be displayed something like:

$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051... Revert "added file_size"
stash@{2}: WIP on master: 21d80a5... added number to log

So just select one with git stash apply to select the latest, or for example to select a specific stash in the list:

git stash apply stash@{2}

$ git stash apply
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#
#      modified:   index.html
#      modified:   lib/simplegit.rb
#
  • 1

    Thanks, it worked out (:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.