For newcomers, Github is a version control system used by lots of software engineers for keeping track of code. Github can be hard! Really hard to learn!
Have you ever heard the saying,
"It's like riding a bicycle. You never forget?"
Github can be the same as riding a bicycle for any beginner! Everyone knows the power of metaphors or sayings. They stay in your head, so here’s one for learning Github for the first time.
Here are 6 steps to learn the basics of Github for the first time with a metaphoric comparison to remind yourself. If you’re using Windows, you will have to install Git-scm on your machine at http://git-scm.com. Afterwards, Windows users can use Powershell as your terminal.
1) Getting the code – Git repos are like letters to penpals!
I’m assuming that you’ve created a new account on http://github.com and your computer is ready with Git installed on your terminal. The very first step is downloading the actual code that someone or you have uploaded.
Let’s assume that you want to create a folder on Github to keep the code of your new project. To do so, you should create a repository on Github. Repositories are just another way of saying folders containing your code.
We fill information about the Git repository or folder. You can leave most of this by default except for the name and make sure that you initialize with a
Copy the Github link by hitting the button!
Open a new terminal, and type in the following command:
git clone (paste the link)
git clone https://github.com/huyle333/tutorial4.git
This command downloads the folder in the folder where your terminal is.
git clone is a command designed to download all the contents of the repository into a folder of the same name. For example, I’ll use git clone to download the folder with all the files (
README.md) called tutorial4.
Which leads me to the metaphor. Think of this git repository containing a line of letters and communication between you and a penpal in Antarctica. All communication with these letters stays within this Git repo. All the changes and updates to your penpal are noted within this Git repository and folder.
2) Adding a letter into an envelope
Once you’ve used git clone to download the repository, you should change your terminal location into the folder.
Now, we’ll get to the metaphor. Github is a system to keep code, but all these commands can get confusing. How do you simplify the process? What do you need to remember?
Let’s say that you add a file and write things inside it.
Confirm the prompt to create a new file. I’ll write a few words inside this file and save. If this were code, then I’d want to have this code on Github for safe keeping.
Adding files on Github for the first time is a pain in the ass.
Think of the process of adding a letter into an envelope before sending that letter. You’re writing a letter to your far-away friend who lives in Antarctica. After you’re done writing a few paragraphs, you’re ready to send the letter.
I want to do the same on Github. I’ve written some lines of code, and I want to add this new code onto Github.
git add .
This command adds all the changes into an envelope ready to ship, but it has not shipped yet.
git add is used to add your letter into an envelope. When you write a letter on a piece of paper, you always have to leave it inside an envelope. But, the key point is that you have not sent that letter yet, but it’s ready to go.
git add is a protocol that forces you to prepare the letter to be ready to send in its envelope package.
3) Writing a P.S. note on the bottom of the letter
After you’ve staged the files to be ready to add like leaving a letter in an envelope ready to be sent, on Github, you have to leave a commit message. Commit messages are little notes to keep track of what you just sent.
Think of it as a Github necessity to keep a log of a little note to why you uploaded these set of changes onto Github. For our metaphor, think of this Git commit as adding an extra P.S. note onto all of your letters.
git commit -m "added my first file to Github"
git commit means that we’re adding a message to the Github logs to why we adding these files ready to be uploaded onto Github. But remember! We have not uploaded our new code to Github yet, but committing messages is mandatory.
Up to now, we have a letter with new code and a P.S. note with a commit message at the bottom of the letter ready to be sent to our pen pal in Antarctica or update that folder on Github.
4) Enveloped letters have destinations
Every letter has an origin and destination. For cloned git repositories, the destinations are saved by default as “
origin master.” Step 4 is more of a conceptual understanding step.
The origin is by default the link of the Git repository. You can think of the origin as the sending destination that your letter is directed to. We’re sending a letter to our friend in Antarctica, so Antarctica would be our origin.
git remote show origin
We can see that the
origin is simply our link of the Git repository. What does master mean in origin master?
master is the main branch of Github. On Github, you can have different versions of the same code. You can think of this as alternate timelines. You can have versions of your code labeled as different timelines.
The main timeline is called
master, so our destination for beginner purposes is directed toward the main timeline. To list all the branches or timelines, you use the following command:
Now, we can get to actually uploading our new code and actually sending our letter to our penpal in Antarctica.
5) Postman gets the envelope from the post office box and sends it to its destination
After leaving our enveloped letter at the local post office box, the postman sends the actual enveloped letter to its destination as written on the envelope. How does he know?
Well, when writing an enveloped letter, we always put the address of the destination! This is the
origin master that I was talking about in step 4.
What we do to upload our enveloped letter with the P.S. commit message is the following command:
git push origin master
What we essentially do is we push or upload all changes that we have created in this added and committed enveloped letter to Github.
git push can be seen as an upload command while
origin master is the destination. You’ll have to enter your username and password when Git asks you.
origin is the link to which we are aiming these additions toward, and
master is the main timeline because Github can have multiple timelines. We’ve successfully uploaded the new file and code with the P.S. committed message!
6) You receive a letter in your mailbox
Anybody can make changes to the Git repository that you have added as a collaborator! In addition, you might switch computers where you don’t have all the latest changes.
How do you download the latest changes. This step is like receiving a new letter from your penpal from Antarctica.
All you need to do is the following command to get the changes on the main timeline:
git pull origin master
If there are changes, then you’ll download all those changes.
I’ve only touched the lightest layer of Github. If you’re completely new, this metaphor along with the commands may help you get started with Github for the first time! Refreshing the link on the Github page, we see the latest changes to the Git repository.
Our Github repository at our origin link has our new file with the commit logged message. Hurray!