Initializing a Git Repository after Project Creation

Godot Version

4.4.1

Question

I’ve started working on a game, and I forgot to initialize Git. I’ve already made huge progress and don’t want to lose my code, is there anyway I could start a git repo in the current working folder? I already have .gitignore and a README file set up, so I’m wondering if I can init a git repo without having to start a new project and copy over all my code and scenes, as that would be really tedious.

(Edit: I’m using GitHub Desktop, I can use CLI but if there’s a more GUI way then that would be appreciated)

Thanks

1 Like

You should be able to just git init in the root folder of your project. Not sure how you’d do that in the GitHub Desktop, as I haven’t used in a while, but there should be a way to initialize a new repo in the existing location.
If I remember right it’s pretty confusing as you need to select a parent folder of your project, not the root location. It’s definitely easier to use Git CLI to do that. I switched and never looked back.

3 Likes

Hey @wchc, thanks for clarifying that! I was also wondering the same thing since I forgot to set up Git at the start of my project, too. Good to know it’s safe to just run git init in the existing project folder. I’ll give it a try using CLI, even if GitHub Desktop is a bit clunky for that step, it sounds like this is the more straightforward way. Appreciate the tip!

1 Like

I did run git init but for some reason when I pushed it to GitHub, it took the files with it which I specified in .gitignore. Any particular reason for why it might be doing this?

Here’s the repo if it might help
Chess Repo

How did you add the files? git init just creates the repo by default, it doesn’t add anything to it. Are you sure the .gitignore was in place when the files got added? Did you use any “force” arguments?

1 Like

Looking at your commits, the .import/ was for ignoring a .import folder not the .import files.
So I don’t believe anything excluded was committed.

If there is a separator at the end of the pattern then the pattern will only match directories, otherwise the pattern can match both files and directories.
Git - gitignore Documentation

Also just an FYI, I’m pretty sure the .import and .uid files are intended to be committed to version control.

1 Like

I don’t know if this will help in your situation, as I tend to use the terminal (or Command Line Interface). What I do is navigate within your terminal/CLI to the project’s root directory. Then execute the command:

git remote add origin

in which is the github URL of your project’s intended GitHub repository. Then, follow the init-commit-push cycle of GitHub repository contributions, as follows:

git init

To initiate your commit to your github project directort, followed by:

git add .

or:

git add <list_of_files>

Then:

git commit -m “Initialized github repository for Godot Engine project root directory.”

Then, finally:

git push origin master

Which should push your project’s work to a ‘master’ branch. Notice I said ‘master’ branch as opposed to ‘main’ branch, as this initialization of the github project repository’s source code in its project root directory defaults to the ‘master’ branch, as this name is still left over in this feature from GitHub’s change from the mainline from branch name ‘master’ to branch name ‘main’ in 2020. Then, open your github repository’s Graphical User Interface (GUI), refresh the page (if need be), click on your list of available branches, and you should see the branch named ‘main’ in addition to a second branch named ‘master’. If you do not see a message box asking you if you’d like to merge the recent commits in branch named ‘master’ to mainline in branch named ‘main’, navigate to the master branch in your GitHub repository GUI by clicking on the branch named ‘master’, then you should see a message box somewhere just above the project’s file structure in the GUI notifying you that a recent commit was made to the master branch, asking if you’d like to merge this branch to another branch. Click to merge commits, then you should be given the option to merge the master branch to your main branch by default. Type whatever merge message you’d like, check for potential merge conflicts (which there shouldn’t be if your github repository is initialized as an empty new repository with its README.md and .gitignore files by default), then merge the master branch to the main branch. Once this is done, delete the master branch from your project’s branch structure through the GUI on the same screen you should be on, then navigate back to your project’s main branch with respect to its source code by clicking on the “code” tab at the top of your GitHub repository page. If branch ‘main’ is not selected, go ahead and select it from the list of branch names to navigate to said main branch. Best of luck to you!