Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching a branch after aborting current changes in git

I cloned a git repo and then started playing around in its master branch. After a while, I want to ignore the changes I just made (without committing them), and switch to a different branch. However, it stops me from switching because there are uncommitted changes. How do I ignore them without stashing them either? This is what happens:

$ git checkout gh-pages error: Your local changes to the following files would be overwritten by checkout:         somefile.txt Please, commit your changes or stash them before you can switch branches. Aborting 
like image 231
highBandWidth Avatar asked Sep 18 '11 18:09

highBandWidth


People also ask

How do I move current changes to a new branch?

The git checkout -b <BranchName> command will create a new branch and switch to it. Moreover, this command will leave the current branch as it is and bring all uncommitted changes to the new branch.

Do you have to commit changes before switching branches?

You must commit or stash those changes first before switching branches. You can think of stash as a drawer to store uncommitted changes temporarily.

Can you switch branches after commit?

Before you commit your changes reside on disk (the "working copy") or after you git add the "staging area". Neither of these belong to a particular branch. When you switch branches, uncommitted changes will go with you. If changing a branch would overwrite your uncommitted changes, Git will not let you switch.


1 Answers

Option 1

git checkout -f gh-pages 

Option 2

git reset --hard     # beware: don't make that a habit git checkout gh-pages 
like image 104
sehe Avatar answered Sep 25 '22 05:09

sehe