35

I am facing issues with the git checkout command. Please help.

Here is what I did.

  1. Created a local directory. Created some files. Initiated a git repo locally.
  2. Created a repo in bitbucket & pushed my local repo to bitbucket
  3. Created a new branch locally, b01
  4. Added new files to b01, committed them and successfully pushed the branch to bitbucket
  5. Now I am trying to merge b01 with master and that is where I am facing issues. I am not able to checkout master.

Lists of commands used and the error details:

git clone [my repo]
git checkout -b b01

Hereafter I made multiple changes, including creating new files and directories, committed those changes and successfully pushed the changes on that branch to the repo. like:

git push -u origin b01

Now I am done with the changes and want to merge it to the master branch. So I do a:

git checkout master

Here is the error it throws:

fatal: cannot create directory at 'workfolder': Permission denied

Git keeps me in b01, but deletes ALL the files I had created in the branch. So if I now do a

git status

it shows me:

On branch b01
Your branch is upto date with 'orgin/b01'
Changes not staged for commit:
(use "git add/rm .....)
  deleted: new file1
  deleted: new file2
<list of the new files I had added but which got deleted as a result of the command>

I am having to run a

git checkout -- . 

to restore the files.

Please help.

pppery
  • 3,731
  • 22
  • 33
  • 46
TechiRik
  • 1,893
  • 6
  • 27
  • 37
  • Did you run the Git Bash with admin rights? This sounds like it doesn't have the rights to create folders. The other things might be a side effect of this. – Tim Biegeleisen Sep 23 '16 at 00:17
  • I am using a windows machine. I am the admin on the machine. @TimBiegeleisen – TechiRik Sep 23 '16 at 00:41
  • UPDATE: I don't know what had gone wrong, but I opened up a second git bash window and it worked through the second window. I had not changed anything. I am still keeping this question open to see if someone can make sense of what did I do to mess things up. – TechiRik Sep 23 '16 at 19:28

7 Answers7

72

I had a similar situation. Though it was due to me having the solution open in visual studio. Closing visual studio and doing a git reset put me into a good spot.

I.e. check to see if any processes are locking the folder/files.

gzimmers
  • 959
  • 7
  • 11
  • 1
    I had both visual studio and vs code open. One of them must have been maintain a file lock. Once they were both closed `git checkout foo-branch` worked for me. – Kevin Kuszyk Jan 24 '18 at 14:39
  • 1
    I had studio and VScode open too, I only needed to close VScode and could perform the operation through studio's TFS interface after that. – 333Matt Jul 25 '18 at 18:41
  • Closing VScode and doing `git reset --hard` fixed it for me too. But be careful, people! – Kenmore Dec 07 '18 at 23:37
  • In VSCode, I had two terminals open. One for running a gulp task that communicated with a watch, and another terminal for "trying" to switch branches. I exited out of both terminals, re-opened a new one, and I was then able to switch. Thanks for pointg this out! – klewis Feb 06 '19 at 16:13
  • I had visual studio code opened on a DIFFERENT project's workspace and it still blocked my merge. Worked fine after i closed ALL my visual code studio instances. – sed lex Nov 22 '19 at 10:01
  • VSCode was the culprit too in my case – Mohammad Faisal Dec 21 '21 at 18:21
8

As it is already said, these could happen when another process is already accessing the files, so closing any app that could potentially be using it will help, as of me Visual Code was opened and closing it did help.

robivictor
  • 399
  • 3
  • 5
3

Just change permission to allow one or group user for this 'workedfolder' by this

sudo chmod u+w <exact_workedfolder_permission_denied>

or

sudo chown -R user_name:user_name <exact_workedfolder_permission_denied>

2

I know the question is a little bit old, but as top answer suggests using --hard flag, which may not be necessary, I decided to post this.

Probable problem cause: some other process is using/locking file/folder you're trying to remove

Solution: stop this process, then try again. One way to found it is via Resource Monitor (way I've learned from here):

  1. Open Task Manager
  2. Navigate to Performance tab
  3. Click Open Resource Monitor at the bottom
  4. Navigate to CPU tab
  5. At Associated Handles section, there's a search field - enter path to dir/file for which you're getting 'Permission denied' error
  6. (Review and) stop any process that appeares as search result.
tyrrr
  • 528
  • 2
  • 11
  • oddly in my case VS code was the process hanging things up. I closed vs code and was able to checkout a different branch from powershell – RayLoveless Jun 19 '20 at 09:05
  • 4
    As somebody downvoted this answer, I just wanted to suggest that it is a good thing to also write what's wrong here, at the comment section - that way it is much more helpful for the community... – tyrrr Jul 29 '20 at 22:13
0

First, between every operation with git I would recommend doing a quick git status. I cannot tell you how many times this has saved me lots of headaches.

A couple of things I might try to shed some light on the problem(s):

  1. You said you created directories and made lots of changes to b01, then pushed it to the origin. Look at the commit for this action `git show . Did you change permissions on the parent directory of "workfolder"? This might be of help.

  2. Are you on windows? If you are, how deep in directory paths are you? Is the path length exceeding windows max? Reference here.

  3. If none of these work, have you tried creating a throwaway branch off of b01, and merging "master"? This sometimes smokes out a cause.

  4. The ugly hail-Mary. Have you tried making a copy/paste of "b01" outside the repository, rolling back to the matching last commit of "master", and then pasting the changes in the "b01" copy over master? When things are totally upside down and not making sense, sometimes it is easier to work backwards through the problem to figure out what went wrong and learn what not to do in the future.

Community
  • 1
  • 1
Benjamin Dean
  • 1,218
  • 11
  • 11
  • 1. I am on windows, not changed perms. (2) Path length currently is 97 chars with spaces. So should not be a problem (3) How do I do this? Do you mean create branch b01a off branch b01 and then merge b01a to master instead of b01 to master? (4) Have not tried this. I believe this is a manual process, will only try if none above works. Any idea what did I do wrong to mess things up this way? – TechiRik Sep 23 '16 at 19:19
0

In my case I just checked out to the offending commit using the commit hash rather than the branch name.

Devorein
  • 1,112
  • 2
  • 15
  • 23
0

304/5000 The answer to that can be quite simple. Say you want to mash your branch with a master, you can do the following:

git checkout -b master-2 // from your current branch where all the changes are
git branch -D master
git branch -m master-2 master
git push origin master

Ready! Remember that this is in case you want to overwrite everything in the branch