DEV Community

Cover image for How to change the root folder of your Git repo
Hibo Abdilaahi
Hibo Abdilaahi

Posted on

How to change the root folder of your Git repo

Spoiler alert: this is not as daunting as it sounds πŸ™Œ

So today, I decided to change the root folder of my GitHub repository from the parent folder I had initially initialised the repository in, to a child folder in the same directory.

I almost didn't bother as I had flashbacks of past nightmare experiences changing things on git but I decided to give it a go and luckily it was much easier than I thought wooo!

I thought I would record this for my future benefit and yours.

The process

Essentially what you need to do is move the .git folder to the folder you want to be the root folder.

These are the steps I took in the command line (remember I am moving my .git one folder inwards - you will need to adjust the commands to your needs).

In the below example, $ represents a command line prompt which may look slightly different to yours.

Now without further ado, here are the steps:

  1. Start by moving your .git file to the folder that you want to go to.

    • $ mv .git
  2. Then navigate to that folder.

    • $ cd
  3. Then add all the changes to the staging area. Git will detect these files as renamed versions of old files that were 'lost' and so no history will be lost.

    • $ git add .
  4. Commit all the changes with the -a command. The -a command stands for all. It tells the commit command to automatically stage files that have been modified and deleted whilst new files that you have not told Git about are not affected.

    • $ git commit -a
  5. Finally, push the changes to your repo. You might see a prompt that asks for a merge message and in this case you will need to follow the additional steps below.

    • $ git push

Additional steps to add a merge message:

You may receive the following git merge error message:

Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch

To solve this:

  1. Press β€œi”
  2. Write your merge message
  3. Press the β€œesc” key.
  4. Type β€œ:wq”
  5. Then press enter

You will then to git push again and you're done!

Top comments (9)

waylonwalker profile image
Waylon Walker • Edited

Very interesting workflow that I hadn't thought of.

If you are trying to rename the directory from the start you can do it with the clone command.

git clone <repo-url> <directory-name>
hiboabd profile image
Hibo Abdilaahi

Useful tip thanks!

art4 profile image

The title and the first sentence is missleading. This has nothing to do with GitHub but just git. I was thinking that one can change the root folder inside GitHub what is not true.

You can correct that by simply replace every "GitHub" with "git". Thank you.

hiboabd profile image
Hibo Abdilaahi • Edited

Good point! This does change the root folder of your Github repo as my current title states but I understand where you are coming from as it may seem like I meant you make the change in GitHub itself. I will amend the title to make it clearer :)

volpito profile image
RIGOT Maxime

Solution is easy enough but I didn't think of it and messed up my portfolio repo x)
I owe you une

dmikester1 profile image
Mike Dodge

I assume the .gitignore file would need to be moved to the same folder, right?

minhcrypto_ profile image
Minh Le

Just the thing I needed! πŸ™πŸ½

gabrielaramburu profile image
Gabriel Aramburu

very useful.

hetal393 profile image

thanks a lot...
this worked for me !!