DEV Community

Billy Okeyo
Billy Okeyo

Posted on • Originally published at billyokeyo.codes on

Git Commands (Continuation)

In this article we are going to talk about pull command and branches. This will be a continuation of the second bit of the series. So let's get started.

In our last article we had talked about how to add code in GitHub from the online editor. So let's now talk about starting your project locally and pushing to github.

2. Writing your code in your text-editor locally then pushing to GitHub.

First we need to create our project folder, then inside the folder we can create a Readme.md file as we did earlier but this time locally. Add some content into your Readme.md file then save.

Now we need initialize git locally in our machine and we do that by running

git init
Enter fullscreen mode Exit fullscreen mode

This will initialize git and a hidden folder will be created called .git which will be saving all our commits.

Now that we have that set up, we now need to add files to our git and we do that by running

git add .
Enter fullscreen mode Exit fullscreen mode

This will stage all our files in the current directory then we can now commit by running

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

All these commands I had explained in the previous article, so if you don't understand what's going on here, I'd suggest you go check those articles.

Up to there, we now have our files committed but they are still within our local machine, we need now to push them to github, and this time we will use a different command before the push command.

We need to tell GitHub where it should push our files into.

So just head on into Github quick and create a repo, I'll name mine "demo-2", name yours anything you want then as we copied the url when we wanted to clone you can do that, or after creating your repo you'll see a screen showing you how to add files into your repo, there will be a link that will be displayed there, copy that link.

Now let's go back to our terminal and add that and we do that by running git remote add origin

git remote add origin git@github.com:cartel360/demo-2.git 
Enter fullscreen mode Exit fullscreen mode

After that we can check which remote repositories we have by running

git remote -v
Enter fullscreen mode Exit fullscreen mode

Note: You only set the remote repos once, after doing that, you can now push without running the add remote command again.

After setting this, we can now use

git push origin master 
Enter fullscreen mode Exit fullscreen mode

And this will now push our code to GitHub.

And that's all for this, you can now continue adding more files and code, then you git add . then git commit then git push

3. Pushing an Already Created Project.

There are two ways you can do this.

You can just use the GUI in the GitHub webpage as shown below and drag and drop your files or you can do it from your terminal.

You will Click on The _ "Upload Files" _ button which will open another screen which will allow you drag and drop files or Choose your files.

Doing it from the terminal follows the same procedures as discussed in (2). You just initialize your project with git init, then add your files with git add . , commit the files with git commit, create a repo in Github and copy the link and add a remote repo with git add remote then finally push.

Now that that's done, we'll go ahead and discuss about the pull command but before we get into pull let's discuss briefly about branching

Branching

When working with git you might have noticed somewhere written master, master is a branch and it acts as the main branch. You can work with that one branch or you can choose to make other branches.

On creating a branch you'll be be able to make changes in that particular branch without messing with the master branch. This is ideal when you want to test out things but don't want to break your code. You can now test your codes in your created branch and when satisfied with the outcome you can merge the changes into your master branch. This is also ideal if you are working with a team and each person handles different features of the project.

Type

git branch 
Enter fullscreen mode Exit fullscreen mode

This will list all your branches.

To exit out of this type q

Now to create a branch we use git checkout -b

git checkout -b feature-readme
Enter fullscreen mode Exit fullscreen mode

You can use any branch name you prefer but make it as much descriptive as it can be.

-b is used when creating branches. So only use that when you want to create a new branch

To switch between branches we now use git checkout but without the -b

git checkout master
Enter fullscreen mode Exit fullscreen mode

This will switch our working to master, you can do the same to switch back to our feature-readme branch, you'll only change the branch name.

Hope you have switched back to your created branch because we want to work on it.

Now add something in your readme file then run

git commit -am "Made changes to readme" 
Enter fullscreen mode Exit fullscreen mode

This will commit the changes and note the _ -am _ I have used that because I want it to add/ stage changes then commit instead of running git add first then commit.

This changes will now be saved in your feature-readme branch and won't be visible in your master branch.

Let's now merge the feature-readme into our master branch and we do that by running git merge

Before merging make sure you switch to your master branch then run the merge command

git merge feature-readme
Enter fullscreen mode Exit fullscreen mode

This will merge those changes you made into your master branch or alternatively you can merge the changes in GitHub where you'll use the graphical representation.

That's it guys, this article is becoming kind of long, so I'll stop there and in the next article will be our final article for this article, where we will talk about how to make a pull request, talk about the pull command and how to merge your branches in github.

See you in the next article.

Top comments (0)