DEV Community

ruthmoog
ruthmoog

Posted on • Updated on

How to fix file structure when your local and remote repos don't match

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!

 💡 About the examples

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

The problem.

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.

MyWebsite > MyWebsite > [my website files]
The front page of the repo shows only the directory called My Website

My website files including the README.md are in an inner 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]

Ah-ha!

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/
£


Enter fullscreen mode Exit fullscreen mode

These untracked files don't belong!

What went wrong?

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.

How to fix it

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.

1. Back up your work.

Make sure everything you want to keep is saved to your remote repo. This way, you don't lose any content.

2. Remove the local git repo.

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
£


Enter fullscreen mode Exit fullscreen mode
  • ls is the list command that will list the folders and files in your current directory
  • -lha is a flag that will customise the list output as follows:
    • -l long list, provides additional information for each item
    • -h used with -l caps the number of digits by using unit suffixes in the size column, a good habit to use this for readability
    • -a shows all, including hidden files and directories with names that start with a dot; we need this to see .git.

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
£


Enter fullscreen mode Exit fullscreen mode
  • rm [file] the remove command that will remove given files
  • rf this flag can let us use rm to remove directories as well as files
    • -r this will attempt to remove the file's/directory's hierarchy, and implicitly uses -d which will let us remove directories as well as files
    • f this forces removal without checking permissions first

Run ls -lha again to confirm .git has gone.

3. Clone the remote repo to your local directory.

In your GitHub repo copy the link to clone your repo, then clone it into the directory for the project you want.
In GitHub, in the repo you want to clone, select the <> code pull-down button to copy the link you want to use



£ cd MyWebsite
£ git clone git@github.com:dev.to/mywebsite.git
...
£


Enter fullscreen mode Exit fullscreen mode
  • cd change directory
  • git clone this will clone the contents of the remote repo into your directory and also this will make it a git repo again

4. Fix the file structure.

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


Enter fullscreen mode Exit fullscreen mode
  • 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
  • ls list the contents to check it's empty (so we don't accidentally delete any content) ```bash

£ rm -rf MyWebsite
£ ls MyWebsite
index.html style.css README.md

- we've already seen `rm -rf` earlier, now we'll use it to _remove_ that empty directory
```bash


£ git commit -am "Fix directory structure"
[main ab12cd34] Fix directory structure
 1 file changed, 1 deletion(-)


Enter fullscreen mode Exit fullscreen mode
  • git commit saves your progress to your local version control
    • -a add files that are already being tracked to the commit
    • -m "[message]" include a message on your commit ```bash

£ 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 push`](https://github.com/git-guides/git-push) will 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.

### 5. Check for fall-out.

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.


Enter fullscreen mode Exit fullscreen mode

Top comments (0)