Git Basics — An Idiot’s Guide.
Rukee OjigboBlockedUnblockFollowFollowingMay 12We are consistently making changes to files as programmers.
You add new lines of code, you pull out old ones or refactor them.
With this comes a risk, you could eventually mess things up and want to go back in time to a point where everything worked perfectly.
Well you could probably have decided to “ctrl + z ” till whenever that was.
But why would you want to kill yourself?Behold Git.
See the light.
comSo what the heck is Git?Git is a version control system used for tracking changes made to files.
Although largely used by programmers, git can be used by virtually anyone…even your village people (ask Nigerians.
), to track changes made to your project.
Git was invented by in 2005 by Linus Torvalds.
You most likely didn’t come here for history classes.
So let’s forge on.
Let’s BeginTo start using git for version control, git has to be installed on your computer.
To install git, follow the link: https://git-scm.
com/downloadsNow that we have git installed.
In your terminal, navigate to your project folder and run the following code.
git initThe above command initialises the folder as a git repository and tells git to start keeping track of changes made to files in that folder.
You can simply just open the project folder in Visual Studio Code and run the command in the terminal.
That’s what I will be using.
To add a new file… you can just create it normally…or run the following code in your terminal.
touch <fileName>I have created a sample index.
html and index.
js file as sample.
My folder looks like this:We just created those files.
To the status of our files, if they are being tracked or not, run the following codegit statusIf we run the above line of code in our terminal, git tells us about the files that were created and that they are not being tracked.
Now we have to commit these files to git… so that git can keep track of them.
Before we proceed, let me take a moment to clarify certain things.
In git, there are three levels, these are:The Git RepositoryThe Staging Area (or Staging Index)The Working DirectoryThe Git Repository:This is where git stores files that have been committed (handed over) to it to track.
Here, the history of changes made are kept and each one has a unique key known as SHA key attached to it, with which you can navigate to that particular version of the file or project.
The Staging Area:The staging area, is like an intermediary between the git repo and your working directory.
The Working Directory:Your working directory is your normal folder where you are currently working on.
Every-time you make a change to a file in your working directory, git compares the version it has in the git repo to what you currently have in the working directory and let’s you know that the file has been modified.
Alright let’s go back.
Git was telling us we had untracked files.
Adding Files To the Git RepoTo make git track these files run the following commands on your terminalgit add <fileName>Adds the relevant file to the staging area…you can use (git add .
) to add all the files.
git commit -m <add a message here relevant to changes you made>This takes the file from the staging area to the git repo…Now if we run the “git status” command….
We see that there’s nothing to track.
To read and review all the changes that have been made to your git repo, simply run the following codegit logWe’ve only made one commit.
So this is what our history looks like.
Creating Branches With GitGit also allows us create branches.
Say you want to try out a new idea, like create a login system on your project and you don’t want to risk messing up with your main project.
Rather than copy the entire folder into a new folder, you can just create a separate branch, where you try out your new idea.
Once you are done and sure everything is working fine, you can merge this branch into your main branch.
To create a new branch, first ensure all changes made have been committed to your git repo.
git checkout -b <new-branch-name>To view the branches you have and know the current branch you are on.
Run the following code:git branchYou can switch from one branch to another and work on the different branches simultaneously.
The changes made in each branch isolated from the other.
Switching Between Branchesgit checkout <the-name-of-the-branch-you-intend-to-switch-to>Merging BranchesSwitch to the branch you want to merge into.
Then:git merge <the-branch-you-would-like-to-add-to-the-existing-branch>To Delete a BranchSay you no longer want a branch and you want to delete it.
git branch –d <branch-name>Going back in Time.
Let’s say you made a mistake and you want to return back to a point in time where everything was working well, do a “git log”, copy a part of “SHA key” of the particular history you want to revert to.
The run the following command:git checkout <SHA key-of-the-commit-you-want>Saving to an Online RepositoryGit also gives you the chance to upload your files to a remote repository (online location).
So, other developers can access it if you want…or just to store it.
While there are several online platforms for storing git projects, github appears to be the most popular.
And what I am going to use for this illustration.
You need to have an account first, if you don’t, you can visit their siteStep 1:Create a new repo.
In the top right corner, click on the plus (+) icon….
then click on new repositoryStep 2:Enter a name for your repository and description.
Then click on create new repository (the green at the bottom left corner)Step 3:Copy the following line and paste your in terminal.
The command is basically:git remote add origin <the-link-to-the-repo>To confirm that the above command work, you can just type in “git remote” in your terminal and you should see “origin”.
You just successfully added a remote repo to your project.
Now let’s push our project online.
git push -u origin masterThis is for your first push.
Subsequently, to keep updating your online repository you just run the command.
git pushThere’s more to know about git, but i hope this gives you a basic introduction.
Thanks for reading.