Using Github To Store Scripts

2 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.

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. In vscode, bring up the terminal and type (assumes you have WSL installed):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    bash
    cd /mnt/c/scripts/google/github/powershell-test
    git init .
    touch 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
    
    # enter username for github.com
    # enter password from previous step: 1ef09e5aac03f99fc5135a105d104bac70652898
    # should be successful
    
    # now we set it to where it won't ask for credentials as often
    echo "https://myemail%40domain.com:1ef09e5aac03f99fc5135a105d104bac70652898@github.domain.com" > /mnt/c/scripts/google/github/.gitcreds
    git config --local credential.helper 'store --file=/mnt/c/scripts/google/github/.gitcreds'
    git push --set-upstream origin master
    
    # now anytime you do a `git push`, it will use the credential file and shouldn't ask for a password
    # make sure to lock this file down with NTFS permission for Windows or chmod on Linux
    
  4. 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
    4
    
    git config --local github.user yourGitUserName 
    git config --local user.email your_email 
    git config --local credential.helper wincred
    git clone git@github.com:yourGitUserName/YourRepoName.git
    
  5. 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
    • Uninstall Credential Manager: "C:\Program Files\Git\mingw64\libexec\git-core\git-credential-manager.exe uninstall"

-----------------------------------------------------------
Spotted a mistake in this article? Why not suggest an edit!
-----------------------------------------------------------

Comments