"... and no one shall work for money, and no one shall work for fame; But each for the joy of the working, and each, in his separate star, shall draw the thing as he sees it, for the god of things as they are"

-Kipling

 

Creating a GitHub Repository from an Existing Project or Folder using GitHub Desktop

Summary

Here is the scenario: you have a GitHub account and you also have some work in a folder on your Windows PC you would like to place in an online repository. Furthermore, you do not have the git command line tools available (such as git bash) and, what is more, you don't particularly want to mess with all that sort of thing. You do have the GitHub Desktop GUI tool though.

Ultimately, all you really want to do is just place the local folder under some sort of git version control and then move it up to GitHub and automatically create a repository for it there.

Warning, you should always make a backup copy of your files before messing about with GitHub Desktop. It is not likely anything will go wrong in a way that affects your work - but why take chances.

Scenario 1: The Repository and the Local Folder Have the Same Name

This operation is simple if your repository on GitHub is to have the same name as the local folder on the PC. In that case you just create a new local repository in GitHub Desktop and then push it up to GitHub. The main thing to be aware of is that when GitHub Desktop creates or adds a new local repository it always wants the folder name to be the same as the name of the repository. So, if your directory structure looks like …

TopLevelDirectory
  | - File1 not for GitHub or version control
  | - File2 not for GitHub or version control
  | - ProjectName
        | - FileA for GitHub and version control
        | - FileB for GitHub and version control
        | - FileC for GitHub and version control
… all you need to do is choose the Create New Repository option in GitHub Desktop, choose the TopLevelDirectory as a target and give it the name ProjectName as the name of the local repository. Since the ProjectName folder already exists, GitHub Desktop will not create it and will create the appropriate git tracking files and subdirectories in the existing directory. You will end up with a structure like …
TopLevelDirectory
  | - File1 not for GitHub or version control
  | - File2 not for GitHub or version control
  | - ProjectName
        | - FileA for GitHub and version control
        | - FileB for GitHub and version control
        | - FileC for GitHub and version control
        | - .gitattributes
        | - README.md
        | - .git
              | - … more files
If you make a mistake and specify the ProjectName folder as the target when you create the new local repository, you will end up with a structure which looks like …
TopLevelDirectory
  | - File1 not for GitHub or version control
  | - File2 not for GitHub or version control
  | - ProjectName
        | - FileA for GitHub and version control
        | - FileB for GitHub and version control
        | - FileC for GitHub and version control
        | - ProjectName
              | - .gitattributes
              | - README.md
              | - .git
                    | - … more files
… and that is not what you want. In this case just delete the bottom ProjectName directory and its contents and also delete the local repository in GitHub Desktop (or see below for how to fix it). Once you have done that, try again.

If you are satisfied that you have created the local repository in the way you wish, make it the active repository and press the Publish this Repository to GitHub button located on the top right hand side of the GitHub Desktop to send the repository up to GitHub.

Scenario 2: The Repository and the Local Folder Do Not Have the Same Name

What about a situation in which the name of the local folder on the PC is not the same as the name of the Repository name you wish to use on GitHub? This often happens for me as I tend to set up project directory structures which look like ...

ProjectName
  | - File1 not for GitHub or version control
  | - File2 not for GitHub or version control
  | - FolderWithGitHubDetails
        | - FileA for GitHub and version control
        | - FileB for GitHub and version control
        | - FileC for GitHub and version control
What I want is all of the contents of the FolderWithGitHubDetails to be a local git repository named ProjectName on the PC and then be able to move it up to GitHub and create a repository there also named ProjectName. In other words, I wish to be able to create a git repository via GitHub Desktop GUI in which the name of the repository is not the same as the name of the local PC folder. This is not a simple one-step operation with GitHub Desktop - but it is quite do-able.

Here is how it is done.

  1. Use the GitHub Desktop to create a new local repository with the name ProjectName and select the FolderWithGitHubDetails as the target.
  2. Once you create the new local repository, you will see it also created a new subfolder called ProjectName below the FolderWithGitHubDetails folder. GitHub Desktop always does this - there does not seem to be a way to stop it.
  3. At this point you will end up with a structure that looks like this ...
    ProjectName
      | - File1 not for GitHub or version control
      | - File2 not for GitHub or version control
      | - FolderWithGitHubDetails
            | - FileA for GitHub and version control
            | - FileB for GitHub and version control
            | - FileC for GitHub and version control
            | - ProjectName
                  | - .gitattributes
                  | - README.md
                  | - .git
                         | - … more files
    
  4. Close GitHub Desktop
  5. The above structure is not what we want - however it is fixable. Simply move the .git files which form the content of the new ProjectName folder into the FolderWithGitHubDetails folder above and delete the newly created ProjectName folder at the bottom of the tree. If you do not see the .git sub-folder. Use the Show Hidden Files check box option under the Windows Explorers View menu to make sure it is visible. You must copy the contents of the .git subfolder as well. Once that is done, the directory structure should then look like this ...
    ProjectName
      | - File1 not for GitHub or version control
      | - File2 not for GitHub or version control
      | - FolderWithGitHubDetails
            | - .gitattributes
            | - README.md
            | - .git
                  | - … more files
            | - FileA for GitHub and version control
            | - FileB for GitHub and version control
            | - FileC for GitHub and version control
    
  6. Open GitHub Desktop again. It will complain that the newly created ProjectName repository cannot be found. This is to be expected, you just moved the files that track this into the directory above and deleted the target directory. Just tell GitHub Desktop to delete the local repository.
  7. Now add the FolderWithGitHubDetails folder as an existing local repository in GitHub Desktop. This is now possible since it now contains all of the necessary git tracking files and subdirectories.
  8. Once, added, you will note that the repository name is FolderWithGitHubDetails not ProjectName as we wish. This will change in a subsequent step.
  9. You may wish to commit your files at this point. Just use the on-screen tools in GitHub desktop.
  10. Press the Publish this Repository to GitHub button located on the top right hand side of the GitHub Desktop to send the repository up to GitHub.
  11. The publishing process will prompt you for a new repository name. Give it the name you wish - in this example it is ProjectName.
  12. The repository will be created on GitHub and it will be named ProjectName. The repository will also be named ProjectName in GitHub Desktop software. The local folder for this repository will be FolderWithGitHubDetails and any files you place in it will be under version control and can be commited and pushed as normal using the GitHub Desktop GUI.

License

The contents of this web page are provided "as is" without any warranty of any kind and without any claim to accuracy. Please be aware that the information provided may be out-of-date, incomplete, erroneous or simply unsuitable for your purposes. Any use you make of the information is entirely at your discretion and any consequences of that use are entirely your responsibility. All source code is provided under the terms of the MIT License.