A GitHub Refresher

Bleak Chandler
4 min readMar 12, 2021

--

Photo by Chris Ried on Unsplash

Learning how to use and interact with GitHub, on top of learning a new programming language can be overwhelming for a new user. Let’s take a look at some basic commands and best practices to fully understand and get the most out of GitHub.

Basic Commands

First, let’s take a look at some basic commands for GitHub. While I was familiar with many of these steps, writing this blog post helped me better understand what they were all actually doing.

git init

The changes you make in in your code are tracked through something called a repository. The first command we need to use is “git init”, in our main directory, which will create a hidden .git file inside the main directory of our project.

This is a local git repository, to track the version history of our project, and this file is what turns your project into a Git repository, which will allow us to run Git commands.

git add .

Once you’ve made some changes to a file, use the git add command, which adds all of our code from the working directory to staging area (which is a place to record new data locally before committing to GitHub.)

git commit -m "Initial commit"

“git commit” commits all of our changes in the “staging area” to the local git repository with a comment (so at this stage nothing is being uploaded to GitHub still.)

git status

We can run the git status command at any point to see the changes we’ve made but not yet committed.

git push

Using git push will commit all of your modifications from the local repository to the remote repository with the comment we wrote with “git commit”.

git remote add origin <your-copied-github-url>

However, if we just try pushing this without first connecting the remote repository, it won’t work. To connect a remote repository, we can use the above command.

git fetch

git fetch updates your local repository, but it does not modify or change any files in the working directory. In other words it downloads new data, but doesn’t actually integrate them into your working files.

git merge

git merge gets files from the local repository to the working directory.

git pull

git pull is essentially a git fetch, followed by a git merge. So, in other words, it fetches the info, and then merges the changes from the remote repository.

Branches

Branches allow us to create a copy of the master repository, and make changes without changing the master branch. Since I’ve been working on smaller scale projects a student, it’s been hard to get myself in the habit of using branches, but they are critical for efficiently developing an application, as it’s all too easy to overwrite someone else’s changes on the master branch.

git checkout -b <branchname>

The above command will make a new branch, and will automatically put you on the new branch so that any new commits will go to that branch instead of the master.

git push [remote_branch_name] [name_of_local_branch]

The above command will push to the branch in GitHub.

git push --set-upstream origin <branchname>

Running the above command will allow us to set the default remote branch. If we now try to git pull, we will be pulling in commits from that remote branch into the current local branch.

git branch

You can then use the above command to see all of the branches.

Sources:

https://gist.github.com/JennDudley/2493288

https://coderefinery.github.io/git-intro/04-staging-area/

https://medium.com/@abhishekj/an-intro-to-git-and-github-1a0e2c7e3a2f

https://stackoverflow.com/questions/292357/what-is-the-difference-between-git-pull-and-git-fetch

https://medium.com/swlh/a-beginners-guide-to-github-e4747a93765c

--

--

No responses yet