Don’t Get Stuck With Git

Sylva Elendu
Basic Coding Challenge
7 min readJan 29, 2018

This is git for first timers

source: www.pexels.com

You remember that upsetting feeling you had when the internet froze just before you sent an email, especially when you spent hours composing that mail. Yeah right, that feeling. Of course, you knew it meant rewriting your mail, but hey that’s a thing of the past now. Now, you can start composing a mail, close the tab and return to continue from where you left off. This concept is what smart guys call Version Control. Welcome to the present guys.

You use version control everywhere, from working on both your Google and Microsoft office tools such as docs, excel, spreadsheets, and the likes, to blog posts such as Medium and the rest.

So what exactly is version control? Here’s an awesome definition from github; “A version control system, or VCS, tracks the history of changes as people and teams collaborate on projects together. As the project evolves, teams can run tests, fix bugs, and contribute new code with the confidence that any version can be recovered at any time. Developers can review project history to find out which changes were made, who made the changes, when the changes were made and why they were made. To read more, click here.

Now all of these are beautiful, but then how do you share what you’re working on with your team, or say your partner, then you will need some centralized system, thus a Centralized Version Control Systems (CVCSs).

CVCSs are awesome except that centralized system loosely meant that if the central server that houses the databases goes down for an hour, then there will be no collaboration for that long, and you don’t want to know what happens if the server gets corrupted.

Distributed Version Control Systems (DVCSs) solves this by allowing your team to copy (let’s use clone henceforth please) your entire work history, thus preserving your files in the event that the server crashes and also enables collaboration. When your team clones your repository, they are basically cloning the full backup of all your work data contained in that repository. Example of DVCSs includes Git, Mercurial, Darcs, and the most common is git.

First timers always have issues with git and version control. We know this because a number of our Basic Coding Challenge participants complained about understanding the basics of git. This article should cover all the basics you need to know about git to get started.

Oh I should mention, you will have to learn and be comfortable with command terminals. Don’t worry, it is so easy to learn, and secondly, you will need a github account. Click here to create yours.

Download and Setup Git

To download git (for both Mac and Windows), use this link

It shouldn’t take much time to download. Click on the downloaded file to begin installation. Usually, it is safe to click next all the way until install. Launch after installation is complete.

Git Config

You will need to configure git. You can consider this as introduction rite. Here’s how, run the following command: ­

git config -global user.name “Your Name”

git config -global user.email yourname@mail.com

git config

You will need to insert your actual name in place of the default as well as for the email address. The first git command registers your name, while the second registers your email address. Usually, it is advisable you use the same details as provided on your GitHub account. Now, you have configured git. Congratulations on completing this.

Basic Git Commands

git init: This command is used to initialize your local git repository. It will create a hidden git folder by default. You can ignore this hidden folder, I usually do. Alternatively, a git repository can be created directly on the github account, but what’s the fun in that.

Also by default, your repository starts with the Master branch as is visible in the image above.

git status: This command is probably the most used command on the terminal, at least for me. It is used to track changes done on the repository. With this command, you get to know what you’ve done and yet to do.

git add: Now, let’s safely assume that you’ve done some work and have a new file in your repository or probably just imported a file into this repository. You will need to add this file.

In the image above, I created a basic Hello World using HTML. As seen above, I ran a git status command, and there in red ink is the Hello World in the index.html. The display command reads, “nothing added to commit but untracked files present.”

Next, I ran the git add index.html, and then the file changes to green. This means the index.html file has been added to the staging area. Files in the staging area are ready to be committed.

git rm -cached: This git command removes files from the staging area.

In the illustration above, I removed the index.html using the git command, git rm -cached index.html. Note: it’s a double hyphen, I can’t seem to type on Medium without preventing it from turning a larger hyphen. As seen above, running git status reveals the untracked file using the red ink.

git commit: Every time you make a key change or update, you’re required to commit this to the repository. This command is also very important as it saves a snapshot of your entire work history at the time of running the command.

You can consider this as checkpoints you can always return to if you encounter bugs going forward. In the illustration above, I added back the index.html file and then commit.

Notice the extra command after git commit index.html -m “commit index file” Git will always require you add a comment at the point of commit. Comments are important because it will remind you as well as anyone who reads through your code what you achieved at the point of commit. Oh you should know that commit is similar to saving your work history in your local repository.

git branch: You or any member of your team might be working on a unique feature that will later be incorporated into the main app. The best way to go about it is to create a new branch off the main branch. This allows you to work and test all functionalities in the new branch and be sure its working before merging with the main branch. To create a new branch, git branch <new branch>

In the illustration above, I used a slightly different command because it saved me some time anyway. If I run, git branch develop, a new branch develop will be created, but I will need to run another command, git checkout develop to open the newly created branch.

Running the command above not only creates the new branch but it checkout from the former to the new. You should know that when you checkout of a branch to another, all the committed files in the previous branch is automatically available in the new, but whatever changes done in this new branch doesn’t reflect in the former until merged.

git reset: This command is used to undo changes done on your working directory before it is published or pushed to a public repository.

Working With a Public Repository

git clone: This command creates a copy of an existing public repository. Git allows you to easily clone any project you fancy on github into your computer. Git is open source. This is awesome for team collaboration such that you can have your team remotely review and work together.

git push: This command lets you publish your local git repository to your public github account. This is equivalent to making your work available to be accessed from any remote systems.

git pull: This command pulls down the recent changes from a remote repository.

Once again, you should know that this isn’t the comprehensive list as a whole more can be done using your git commands. These are pretty much the basics. If you’re an advanced developer or work in large teams, you’d need to be more comfortable using git. Here are some recommend websites you should visit:

If you enjoy text, then click here, Atlassian

If you enjoy video, then click here, Traversy Media

For an interactive session, click here, Learn git in fifteen minutes

Thanks for reading this far. Please applaud if you found this useful. I will really appreciate that by the way. Also, you can also leave a comment as well, I will respond immediately.

--

--