cuHacking logocuHacking DevDocs
Knowledge BaseHack the Tunnels

3. Share cool automated things with others

Convince someone's thing to depend on your thing, and then push a change that breaks their thing.

Creating repositories

You're at Hack the Tunnels. Your friend wants to impress their crush somehow.

We need a way to share the cool thing we made with them. Let's turn that into a repository.

Navigate back to your VS Code Terminal and do the following.

Terminal
git init .

Now let's make a commit.

Terminal
git commit

You should see.

On branch main
 
Initial commit
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file.sh
 
nothing added to commit but untracked files present (use "git add" to track)

Adding files

Let's tell git to track changes to the file we want to commit.

Terminal
git add file.sh
Terminal
git commit -m "initial commit"

You should see:

[main (root-commit) d8f6d74] first commit
 1 file changed, 11 insertions(+)
 create mode 100755 file.sh

Pushing to remote

Great, now we need to actually send the link to this somehow. We can use GitHub for this.

It will host a remote version of the repository that our friend can download.

Terminal
gh repo create

We don't want to create a new repository, but rather push an existing one.

? What would you like to do?  [Use arrows to move, type to filter]
  Create a new repository on GitHub from scratch
  Create a new repository on GitHub from a template repository
> Push an existing local repository to GitHub

Leave the path as is, and hit enter.

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository (.)

Leave the Repository name as is, and hit enter.

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name (hack-the-tunnels-git-workshop)

Select your personal account as the owner, and hit enter.

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name hack-the-tunnels-git-workshop
? Repository owner  [Use arrows to move, type to filter]
  CarletonComputerScienceSociety
> MFarabi619
  cuhacking
  cusec
  ieee-spac

You can provide it any description you like. Since we're in a hurry, we'll leave it blank.

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name hack-the-tunnels-git-workshop
? Repository owner MFarabi619
? Description

Leave it as public.

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name hack-the-tunnels-git-workshop
? Repository owner MFarabi619
? Description
? Visibility  [Use arrows to move, type to filter]
> Public
  Private

Hit enter to add the remote.

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name hack-the-tunnels-git-workshop
? Repository owner MFarabi619
? Description
? Visibility Public
✓ Created repository MFarabi619/hack-the-tunnels-git-workshop on GitHub
  https://github.com/MFarabi619/hack-the-tunnels-git-workshop
? Add a remote? (Y/n)

Hit enter, leave it as origin.

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name hack-the-tunnels-git-workshop
? Repository owner MFarabi619
? Description
? Visibility Public
✓ Created repository MFarabi619/hack-the-tunnels-git-workshop on GitHub
  https://github.com/MFarabi619/hack-the-tunnels-git-workshop
? Add a remote? Yes
? What should the new remote be called? (origin)

Hit enter, leave it as origin.

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name hack-the-tunnels-git-workshop
? Repository owner MFarabi619
? Description
? Visibility Public
✓ Created repository MFarabi619/hack-the-tunnels-git-workshop on GitHub
  https://github.com/MFarabi619/hack-the-tunnels-git-workshop
? Add a remote? Yes
? What should the new remote be called? origin
✓ Added remote https://github.com/MFarabi619/hack-the-tunnels-git-workshop.git
? Would you like to push commits from the current branch to "origin"? (Y/n)

At this point you're done :)

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name hack-the-tunnels-git-workshop
? Repository owner MFarabi619
? Description
? Visibility Public
✓ Created repository MFarabi619/hack-the-tunnels-git-workshop on GitHub
  https://github.com/MFarabi619/hack-the-tunnels-git-workshop
? Add a remote? Yes
? What should the new remote be called? origin
✓ Added remote https://github.com/MFarabi619/hack-the-tunnels-git-workshop.git
? Would you like to push commits from the current branch to "origin"? Yes
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 270.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/MFarabi619/hack-the-tunnels-git-workshop.git
 * [new branch]      HEAD -> main
branch 'main' set up to track 'origin/main'.
✓ Pushed commits to https://github.com/MFarabi619/hack-the-tunnels-git-workshop.git

Navigate to the url in the final line to see your git repository hosted on GitHub.

Nice, now your friend can get your cool stuff with:

Terminal
# Replace my GitHub user ID with yours
gh repo clone <your-github-id>/hack-the-tunnels-git-workshop

The format for what comes after `gh repo clone` is the same as shown on the top left of your GitHub repo.

Creating and renaming branches

Your friend calls you over...

UH OH

Your crush is there too. We need to impress them somehow.

Your friend has the same code as you and your crush has probably already seen it.

Your cool thing must not look the same as theirs. You need to quickly make changes and save them to a different version of the file.

Let's see the branch we're currently on.

Terminal
git branch
* main

If you see (END), press q to quit.

Make a new branch called my-branch.

Terminal
git checkout -b my-branch
Switched to a new branch 'my-branch'

Check the branch you're on again.

Terminal
git branch
* my-branch
  main

Make some changes to your file, run them to check that they work and commit them.

file.sh
#!/bin/bash
echo "Hack the Tunnels"
cowsay -f milk "Hack the Tunnels" | lolcat
cowsay -f dragon "Hack the Tunnels" | lolcat
cowsay -f cupcake "Hack the Tunnels" | lolcat
cowsay -f meow "Hack the Tunnels" | lolcat
Terminal
./file.sh
Terminal
git commit -m "change cowsays"

That didn't seem to work. This is because git doesn't automatically mark your changes to be committed.

Let's stage them.

Terminal
git stage file.sh

Now try again.

Terminal
git commit -m "change cowsays"
[main 94251e9] change cowsays
1 file changed, 4 insertions(+), 9 deletions(-)`

Nice.

Let's change the echo to something cooler, like figlet, and RGB it as well.

file.sh
#!/bin/bash
figlet "Hack the Tunnels" | lolcat
cowsay -f milk "Hack the Tunnels" | lolcat
cowsay -f dragon "Hack the Tunnels" | lolcat
cowsay -f cupcake "Hack the Tunnels" | lolcat
cowsay -f meow "Hack the Tunnels" | lolcat
./file.sh: line 2: figlet: command not found

Uh oh, do we have figlet installed? Let's check.

Terminal
figlet
bash: command not found: figlet

Looks like we don't. Let's install it and try again.

Terminal
brew install figlet
Terminal
./file.sh

It works! We can both stage and commit it in one go with -am

Terminal
git commit -am "replace echo with figlet"

See if everything worked and what the current state of our repository is.

Terminal
git status
Terminal
git log

Let's push our changes to remote.

Terminal
git push

You will see:

fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin main

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

This is because GitHub (upstream) doesn't have the main branch that we created locally.

So we do:

Terminal
git push --set-upstream origin main
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 622 bytes | 622.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
remote:
remote: Create a pull request for 'main' on GitHub by visiting:
remote:      https://github.com/MFarabi619/hack-the-tunnels-git-workshop/pull/new/main
remote:
To https://github.com/MFarabi619/hack-the-tunnels-git-workshop.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

Now you can find your branch on GitHub.

Switching between branches

Let's see what the file looked like before on the other branch.

Terminal
git checkout main

You'll see file.sh change to the version it was before.

Back to my-branch:

Terminal
git checkout my-branch

There are many more git commands, and this barely scratches the surface.

What will be the outcome of your adventure? It's the one you create here in present :)

To be continued...

Git commands review

ActionCommand
Create repository in the current directorygit init .
Clone repository from GitHubgh repo clone username/reponame
Upload repository to GitHub (also creates repo)gh repo create
Create new branchgit checkout -b newbranch
Switch to existing branchgit checkout oldbranch
Track filegit add filename
Stage changes for filegit stage filename
Commit changes with messagegit commit -m "your message"
View commit historygit log
View status of changesgit status
Push changes to remote repositorygit push
Pull changes from remote repositorygit pull

📕 Learning Resources

Last updated on

On this page

Edit on GitHubMade with 🩶 for Hackers by Hackers