-1

Whenever I am trying to run:

git checkout main

I am getting an error:

error: The following untracked working tree files would be overwritten by checkout:
    README.md
Please move or remove them before you switch branches.
Aborting

I have tried doing the following steps:

  1.  git rm --cached README.md
     git checkout main 
    

    Didn't work.

  2.  git add -all
     git commit -m "initial commit"
     git checkout main 
    

    Still the same output.

Biffen
  • 6,249
  • 6
  • 28
  • 36
Hemang Joshi
  • 158
  • 1
  • 5
  • 13

2 Answers2

2

If you don't want to keep README.md change:

git add README.md
git reset HEAD --hard
git checkout main

If you want to keep README.md change:

git add README.md
git commit -m "Your commit message"
git checkout main

or you want edit it at main branch later:

git add README.md
git stash
git checkout main
git stash pop
hms5232
  • 323
  • 1
  • 6
  • Will `git reset HEAD --hard` remove an untracked `README.md` file? – Joe Sep 16 '21 at 05:58
  • @Joe Thanks! You should add it first, then reset will work. I will edit answer. – hms5232 Sep 16 '21 at 06:00
  • BTW, if your git version is 2.23 or later, i recommend new command: `git switch ` – hms5232 Sep 16 '21 at 06:03
  • `add`/`reset --hard` is a fairly confusing way to achieve `rm`. – Joe Sep 16 '21 at 06:11
  • I think there is a README on main, and here is one, too. But the file modified, cause can't switch branch. If we use `git rm` or with `--cached` flag, git will delete file or make the file untracked. This result may not be what we want so I decide the `reset` to make file unchanged. – hms5232 Sep 16 '21 at 06:33
  • If `git` says "untracked working tree files", then no, there isn't a file on this branch. Use `rm README.md` to clear it out. – Joe Sep 16 '21 at 07:37
  • 1
    Oh, i understand what you mean. Because the file is untracked, we can directly `rm` it if we don't want to keep it, right? – hms5232 Sep 16 '21 at 08:07
0

Try: git checkout -f master

Also check branch name. The default branch name in Git is master.

ZygD
  • 22,092
  • 39
  • 79
  • 102