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.
Now let's make a commit.
You should see.
Adding files
Let's tell git to track changes to the file we want to commit.
You should see:
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.
We don't want to create a new repository, but rather push an existing one.
Leave the path as is, and hit enter.
Leave the Repository name as is, and hit enter.
Select your personal account as the owner, and hit enter.
You can provide it any description you like. Since we're in a hurry, we'll leave it blank.
Leave it as public.
Hit enter to add the remote.
Hit enter, leave it as origin.
Hit enter, leave it as origin.
At this point you're done :)
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:
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.
If you see (END), press q to quit.
Make a new branch called my-branch.
Check the branch you're on again.
Make some changes to your file, run them to check that they work and commit them.
That didn't seem to work. This is because git doesn't automatically mark your changes to be committed.
Let's stage them.
Now try again.
Nice.
Let's change the echo to something cooler, like figlet, and RGB it as well.
Uh oh, do we have figlet installed? Let's check.
Looks like we don't. Let's install it and try again.
It works! We can both stage and commit it in one go with -am
See if everything worked and what the current state of our repository is.
Let's push our changes to remote.
You will see:
This is because GitHub (upstream) doesn't have the main branch that we created locally.
So we do:
Now you can find your branch on GitHub.
Switching between branches
Let's see what the file looked like before on the other branch.
You'll see file.sh change to the version it was before.
Back to 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
| Action | Command |
|---|---|
| Create repository in the current directory | git init . |
| Clone repository from GitHub | gh repo clone username/reponame |
| Upload repository to GitHub (also creates repo) | gh repo create |
| Create new branch | git checkout -b newbranch |
| Switch to existing branch | git checkout oldbranch |
| Track file | git add filename |
| Stage changes for file | git stage filename |
| Commit changes with message | git commit -m "your message" |
| View commit history | git log |
| View status of changes | git status |
| Push changes to remote repository | git push |
| Pull changes from remote repository | git pull |
📕 Learning Resources
Linux Journey
- Learn the basics of different Linux features through simple step-by-step tutorials.
Git How to
- Web app to easily demo basic git commands visually.
- Very limited in functionality.
Learn Git Branching
- Web app to learn Git commands interactively with guided lessons.
Git Visualizer
- Web app to learn simple Git commands visually.
Shortcutfoo
- Web app to drill commands, keyboard shortcuts and programming language syntax.
Lazygit
- A TUI (Terminal User Interface) version of Git.
- Saves you from typing out all the commands and makes you faster.