Have you ever set up a project with Git but messed up the project directory? We've all been there! I've done this and since I didn't know how to get out of it, I got some advice and have documented what I did here. Hope it helps!
My examples are using Unix shell commands in the terminal, so not all of them will work on windows - you might need to search for the equivalent instead.
I have used a GitHub repo here; the clone process might be a little different if you're using another provider
My local git repo structure looked correct, I had a main directory with a subdirectory for my project, containing my project files. I could make changes here and push them to my remote, no problem.
MyProjects > MyWebsite > [my website files]
But, my remote repo on GitHub was not displaying the structure I wanted, with my website directory inside another directory.
The directory names on the remote repo are both "MyWebsite", which is confusing: the name of the repo locally and remote don't need to match. On GitHub, I was looking at a git repo set up from my main directory called MyProjects:
MyProjects (as "MyWebsite") > MyWebsite > [my website files]
There is a tell-tale sign when I use git in the terminal too:
£ git status On branch main Your branch is ahead of 'origin/main' by 1 commit. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: MyWebsite/README.md Untracked files: (use "git add <file>..." to include in what will be committed) FizzBuzz/ GameOfLife/ GuildedRose/ £
These untracked files don't belong!
Most likely, I have initiated the git repo within the main directory by mistake, and therefore pushed the nested project folder and its contents to the remote.
To fix this set up, we're going to remove the local repo, clone the remote in the correct directory, then move the files to the directory we want them in, and remove the extra directory.
Make sure everything you want to keep is saved to your remote repo. This way, you don't lose any content.
In your terminal, look for the
.git directory. This is where git stores everything it needs for version control
£ ls -lha total 72 drwxr-xr-x 11 ruthmoog staff 352B 4 Apr 10:59 . drwxr-xr-x 12 ruthmoog staff 384B 9 Apr 10:33 .. -rw-r--r--@ 1 ruthmoog staff 6.0K 4 Apr 10:59 .DS_Store drwxr-xr-x 12 ruthmoog staff 384B 10 Apr 14:09 .git -rw-r--r-- 1 ruthmoog staff 16B 28 Mar 2022 .gitignore -rw-r--r-- 1 ruthmoog staff 6.6K 19 Mar 2022 README.md -rw-r--r-- 1 ruthmoog staff 37B 19 Mar 2022 index.html -rw-r--r-- 1 ruthmoog staff 2.1K 19 Mar 2022 style.css £
lsis the list command that will list the folders and files in your current directory
-lhais a flag that will customise the list output as follows:
-llong list, provides additional information for each item
-lcaps the number of digits by using unit suffixes in the size column, a good habit to use this for readability
-ashows all, including hidden files and directories with names that start with a dot; we need this to see
We want to remove the
.git file from the parent directory
£ rm -rf .git £ ls -lha total 72 drwxr-xr-x 11 ruthmoog staff 352B 4 Apr 10:59 . drwxr-xr-x 12 ruthmoog staff 384B 9 Apr 10:33 .. -rw-r--r--@ 1 ruthmoog staff 6.0K 4 Apr 10:59 .DS_Store -rw-r--r-- 1 ruthmoog staff 16B 28 Mar 2022 .gitignore -rw-r--r-- 1 ruthmoog staff 6.6K 19 Mar 2022 README.md -rw-r--r-- 1 ruthmoog staff 37B 19 Mar 2022 index.html -rw-r--r-- 1 ruthmoog staff 2.1K 19 Mar 2022 style.css £
rm [file]the remove command that will remove given files
rfthis flag can let us use
rmto remove directories as well as files
-rthis will attempt to remove the file's/directory's hierarchy, and implicitly uses
-dwhich will let us remove directories as well as files
fthis forces removal without checking permissions first
ls -lha again to confirm
.git has gone.
£ cd MyWebsite £ git clone firstname.lastname@example.org:dev.to/mywebsite.git ... £
git clonethis will clone the contents of the remote repo into your directory and also this will make it a git repo again
Now that we have our local and remote repos matching, we should lose that extra directory. First we need to move our files, then remove the emptied directory.
£ mv MyWebsite/* . £ ls MyWebsite MyWebsite index.html style.css README.md
mv [source] [target]move files from the current location to the new location, this command can also be used to rename files re-writing the name at the target
lslist the contents to check it's empty (so we don't accidentally delete any content)
£ rm -rf MyWebsite £ ls MyWebsite index.html style.css README.md
- we've already seen
rm -rfearlier, now we'll use it to remove that empty directory
£ git commit -am "Fix directory structure" [main ab12cd34] Fix directory structure 1 file changed, 1 deletion(-)
git commitsaves your progress to your local version control
-aadd files that are already being tracked to the commit
-m "[message]"include a message on your commit
£ git push Enumerating objects: 15, done. Counting objects: 100% (15/15), done. Delta compression using up to 8 threads Compressing objects: 100% (10/10), done. Writing objects: 100% (10/10), 1.65 KiB | 1.65 MiB/s, done. Total 10 (delta 3), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (3/3), completed with 1 local object. To github.com:dev.to/mywebsite.git 12ab34cd..ab12cd34 main -> main £
git pushwill upload your local structural commits to the corresponding remote branch
Make sure to commit and push your change with a helpful message and, your local and remote repos will be correct.
If you're using any references to the file structure in your project, check these are still working. These shouldn't change with the steps above but if you made any other structural changes alongside these, now is a good time to fix any broken links.