414

I was working on a git branch and was ready to commit my changes, so I made a commit with a useful commit message. I then absentmindedly made minor changes to the code that are not worth keeping. I now want to change branches, but git gives me,

error: You have local changes to "X"; cannot switch branches.

Can I change branches without committing? If so, how can I set this up? If not, how do I get out of this problem? I want to ignore the minor changes without committing and just change branches.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62
  • 1
    I believe this only happens when they changes are staged for commit but not commited? git checkout works just fine for changing branches if you haven't staged the files yet using git add or the like. – Jeremy Wall Aug 21 '09 at 03:16
  • 1
    Hi Jeremy, What do you mean by 'staged'? Forcing the user to commit file before changes branches doesn't seems like a great workflow. For example, if I'm in the master repository and quickly want to check something in a branch. I have to commit the code to the master first, even it the code is half written! Are you saying that indeed, it should be possible to checkout a branch in this situation? – Daniel Farrell Aug 21 '09 at 09:25
  • @boyfarrell You can use 'Git stash' to temporarily save the changes without committing. – Howiecamp Oct 29 '16 at 23:01
  • Cross-linking the closely related [How do I force “git pull” to overwrite local files?](https://stackoverflow.com/questions/1125968/how-do-i-force-git-pull-to-overwrite-local-files) – user56reinstatemonica8 Jan 09 '18 at 17:51
  • 4
    when you switch to a branch without committing changes in the old branch, git tries to merge the changes to the files in the new branch. If merging is done without any conflict, swithing branches will be successful and you can see the changes in the new branch. But if a conflict occur, you will get `error: You have local changes to ''; cannot switch branches.` and branch will not change. you can do `git checkout -m ` to merge conflicts and checkout to the branch and resolve conflicts yourself, or `git checkout -f ` to ignore changes. – samad montazeri Jun 08 '19 at 09:10
  • `git stash save` `git checkout branch` // do something `git checkout oldbranch` `git stash pop` – Raskul Apr 27 '21 at 19:25

16 Answers16

518

You need a clean state to change branches. The branch checkout will only be allowed if it does not affect the 'dirty files' (as Charles Bailey remarks in the comments).

Otherwise, you should either:

  • stash your current change or
  • reset --hard HEAD (if you do not mind losing those minor changes) or
  • checkout -f (When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes. )

Or, more recently:

Proceed even if the index or the working tree differs from HEAD.
Both the index and working tree are restored to match the switching target.

This differs from git switch -m <branch-name>, which triggers a three-way merge between the current branch, your working tree contents, and the new branch: you won't lose your work in progress that way.

PoolloverNathan
  • 148
  • 2
  • 11
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 42
    "You need a clean state to change branches." is only true if the branch change affects the 'dirty files'. – CB Bailey Aug 20 '09 at 11:46
  • 11
    For the stash method, I typed "git stash save", "git checkout otherbranch", then finally "git stash pop". – Venkat D. Oct 13 '11 at 01:05
  • 1
    Currently i don't see this error message, and the changes i made on one branch shows up on the other when i do "git status". has something changed? – Senthil A Kumar Nov 28 '11 at 06:50
  • 2
    thanks. the checkout -f was what i needed. i did git reset --hard git clean -f git checkout mybranch -f – nologo Jul 08 '15 at 01:12
  • Especially usefull if you're stuck on a branch with tons of merge conflicts which makes you unable to checkout a different branch without the -f command. – Sander Sep 14 '15 at 08:40
  • Git stash is not branch specific though, if I git stash pop while on another branch, the changes from the previous branch will pop out – Aditya Mittal Jan 19 '16 at 19:06
  • 1
    Here is the one great thing Git got totally wrong by violating the basic definition of a branch. Unlike git branch means two totally different workspaces forked from a repository. – nehem Feb 14 '17 at 22:23
157

If you want to discard the changes,

git checkout -- <file>
git checkout branch

If you want to keep the changes,

git stash save
git checkout branch
git stash pop
Jamie Macey
  • 2,974
  • 1
  • 18
  • 8
  • 16
    Indeed what Romerun says (to be complete): `git stash save` (when in working branchY) then `git checkout branchX` do something `git add/commit -m` etc. `git checkout branchY` and `git stash pop` to get back the stash – Highmastdon Nov 09 '12 at 09:36
  • 2
    Maybe so. I have a situation though where I want to do what the answer says, if I’m understanding it right: stash changes, switch from Y to X, then pop changes and commit them on X. – Ben Klein Oct 06 '15 at 17:02
  • 1
    note that `git stash save` is now deprecated in favor of `git stash push` – Amr Saber Oct 21 '18 at 15:45
  • [This alias](https://stackoverflow.com/a/56505204/5353461) simplifies the case of keeping changes when changing branches. – Tom Hale Jun 08 '19 at 09:49
81

well, it should be

git stash save
git checkout branch
// do something
git checkout oldbranch
git stash pop
romerun
  • 2,161
  • 3
  • 18
  • 25
26

Follow,

$: git checkout -f

$: git checkout next_branch
simplyharsh
  • 35,488
  • 12
  • 65
  • 73
20

Note that if you've merged remote branches or have local commits and want to go back to the remote HEAD you must do:

git reset --hard origin/HEAD

HEAD alone will only refer to the local commit/merge -- several times I have forgotten that when resetting and end up with "your repository is X commits ahead.." when I fully intended to nuke ALL changes/commits and return to the remote branch.

j0k
  • 22,600
  • 28
  • 79
  • 90
ccliffe
  • 209
  • 2
  • 2
15

None of these answers helped me because I still had untracked files even after reset and stash. I had to do:

git reset --hard HEAD
git clean -d -f
vishnu viswanath
  • 3,794
  • 2
  • 36
  • 47
11

git checkout -f your_branch_name

git checkout -f your_branch_name

if you have troubles reverting changes:

git checkout .

if you want to remove untracked directories and files:

git clean -fd
Pedro Trujillo
  • 1,559
  • 18
  • 19
9

If you have made changes to files that Git also needs to change when switching branches, it won't let you. To discard working changes, use:

git reset --hard HEAD

Then, you will be able to switch branches.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
7

Follow these steps:

  1. Git stash save will save all your changes even if you switch between branches.
git stash save
  1. Git checkout any other branch, now since you saved your changes you can move around any branch. The above command will make sure that your changes are saved.
git checkout branch
  1. Now when you come back to the branch use this command to get all your changes back.
git stash pop
Harshit Pant
  • 1,032
  • 11
  • 14
6

Easy Answer:

is to force checkout a branch

git checkout -f <branch_name>

Force checking out a branch is telling git to drop all changes you've made in the current branch, and checkout out the desired one.

or in case you're checking out a commit

git checkout -f <commit-hash>


"thought that I could change branches without committing. If so, how can I set this up? If not, how do I get out of this problem?"

The answer to that is No, that's literally the philosophy of Git that you keep track of all changes, and that each node (i.e. commit) has to be up-to-date with the latest changes you've made, unless you've made a new commit of course.


You decided to keep changes?

Then stash them using

git stash

and then to unstash your changes in the desired branch, use

git stash apply

which will apply you changes but keep them in the stash queue too. If you don't want to keep them in the stash stack, then pop them using

git stash pop

That's the equivalent of apply and then drop

KareemJ
  • 744
  • 10
  • 15
5

switching to a new branch losing changes:

git checkout -b YOUR_NEW_BRANCH_NAME --force

switching to an existing branch losing changes:

git checkout YOUR_BRANCH --force
Jorge Avila
  • 149
  • 1
  • 9
3

If you want to keep the changes and change the branch in a single line command

git stash && git checkout <branch_name> && git stash pop
Vinit Solanki
  • 1,863
  • 2
  • 15
  • 29
3

For your mental calmness (to have much easier access to changes you have left uncomitted doing a branch switch)

Before switching:

git checkout <next_branch>

use

git stash save "brief description of changes"

instead of the default:

git stash    
// or
git stash save

This pays off if your git stash list is a longer list and must switch back to some previous idea started somewhere there.

Sławomir Lenart
  • 7,543
  • 4
  • 45
  • 61
2

Move uncommited changes to a new branch

I created a .gitconfig alias for this:

[alias]
spcosp = !"git stash push && git checkout \"$@\" && git stash pop --index #"

To change to new-branch-name, use:

git spcosp new-branch-name

And any non-commited file and index changes will be kept.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
2

To switch to other branch without committing the changes when git stash doesn't work. You can use the below command:

git checkout -f branch-name

rajkumar chilukuri
  • 235
  • 1
  • 3
  • 8
1

Close terminal, delete the folder where your project is, then clone again your project and voilá.

miguelacio
  • 152
  • 1
  • 15
  • 2
    git is not designed to push you to delete the project and clone again! if you want to get the latest version from origin, you just `reset --hard`! – Ahmed Nour Jamal El-Din Jan 29 '19 at 02:17
  • 1
    This solution will do the job, but it's like you want to drive to Boston from New York, but decide to go via Montreal. – hxin Oct 07 '21 at 20:08