Hi Everyone,
I have been waiting to add this article in my list for a long time now. GitHub is the most commonly used tool for storing files and to improve team collaboration. I will be documenting some of the most important commands that I have frequently used as well as give you a basic idea of what it is and what it solves.
The Whats & Whys
Imagine that I want to create an application consisting of multiple files. Lets say, I created 3 files: main.py, main.tf , terraform.tfvars
Now I am working in my own personal system. Here, I want a way to differentiate these files from the rest of my files. So , one way is to create a folder, that consists of all the 3 files.
Now that we have a folder, we can initialize this folder using command : git init
git init
We have essentially created a Git. Once we initialized the git, we will notice that a .git folder has been created. This is a very important folder that is the core of Git.
Now , imagine I have a group of members in my team. My team mate wants to go through the files that I have created and wants to do a review. So now comes GitHub.
Git
Git is a very smart tool. So lets explore what really happens when we want to upload files to GitHub.
I created a folder called ‘GitRepoForBeginners’ in my local system. Using git init command, I initialized the directory and now it has a .git folder.
I have created the 3 files for demo purposes.
Each git directory is created on a branch. By default the name of the branch is ‘master’. We can use the command git branch to find details of the current branch. All the files created now will be on this branch .We shall explore more on that later.
git branch
Once a file(s) is created , it will be on untracked files state. What it means is that, git has no idea about the presence of these files. As a prestart to bring it on to stage — literally — we give the command :
git add <filename> or git add . (dot adds all the file present in that repo to staging)
This will bring it on to the pre-stage , where the Git is now aware about files and keeps track of any changes to the file.
Now notice in the above screenshot that I used the command :
git status
git status to find the status of my Git. As on the screenshot, we can observe that 3 files are mentioned under Untracked files.
Now , we have added it to the Cached Stage environment ie prestaging area. Here we can validate file contents as well as the number of files.
But, Suppose , if I do not want to add it to cached Stage yet, I can simple remove it from there using the command
git rm -r --cached <filenames>
Observe that its back to Untracked files now.
Now I added it back to Cached Staging. I have validated my files, and so now we do a commit & give it to the actual Stage . So now, what exactly is a commit.
Commits
A commit is basically a distinct SHA value, which associates each change with it. Its a random string generated to keep track of the local as well as remote changes. The HEAD (as we will see later) keeps track of the most recent commit .
Ex : 6ea936d4dc94973164232de662deda6967897a69
So , we commit our changes using
git commit -m "<Message>"
If we do not give the -m (for message), it opens up an editor to add a message. If we do not provide a message, it will abort the commit. Hence commit message is a requirement. But there are ways to change that , but it is not recommended.
Once the commit is done, we see something like this
Now to make sure all are changes are staged & committed ,we use git status command
git status
Now to view our commits , we use
git log
As you can see , the HEAD (which is a pointer ) points to the master , which is the default branch that is created for the repo. We will see about branching later on.
In a nutshell
We will explore about reset as well as other important concepts on git on Github in the later articles. Stay tuned!
Cheers,
Linu