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:
-
Start by moving your .git file to the folder that you want to go to.
- $ mv .git
-
Then navigate to that folder.
- $ cd
-
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 .
-
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
-
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:
- Press βiβ
- Write your merge message
- Press the βescβ key.
- Type β:wqβ
- Then press enter
You will then to git push again and you're done!
Top comments (9)
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.
Useful tip thanks!
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 :)
THANK YOU !
Solution is easy enough but I didn't think of it and messed up my portfolio repo x)
I owe you une
I assume the .gitignore file would need to be moved to the same folder, right?
Just the thing I needed! ππ½
very useful.
thanks
thanks a lot...
this worked for me !!