loading...
Cover image for How to change the root folder of your Git repo

How to change the root folder of your Git repo

hiboabd profile image Hibo Abdilaahi ・2 min read

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!

Posted on by:

hiboabd profile

Hibo Abdilaahi

@hiboabd

An aspiring software developer. Graduate of the Makers Academy coding bootcamp. Using dev.to to share my thoughts and meet other developers πŸ‘©πŸΎβ€πŸ’»βœ¨

Discussion

markdown guide
 

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>
 
 

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.

 

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 :)