Git that Sh*t. The most pragmatic guide to Git and GitHub.

First things first, Git and GitHub are not same.

In other words what ‘Video’ is to ‘YouTube’, ‘Git’ is to ‘GitHub’, okay…lemme boil it down more : What ‘Porn’ is to ‘Pornhub’, ‘Git’ is to ‘GitHub’.

What exactly is Git and GitHub?

Git is a Version Control, that’s just a cool way of saying : “Git is a software used for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development.”

On the other hand, GitHub, Inc. is a provider of Internet hosting for software development and version control using Git. It offers the distributed version control and source code management functionality of Git, plus its own features.”

This is a beginner guide and beginners do not read theory(or do they?) So let’s get our hands dirty and create something up to use git and GitHub.

Git Basics:

**I’m using a Debian based OS so I’ll execute commands on Terminal, you can do similar stuff on your Window’s Git bash or Mac’s Terminal.**

Let’s first create a local repository(a simple folder on your PC) and a README.md file. A README.md file is often the first item a visitor will see when visiting your repository. README files typically include information on:

  • What the project does
  • Why the project is useful
  • How users can get started with the project
  • Where users can get help with your project
  • Who maintains and contributes to the project

md stands for markdown documentation, README.md is used to generate the html summary you see at the bottom of projects.

Now let’s initialize this gitprac with git, by using the command:

git init

if you’ve done things well and good, you’ll see : Initialized empty Git repository in /home/<your system name>/gitprac/.git/

**if not then you might not have git installed on your PC. Do it first…**

You can see that I’ve also used the command git status .The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.

git status tells us three things:

  1. On branch master, git tool works in a manner that you can create off-springs of you project, and all those off springs are branched from the main branch. By default ‘master’ is the main branch.
  2. No commits yet, you’ve committed nothing. For now think of it like : git has not recorded/tracked any of your files.
  3. Untracked files, all the untracked files will be shown here, in our case it’s README.md

Now it’s time to stage our file, what’s stage and staging? Stage is a hypothetical area which contains all the files you will be committing. And staging is the process of adding files to the stage area. Sounds legit, right?

For staging a file use the git command : git add <filename>, you can also use the command git add . it adds all the unstaged files to the staging area, but it’s not a good practice if you’ve got only one file to commit.

so we’ll use git add README.md to add our README file to the staging area.

Remember till now you’ve only added files to the staging area, this doesn’t mean that git will record the changes and your files, yet, for that you’ll have to commit all these files.

For committing files we use the git command git commit

git commit -m “add a message to your commit”

git status displays :

  1. On branch master
    nothing to commit, working tree clean

this means that there are no new files left in the staging area to commit. I’ve added a very small and poorly written commit message, when committing to something important I use this template:

feat: added README file(for eg: )
^ — ^ ^ — — — — — — — -^
| |
| +-> Summary in present tense.
|
+ — — — -> Type: chore, docs, feat, fix, refactor, style, or test.

Let's talk about git branches :

Now our file is already committed let’s create a branch and let me show you the real power of git,

Git command to create and switch to a new branch :

git checkout -b <newBranchName>

We'll use : git checkout -b <powerofgit>

Now we are in a new branch powerofgit, let’s make some changes in our directory gitprac and commit them.

Why do we create new branches ?

We create new branches to work on new features, any changes in our powerofgit branch will bring no effects to our master branch, this way we can always be sure that our master branch contains our ready to use product and all the new features are added to our branches. The beauty of git is that you can have branches from branches from branches from branches….it’s metaphorical but literal.

I’ve added a new text file here and commited it too. Now let me switch and see what happened in our master branch. Command to switch to any branch : git checkout <branch name>

To checkout to branch master : git checkout master

Let’s look at the content of our branch master :

As you can see we only have one single file README.md , all the changes you made in powerofgit branch had no effect on master branch, now this is what makes git powerful, in huge projects you can have as many branches as you can and all them are recorded separately by git log.

How to merge branches?

Now that I’m sure, my powerofgit directory is bug free, I want to merge it with master branch. Suppose you are in Branch A and you want to merge B into it, use command : git merge B

git merge powerofgit

git branch : to check the number of local branches.

You might be wondering what is the meaning of local branch, well any thing that’s solely present in your system, is called local. eg: local repository, local branches…etc.

Now these are the contents of master branch :

You can clearly see now it also contains powerofgit.txt which was only added to ‘powerofgit’ branch, this is because we just merged the branch ‘powerofgit’ in our ‘master’ branch.

How does GitHub comes in action?

Till now we’ve just talked about local repositories and now it’s time to create a global repository, I’ll be discussing the most simple way of doing it :

You’ll get some options to choose from, like you can make your repo private or public, you can also add README file and license in this step, but for our demo we won’t do any of that right now.

after this you’ll see this screen, copy the https url to your repo, this will be used to connect your local repo to your remote repo in the next steps.

Now let’s connect our local repo to our remote repo :

git remote add origin <link of your repository from github>

upstream/origin are just alias names of our remote repo you can call it anything you’d like to, I’ve used ‘upstream’ here.

Now let’s do the last thing : push changes to the remote repo…

Pushing is nothing but simply the process of uploading local repository content/s to a remote repository.

git push <remote repo name> <local repo name> is used to push changes to the remote repo.

that’s it, I hope now you can : Git that sh*t.

further reading :

How to fetch and pull. And you’ll learn everything else once you start using git and GitHub on a daily basis.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Gururani

Abhishek Gururani

31 Followers

I write articles on Android Development, and problem solving.