DEV Community

Billy Okeyo
Billy Okeyo

Posted on • Originally published at billyokeyo.codes on

Git Commands

Hey there, In this article we are going to try the Git commands we had discussed in our earlier sessions and see how each works. If you didn't read the first article of this series I'd suggest going back and reading it here . If you are okay you can proceed with this one.

So I'm hoping you have your text-editors ready, I personally prefer vscode and that's the one I'll be using in this article so let's get started .

Creating Files

The very first thing after creating your repo is creating your files. There are three ways you can do this.

  1. Writing your code directly in the online editor in GitHub
  2. Writing your code in your text-editor then pushing to GitHub
  3. Pushing an already created project or files.

1. Writing Files in the Online Editor

In our last article we had left on a screen that displays how to add files into your repo. In the same screen, you will see a link written creating a new file, click on that and a text-editor will be opened for you. Let's create a simple readme file here.

.md extension stands for markdown. Markdown is a easy and cool way to format your texts.

After creating the file you can scroll down and commit the changes, note the the two text boxes at the bottom just before the "Commit New File", the first text box is used for your commit message where you say what you did. The second is for the message description where you can explain in details what you did in your commit.

You can leave it as default for now and Commit.

You have now successfully created the file. Now lets try our first Git command.

Clone

We want to use clone to download the repo we just created into our local machine.

In your current repo screen copy the url, it will be github.com/your_username/your_repo_name. We'll use this link somewhere.

https://github.com/cartel360/demo-1
Enter fullscreen mode Exit fullscreen mode

Head over to your machine terminal, hoping you have git installed. If not just google on how to install git in you specific platform, I won't handle that because I want this article to be short as possible.

To check if you have git installed type this in your terminal

git --version 
Enter fullscreen mode Exit fullscreen mode

After opening your terminal type git clone then paste the url you copied

git clone https://github.com/cartel360/demo-1
Enter fullscreen mode Exit fullscreen mode

Note: you need to have navigated to your project folder before running the command because it will download the repo in the current directory. In vscode it's easier because it has an integrated terminal which opens in the current directory you are working on.

After completion you will see your GitHub repo and a readme.md file inside it just as it was in the online editor. And that's it for the clone command.

You can now edit the your files in your local machine and commit the changes. So let's get into that.

Edit your readme.md file and add something of your choice then we try the commit command but before we try that you can run

git status
Enter fullscreen mode Exit fullscreen mode

This checks for any modified files and changes you made. Then we add the changes by running

git add Readme.md 
Enter fullscreen mode Exit fullscreen mode

or

git add .
Enter fullscreen mode Exit fullscreen mode

add . will add everything in the entire the directory and add Readme.md will inl add the changes in the Readme.md file

commit

Now let's try to commit the changes we made into git. We do that by using the commit command.

git commit -m "Updated Readme"
Enter fullscreen mode Exit fullscreen mode

-m stands for message and the the words in quotes is the message for our commit.

Now you have successfully committed your files to git, but the changes you made are not online yet, they are still within your local machine. To get them online to GitHub for example, we'll use another git command called push, so let's get into it.

push

Push is a command used to push your locally made changes to GitHub so that it can reflected in your online repo as well.

To push your changes lives you might need to set github SSH keys with your GitHub credentials so that when pushing GitHub will know you are the owner of the account.

I won't talk about setting SSH keys in this article but you can google it out and find solutions to that, instead we'll use vscode which has a GitHub integration thus we will only need to set it once and it will store and our GitHub credentials. I can't recall the exact way I did mine so I can't share on how I did it but I guess yours will pop up after writing the push commands and tell you want to do in order to set it up.

So let's try it out.

Now that we have our changes already committed, let's push those changes.

We do that by running

git push origin master
Enter fullscreen mode Exit fullscreen mode

Origin stands for the location of our git repo and master stands to the branch we'll push into.

You'll notice I have been asked gor my github credentials, this is because I was using the normal terminal, and I don't have SSH keys integrated. Your vscode terminal will most probably ask for the same. I didn't want to use the vscode terminal because I have ny credentials there already so the push would just go successfully without asking for github credentials.

After that all our changes will now be live in GitHub, you can go back to GitHub and you'll see your changes live there.

This article is becoming long, so I'll stop it there so that I give you time to master three commands we just discussed.

In the next article I'll talk about the other two ways to add files into GitHub, the pull command and some other minor git commands.

See you then and don't forget to hit that 👍🏽 button if this helped.

Top comments (0)