Connect To Github Private Repo

3 minute read

Description:

So I’ve been using Google Drive to store my scripts in a “public repo” that I push my sanitized scripts to every so often, but I’ve always wanted to do it the “right way” by using version control (git) and sending to Github. Here are the steps I did to do that from my W10 machine:

To Resolve:

  1. First, you have to find out if you want to use HTTPS or SSH for doing ‘pushes’ to Github/Gitlab/ect. I went with HTTPS for now. I may change it later - see this

  2. Sign in to Github and create a repo, for example https://github.domain.com/gerry/powershell-test. The repo name is powershell-test and is initialized with a README.md on Github.

    • In the top right, click on Settings => Developer Settings => Personal Access Tokens => Click the ‘Generate new token’ button => Check ‘repo’ => Create => Jot this down somewhere, should look something like 1ef09e5aac03f99fc5135a105d104bac70652898
  3. If you haven’t already, install GCM. This will allow Windows to store your git credentials securely.

  4. Clean up your git repos on your computer. See my other post on this for more details. But the idea is that you:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    # first, delete your global git config settings
    Remove-Item ~/.gitconfig
    
    # Set them to what you want. 
    # NOTE: It is important that you run this in powershell on Windows if you have WSL installed. If you do this in bash, vscode will ignore your git config.
    git config --global core.autocrlf true
    git config --global credential.helper manager
    
    # now just go to each git dir and check its settings and add information as needed
    cd c:\scripts\repo\home\gerryw1389.github.io
    git config -l
    # see that my user.name and user.email is not listed. We will need these for commits so we add them like:
    git config --local user.name 'Gerry Williams'
    git config --local user.email 'gerry.williams@domain.com'
    git config -l
    # looks good now
    
  5. Now, to add the Github repo to my computer and authenticate with it, I run the following in vscode:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    New-Item -itemtype Directory -Path "c:\scripts\repo\home\powershell-test"
    cd c:\scripts\repo\home\powershell-test
    git init .
    New-Item -itemtype File -Path "c:\scripts\repo\home\powershell-test" -Name "myfile.txt"
    git add --all
    git commit -m 'first'
    git remote add origin https://github.domain.com/gerry/powershell-test
    git pull origin master --allow-unrelated-histories
    
    # it then brings up your default text editor to add a comment, I usually just write 'because I said so' or something :)
    # When you do the pull, GCM will pop up over VSCode and ask you to enter credentials into it instead of vscode
    # enter username for github.com and the password as the access token
    git push --set-upstream origin master
    
    # now anytime you do a `git push`, it will use the credential manager and shouldn't ask for a password
    
  6. Using SSH instead would work like (NOTE: I haven’t ever tested this since HTTPS seems more common):

    • Generate a SSH key pair and copy the public portion to your clipboard
      • ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
      • ssh-add ~/.ssh/id_rsa
      • clip < ~/.ssh/id_rsa.pub
    • Add the ssh key to your github account.
    • Clone the repo by the following:
    1
    2
    3
    
    git config --local user.name 'Gerry Williams'
    git config --local user.email 'gerry.williams@domain.com'
    git clone git@github.domain.com/gerry/powershell-test.git
    
  7. Troubleshooting:

    • Run git status often and see if it shows any errors. If so, correct them by googling the answers.
    • Run git config -l and look for the correct origin.
    • If it is wrong, type git remote rm origin and git remote add origin https://path/to/repository then confirm git remote get-url --all origin

-----------------------------------------------------------
Spotted a mistake in this article? Why not suggest an edit!
Did you like the article? Donations are always welcome!
-----------------------------------------------------------

Comments