Learning Git Basics Through Pushing an Existing Project to GitHub + Shell Scripting

Carolina Delwing Rosa
7 min readApr 26, 2023

--

Quick intro about me

Hey guys, I’m Carolina, a Brazilian engineer that just moved to Toronto and is building a new life around here. In this blog, I will document my journey and transition from Engineering to DevOps, with the goal of sharing the projects I’m working on, solidifying the content I’m learning, and making new connections. I’m currently enrolled in the Applied DevOps Engineering course at WeCloudData and also taking some extra courses by myself.

Please, don’t hesitate to get in touch with me. I’ll love it! 🤟

Objective

In this quick tutorial, I’ll show you how to push an existing folder to GitHub. So If you’re thinking of uploading your projects to your GitHub profile, stay tuned. I’ll push a folder containing some of the Shell Scritps I wrote.

For learning Git and GitHub, I recommend two courses I took at Codecademy: Learn Git: Introduction and Learn Git & GitHub.

But what are GitHub and Git?

  • GitHub is a web-based platform that allows, according to the GitHub website, anyone from anywhere to build anything. In other words, it allows users to store, track, and manage code repositories online, with version control (Git), collaboration, and project management tools. It’s like a social network but for code.
  • Git is a free and open-source version control system used to track and merge code changes over time, collaborate on projects, and revert to older code versions if necessary. Git uses a branching model that allows you to have multiple local branches (that can be independent of each other).

Let’s get started!

To be able to complete this tutorial, you’ll need a GitHub account and a personal access token (here 🔥 BE CAREFUL! Save your token in an easy-to-access folder on your desktop because you’ll need it later). Also, you’ll need an internet connection, a terminal with Git installed, and basic command-line knowledge.

1. Create a new repository on GitHub

Log in to your GitHub account, and create a new repository (or “repo”). I’ll call it “bash_scripts” (keep it simple) and will put a brief description. You can choose whether this repository will be public or private. This repository is the place where your files will be available in GitHub.

2. Configure and initialize Git in the desired project folder

I’m using Ubuntu 22.04.2 LTS as my operating system (OS). To make sure you have Git installed, type the following command in your terminal:

git --version

Then, use the following commands to configure your identity with Git:

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

After that, navigate to the folder you want to push to GitHub using the cd command. In my case, the folder it’s called bash_scripts (the same as the name of the GitHub repo). The next step is to initialize Git in this folder using the git init command, which creates an empty and hidden directory ‘.git’ and allows Git to start versioning your project.

cd bash_scripts
git init

3. Git add, commit, push, YAY!

Now that the desired folder is initiated and you’re ready with your code alterations, we need to use the command git add to move these files from the working directory to the staging area, which is the place where the files “wait” before being committed. But what is committing? It’s simply saving the changes you made in your files to the local repo. The command below will add all files of the current folder to the staging area.

git add .

You can check all the files waiting to be committed with the command git status below. In my case, I have 17 script files waiting to be committed.

git status

The next step is committing these files. Use the command git commit, and add a message with — m or — — message. This message will be used when someone in the future needs to understand the commit, so, again, keep it simple.

git commit -m 'first commit bash scripts'

The output should look like this:

Now it’s time to connect your local Git repo to a remote repo on GitHub. Then, we will push the files that have been committed to the remote repository. Navigate to your remote repository on GitHub and copy the HTTPS link (highlighted in red in the image below):

In your terminal, add a remote repo called “origin” to your local repository. Use the command below specifying the HTTPS URL of the GitHub remote repository. After that, rename the default branch from “master” to “main” to ensure a more inclusive terminology.

Finally, it’s time to push! Git push will push the files from your local repository to the remote previously named “origin”. The “-u” sets the upstream branch as “main” and the repository as “origin” so that in the future you can type only git push. You will then be asked for your GitHub username and token.

git remote add origin https://github.com/caroldelwing/bash_scripts.git
git branch -M main
git push -u origin main

If everything went well, you should see the files you pushed on your GitHub repository like in the image below:

Sooo, we did it! That’s all for pushing files from a local to a remote repo!

4. Other (very) important Git commands

So, if you’re working, for example, on a big project with other people, you might need to pull code from GitHub, work on it, save the changes you made, and then push the code back to GitHub. But how do we do that? Well, it’s pretty simple actually.

  • Fork on GitHub: fork a GitHub repo means making a copy of the original repository on your GitHub account so you can work on it.
  • Clone the forked repo on your computer: create a local directory via your terminal ‘mkdir <new directory name>’, and use the command ‘git clone <forked repo HTTPS URL>’ to create a copy of the forked repo on your local repository.
  • Make changes to the files, then proceed with the steps explained previously: git add, git commit, and git push.
  • Pull request: once the changes were pushed to the forked repo, go to GitHub in your forked repository and click on Pull Request (PR) and New Pull Request. Explain briefly the PR and submit it to review and approval.

5. Shell scripting

Since I showed you today how to push files to GitHub (and I pushed a bunch of Shell scripts), I will briefly explain what Shell scripting is.

First of all, a shell is the outermost layer that allows users to interact with the operating system. You can provide an input (usually via CLI), that will be interpreted and then executed. There are many shell types available in Linux, but the Bourne-Again SHell (BASH) is the most common and it was the shell I used to create my scripts.

So, a shell script is basically a file that stores a sequence of commands used for file manipulation, text printing, program execution, and task automation. The script can be scheduled to run automatically, saving time and making the life of developers easier.

The script can be written in a text editor like VIM or Nano. You can define variables on it, use arithmetic expressions, logical operators, read user’s input, conditional statements, loops, and so on. Let’s take a look at the first shell script I wrote using Nano:

Name.sh: by convention, bash scripts’ name end with .sh (although they can be run without it).

She(#)bang(!): in the first line of the script, you need to add #!/bin/bash, which is a path to the bash interpreter and tells the shell to execute the commands using the bash shell.

#comments: you can use comments to document the code. They must start with a # to not be interpreted.

Commands: finally, you’re now ready to implement the commands you wish. In my case, I used the echo command to display a message, followed by the ls command to display a current list of directories.

After saving the script, you’re now ready to provide execution rights to your user.

chmod u+x welcome_ls.sh

And then, you can run the script using one of the following ways:

./welcome_ls.sh
bash welcome_ls.sh

And here’s the output:

If you’re interested in Shell Scripting, I’d recommend checking:

Learn the Command Line — Course by Codecademy

Shell Scripting for Beginners — A freeCodeCamp tutorial

My GitHub profile — To check the shell scripts I wrote

That’s all for today! Thanks for reading, and see you soon!

--

--

Carolina Delwing Rosa

Engineer & tech enthusiast. Passionate writer. Documenting my journey into DevOps & Cloud. Reach me here: https://www.linkedin.com/in/carolinadelwingrosa/