Like many developers, I have a bad habit of getting sucked into the code and forgetting to commit my changes to Github. One day a few months back I was happily coding along, adding new features to my React application. Suddenly remembering that commits are important, I went ahead and clicked the source control tab, prepared to write the mother of all commit messages, and gritted my teeth for the inevitable merge conflicts.
But it was much worse than that. For some reason, the source control tab listed every changed file in my development directory. The things I wanted to commit were there, but so were dozens of untracked algorithms and practice components from several different apps. This was a major problem, as I had no desire to store all these unrelated files in my new project repository.
I started poking around the buttons on the source control tab, looking to see if I could remove all the unrelated files. It turns out I could remove them from the changes list by clicking the "discard changes" button, but doing so would totally delete them from my computer as well. Finding this solution less than optimal, I put on my thinking cap.
Why was this happening? Why did everything show up on my changes list? Something must have been wrong with my file structure. I had done the usual when starting the project, creating a repository on Github and connecting it in the root directory of my project with the git add remote origin
command. Eventually, after much head scratching, I decided that there must be a git repository somewhere higher in the file structure that all my projects were connected to. I'd just have to find it.
Fortunately, i was able to use the git rev-parse --show-toplevel
command. This command shows the top level directory of the working tree. In other words, it works backwards through the tree and returns the location of the first encountered git repository. Upon running this command, I received a result of /Users/[my name]/Development/code
. If you couldn't guess from the name, this is where my coding projects live. And there was a git repository there, inconveniently connecting them all together. I had found the source of my problem.
Now for a solution or two. The most obvious solution would be to remove the .git file from my code
directory. I opened a terminal window to this directory and entered the git log
command to see if I could delete it without losing any valuable history. Unfortunately, there was quite a bit of history present, and I didn't want to deal with it at the moment. I just wanted to commit my project changes. If you are in this situation and the git log
command returns nothing or nothing meaningful, go ahead and delete the git file from the directory, and you should be good to go. If git log
returns history and you don't want to deal with it, read on.
Fortunately for me, there is another solution. Git starts at the bottom of the tree and works its way up, meaning it will stop at the lowest repository it finds. I cd'd back into the root directory of my project and entered the git init
command to initialize a new repository. I followed this up with the git remote -v
command to see where my project was now connected, and received no response (meaning no remote existed). This was expected, as I had initialized an empty repository and never connected to it. I then ran the git remote add origin [my repository]
command to connect my project to the repository. The process was successful; when I returned to the source control tab in VS Code I saw only files that were part of my project. I breathed a sigh of relief and got back to work.
Here's the TL;DR for those who find themselves in this situation:
- Run the
git rev-parse --show-toplevel
command from the root directory of your project. This will tell you where to go. - Navigate to the directory found in Step 1, then enter the
git log
command to see if there is any history you need to keep. If there isn't, delete the .git folder in this directory and your problem should be solved. - If there is history you need to keep, navigate back to the root directory of your project and enter the
git init
command. Then run thegit remote -v
command to make sure you aren't connected to any remote. Once you know you aren't connected, use thegit remote add origin [your github repository url]
command. You should be good to go.
Top comments (6)
Thanks so much!! I intentionally created an account here just to say a thank you. I had this problem after creating react app, it was fresh so I was concerned that all my files ended up on the change list. Thanks!! ;)
Hi Adam, I think the same happened to me, both my devices had all the changes listed in my computer after getting the react app. Do you know how to work around this?
Like @adam , I joined to thank you for posting this solution. It was the only one I found and it worked for me.
(I still don't know what caused the problem: Only my Idyll projects were affected, each listing > 5k files to be staged -- no such issue with any other projects in VS Code.)
Thanks again.
--Nick/
Thanks for this post, it literally saved my life. Noob coder (don't even know how to properly use git) freaked out thinking I broke my computer.
omg! Thank you! I am new to all of this and thought I broke my computer with all the files it was trying to commit.
Thank you very much!