Intro to Github

turpp

Thomas(Tripp) White

Posted on May 21, 2021

Intro to Github

Github is used by developers across the world. It can be a little overwhelming for new developers. In this article I hope to clear up the basics of Github for new developers. I will discuss what Github is, what purpose does it serve, and some real life examples.

TLDR

Creating a new Repository

echo "# <repo-name>" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin <git@github.com:username/repo-name.git>
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Creating and Editing

git branch
//Shows what branches that already exist
git checkout -b <newBranchName>
//This will create a new branch and also make it your active branch
git push -u origin <newBranchName>
//This will push the new branch and update your repo
git add .
//This will stage all the changes that occurred in this branch
git commit -m "message to attach to the commits"
//This message will be attached to your changes
git push
//This will push the changes to that branch and update your Github repo
Enter fullscreen mode Exit fullscreen mode

Merging

git checkout main
//This will take you back to your main branch. Main is the name of the branch you want to merge with. Remember you can check this with git branch
git merge <newBranchName>
//This will merge the two branches together. 
git push
//This will push the changes to that branch and update your GitHub repo
Enter fullscreen mode Exit fullscreen mode

Deleting

git branch -d <newBranchName>
//this will delete the branch locally. This will only delete the branch if is has been pushed and merged. If you want to force delete the branch without being pushed or merged use -D instead of -d
git push origin :<newBranchName>
//this will delete the branch in the repo on GitHub.
Enter fullscreen mode Exit fullscreen mode

What is Github?

Github is a code hosting platform that helps developers and teams manage code. It works similar to and external hard drive on steroids. At the most basic level, it gives you the ability to save your hard earned code on something other than your local machine. This has multiple benefits. It gives you the ability to have backups, share code, control versions, and access your code from multiple devices. There are some other great features of Github, but that is out of the scope of this beginner intro.

What purpose does Github serve?

We know what Github is but how does this intertwine into your workflow. When I first started learning to program I used GitHub in the most basic manner. For every practice problem or project I would worked on, I would create a Github repository. A repository acts like a folder on your desktop. This repository holds all the code that you are working on for that specific project. Setting up a repository is very simple. Github lays out a very detailed easy to follow path to setting this up.

echo "# <repo-name>" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin <git@github.com:username/repo-name.git>
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Doing the above will setup your repo. To update and make changes to the repo there are a few commands to be aware of.

git add . Yes, that space and period after add is on purpose. Thi command will stage all the changes that you have made and get it read to commit.

git commit -m “<some text to describe commit>” The text you put in this message is very important. You want to make sure you correctly describe what changes you have made. Taking your time and doing this correctly will make sharing and debugging your code later much easier. These commits will show up on your Github next to the files that were changed in your repository directory.

git push This will "push" the changes to your GitHub. This command is what saves your changes to your Github repo.

Branches

Branches is the last topic I will discuss in this intro. Branches are vital to developing with Github. Going back to our folder example, a repo is like a folder on your desktop. Creating a branch is like duplicating that folder on your desktop. This allows us to be able to introduce new features and also work in teams much easier.

When introducing a new feature to your project, you want to make sure you don’t destroy what you have already created. You can create a new branch and work on that feature just on that branch. This gives you the freedom to change and experiment with whatever you want because you know that your master branch is untouched and fully functional. Worst case scenario, you get too lost in the weeds and not sure how to get the project back on track. You can always just delete that branch and create a new one to try again. Best case scenario, you implement your new feature flawlessly and you merge it with the master branch.

Branches makes working in a team a breeze. Branches allow features to be divided among team members. Each person can create a new branch and work on that feature. Once they are finished with the feature they can easily merge with the default branch. Once merged the whole team can pull the new updated default branch and repeat the process. If a team member gets stuck on a problem someone else can pull the branch and give them assistance without harming what they are working on.

Pro Tip

Last thing on branches, as a new developer you are probably following a lot of tutorials when building projects. When you create a branch you can use it as a way to save that tutorial. After you have finished the tutorial and merged you can later revisit that branch to see how you implemented that feature. This will help you learn and grow as a developer and something many developers don’t take advantage on till later in their career.

Here are the commands to create, update and merge a branch

Creating and Editing

git branch
//Shows what branches that already exist
git checkout -b <newBranchName>
//This will create a new branch and also make it your active branch
git push -u origin <newBranchName>
//This will push the new branch and update your repo
git add .
//This will stage all the changes that occurred in this branch
git commit -m "message to attach to the commits"
//This message will be attached to your changes
git push
//This will push the changes to that branch and update your Github repo
Enter fullscreen mode Exit fullscreen mode

Merging

git checkout main
//This will take you back to your main branch. Main is the name of the branch you want to merge with. Remember you can check this with git branch
git merge <newBranchName>
//This will merge the two branches together. 
git push
//This will push the changes to that branch and update your GitHub repo
Enter fullscreen mode Exit fullscreen mode

Deleting

git branch -d <newBranchName>
//this will delete the branch locally. This will only delete the branch if is has been pushed and merged. If you want to force delete the branch without being pushed or merged use -D instead of -d
git push origin :<newBranchName>
//this will delete the branch in the repo on GitHub.
Enter fullscreen mode Exit fullscreen mode

I hope this article was helpful in increasing your confidence with Github. These are some great nidbits to get started but there is much more to take advantage of with Github. Explore their documentation and get comfortable with Github. Employers will be thankful!

💖 💪 🙅 🚩
turpp
Thomas(Tripp) White

Posted on May 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related