Categories
Technology

Making Sure You Get Git

Cloning, Committing, Pulling, Pushing, Merging, Master.

Photo cred: Medium

I’m happy to write a full-blown in-depth post about git best practices. Let me know in the comment section of this post. I think it’s a really important subject for new developers. And even seasoned vets push to the master branch on accident every now and then.

But I wanted to cover some of the basic features of git and how you can get some footing on the open source control system. If you’re totally new to git, this might be a helpful read to get familiar.

Let’s say you want to create your first repository. You may even be new to coding and want to pull down someone else’s project to get ideas. For new developers this can also be a great way for digging around someone else’s code, or seeing how another developer leveraged an api or library you have been interested in.

The first thing you’ll need to do is download git. Then I want you to generate a new ssh key and add it to your GitHub account (that way you can clone repositories from GitHub using ssh and https, which are two different protocols).

Then go over to GitHub. I’m going to post the screenshots to a project I am creating with a new repository. And once I finish posting this article, I’ll have a project that you can pull down to your local machine (you’ll know what that means by the end of this post too!)

I’m creating a public repository here called ‘git-familiar’ –

Once I’ve created the new repository, GitHub provides me the steps I can take to clone the repository locally (which means you’re going to make a copy of that code in the remote repository named ‘git-familiar’), and you can make your own changes to it on your laptop or desktop, save those changes, and push them to a branch that you created.

I just want to cover our basis before we go any further. By this point, you should have installed git on your machine (laptop, desktop, computer, etc..) The you should have gone to GitHub and clicked on the repository named ‘git-familiar’. You should have copied the repository link (ie: https://github.com/daleyarborough/git-familiar.git). Next I want you to open up a terminal (if you are on a windows machine). Next I want you to create a new directory on your machine with the command mkdir in your terminal. This will make a new directory for you to clone the project into –

Next I want you to type the command cd in your terminal to change directories into the new directory/ folder called ‘git-familiar’ –

Now we’re going to speed things up a bit and start talking about the power of git. I want you to type git init into your terminal. This will initialize an empty Git repository in your directory/folder. Then I want you to type:

git clone https://github.com/daleyarborough/git-familiar.git

This will create a copy of the project to your local machine. I created a new MVC .Net project (you will need Visual Studio installed locally to run it) with the command dotnet new mvc. So when you typed in git clone https://github.com/daleyarborough/git-familiar.git into your terminal, you will have cloned a .Net MVC application locally.

If you want to run the MVC application locally, type start git-familiar.csproj in your terminal. This is what it should look like once you do –

If you want to run your application locally, this is what it should look like –

So let’s get back to git. Once I made sure I could run the project locally, I needed to create a master branch (pictured below) and then afterward push the code up with the command git push –set-upstream origin master –

Here’s what it looked like on GitHub after I pushed up the new project code up to the master branch I just created –

This is where you come in. Now that you’ve cloned the project locally, you can create your own branch, commit the changes, and then push them up to GitHub.

You’ll need to start by creating your new branch, we’ll name it develop for this example, with the command: git checkout -b develop. Now we have created the new branch name and we have checked out that branch. Now you can make your changes to the code in the develop branch without affecting the code in the master branch.

You will need to follow the same commands as I did when you are ready to push your changes. Type git add –all in your terminal to save all of your new changes locally to the develop branch. Type commit -m “Pushing new code changes to the develop branch” in your terminal. This will add a commit message to your new commit so that you can communicate with other developers (especially if you are on a team of developers) about the changes you just made to the original ‘master’ branch code. Once you are ready to push those changes to the remote GitHub branch location, type git push –set-upstream origin develop in your terminal. You should be prompted to login to your GitHub account. Once you’ve entered your credentials, you should see logs that show your code has been pushed to the new branch. Like this –

Nice job! If you’ve followed me all the way to the end, I am super proud of you! This was really fun for me to put together, I hope you enjoyed it! 🙂

Fun Fact: Technical recruiters often look at your GitHub to see if you’ve been working on projects recently and what kind of languages you are proficient with.

Resources mentioned in this article:

Dale Yarborough

By Dale Yarborough

I am a Software Engineer at General Motors and Appalachian State University Alum. Previously: Whole Foods Market IT, Charles Schwab