Share
Explore

Lab - GIT HUB to Git Local Repo Practice Drill

1. Create some simple code in local repo: C:\cicdpipeline-2>
2. Use our new git gh command line window to "pull" from GitHub.com to local
3. Modify code locally
4. Push from local back up to

Lab Exercise: GitHub CLI Workflow with TypeScript Calculator
## Initial Setup
**Prerequisites** - Visual Studio Code installed - GitHub CLI (gh) installed - Node.js and npm installed
Part 1: Creating Local Project
1. Open terminal and create project directory: ```bash mkdir C:\cicdpipeline-2 cd C:\cicdpipeline-2 npm init -y npm install typescript @types/node --save-dev ```
2. Create TypeScript configuration: ```bash npx tsc --init ```
3. Create calculator program in VS Code: ```typescript //calculator.ts export function add(a: number, b: number): number { return a + b; }
// Test the function console.log(add(5, 3)); // Should output 8 ```
4. Initialize Git repository: Do this your newly installed GIT CLI (gh) Terminal Window git init
gh repo create cicdpipeline-3 --public --source=. --remote=upstream ​
image.png

git add .


git commit -m "Initial commit: Add calculator function"


megaphone

This sequence is important because:

git init creates the local Git repository structure
gh repo create then creates the remote repository and establishes the connection
Only after both are set up should you stage and commit files
The --source=. flag in the gh repo create command specifically expects to work with an existing Git repository, which is why git init must come first.
After creating the local repository and before cloning, you need to push the initial content to GitHub.
Here's the correct sequence:
Before you do git push to the main branch, you must create the Main branch, as follows:

info

The error src refspec main does not match any indicates that the main branch does not exist locally. To resolve this, you need to create the main branch and push it to the remote repository. Here are the steps to fix the issue:

Steps to Create and Push the main Branch

Check Current Branch To see if any branch exists, run:
git branch

If you see master, this means your default branch is still master.
Rename master to main (If Applicable) If the branch is named master and you want to rename it to main, use:
git branch -m master main

Create the main Branch (If None Exists) If no branch exists or you want to explicitly create a new branch, run:
git checkout -b main

Add and Commit Files If you haven’t added or committed any changes yet, do the following:
git add .
git commit -m "Initial commit"

Push the main Branch to the Remote After creating or renaming the branch, push it to the remote repository:
git push -u origin main

The -u flag sets the upstream branch, so future pushes and pulls can be done without specifying origin main.
Verify the Push Check your remote repository on GitHub to confirm that the main branch and your files have been uploaded.

If You Are Using a Fresh Repository

If this is a new repository without any branches created yet, follow these steps:
Initialize Git and create the branch:
git init
git checkout -b main

Add files and make the first commit:
git add .
git commit -m "Initial commit"

Add the remote and push:
git remote add origin https://github.com/ProfessorBrownBear/Demo-repository-for-GitHub-Actions-and-Jest-testing.git
git push -u origin main

Confirmation

After completing these steps, you should have a main branch both locally and on GitHub. Verify by:
Running:
git branch

Locally, you should see main.
Visiting the repository on GitHub and confirming the main branch is listed.

PS C:\cicdpipeline-3> git branch -m master main PS C:\cicdpipeline-3> git push -u origin main

fatal: 'origin' does not appear to be a git repository

fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists. PS C:\cicdpipeline-3> ​
image.png
error

The error fatal: 'origin' does not appear to be a git repository indicates that the origin remote has not been set for your local repository.

Here’s how to resolve this issue:

Steps to Add the Remote Repository

Verify the Remote Check if a remote repository is already linked:
git remote -v ( we passed this test - remote repo is there : see above screen shot )
If no output appears, this means no remote repository is configured yet.
Add the Remote Add the GitHub repository as the remote. Replace <your-username> and <repository-name> with your actual GitHub username and repository name:
In the command below:
git remote add origin https://github.com/<your-username>/<repository-name>.git

For example:
git remote add origin https://github.com/ProfessorBrownBear/.git

Verify the Remote Again After adding the remote, verify that it has been set up correctly:
git remote -v

You should see something like this:
image.png

Push to the Remote Now push the main branch to the remote repository:

git push -u origin main


Additional Notes

Ensure Repository Exists on GitHub: Verify that the repository has been created on GitHub with the same name.
Check Access Permissions: Ensure you have the correct permissions for the repository.
Authentication:
If you’re using HTTPS and haven’t configured a Personal Access Token (PAT), Git may prompt you for credentials.
If you’ve already set up an SSH key, you can use the SSH URL instead:
bash
Copy code
git remote set-url origin git@github.com:<your-username>/<repository-name>.git

After performing these steps, you should be able to push your code to the main branch successfully.
bash
# After git init and gh repo create, push initial content
git push -u origin main

# Then proceed with other operations
cd C:\
gh repo clone <your-username>/cicdpipeline-2

The initial push establishes the connection between your local main branch and the remote repository, ensuring your code is available on GitHub before any subsequent clone or sync operations.
image.png

Part 2: Pulling from GitHub

1. Clone repository if not already done:

```bash cd C:\ ​gh repo clone <your-username>/cicdpipeline-2

2. Pull latest changes: ```bash cd cicdpipeline-2 gh repo sync ```
Part 3: Modifying Code Locally (resume)
1. Update calculator.ts in VS Code: ```typescript // calculator.ts export function add(a: number, b: number): number { return a + b; }
export function subtract(a: number, b: number): number { return a - b; }
// Test the functions console.log(add(5, 3)); // Should output 8 console.log(subtract(5, 3)); // Should output 2 ```
2. Save changes and compile: ```bash npx tsc calculator.ts ```
## Part 4: Pushing Changes to GitHub
1. Stage and commit changes: ```bash git add . git commit -m "Added subtract function" ```
2. Push using GitHub CLI: ```bash gh repo sync ```
3. Create pull request (optional): ```bash gh pr create --title "Add subtract function" --body "Added new subtraction functionality" ```
## Verification
Test your setup: ```bash node calculator.js ```
You should see the output: ``` 8 2 ```
Citations: [1] https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/6136/dbe17510-6438-4756-a0b8-5191bac6d2a7/CSD-4503-Assignment-3.pdf [2] https://pplx-res.cloudinary.com/image/upload/v1732722331/user_uploads/vhdboukln/github-cmd-line.jpg

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.