Getting started with Git
Avinash Reddy Arutla
Posted on April 28, 2022
Pre-requisites
- Basic Linux commands
- Able to navigate directories using terminal
So what is Git?
- Git is a DVCS that stores the data in a filesystem made up of snapshots. These snapshots are created when the commit command is used.
What is a DVCS?
Version Control snapshots the present code along with the changes when committed. These snapshots can later be retrieved in case any errors creep in.
In Distributed Version Control System, the same snapshots created by the Version control, are distributed among the authenticated users.
When the need arises to merge everything to a single codebase, these snapshots can be merged fast and effortlessly.
How to Git??
Git, natively is a set of command line utility programs that are designed to execute on Unix based Operating Systems.
There are various other implementations of GIT as GUI, but the the full functionality of GIT is only through the CLI.
Installing Git
Linux/Unix users
- Open your terminal, and install Homebrew with the following command
- Now install Git
Windows users
Go to Git Downloads, download the portable or the executable.
After the installation is complete, open Git Bash.
From now on, it's the same for both platforms.
Working with Git on a CLI
- Executing
git
will list out all the available git commands which can be permutated accordingly.
git init
- Initialising a Git repository
- Now, let us tell Git who we are
- Here,
Avinash
andavinasharutla@gmail.com
you can place your name and email address. These parameters are used in commit history, and we will understand about commits further.
- The above command will list all the globally defined parameters in Git.
- Now lets initialise a git repository
- I chose a folder named
git_chicha
for creating a git repository.
- Ohh, was that a mistake, now we'll remove git from the folder
- Now, we'll will create a file inside the git repository
- We can use a text editor or vim for the editing of the file.
- Now, after we enter the information in the file, lets stage it.
- When a particular file is staged, Git makes the file ready for committing into the Git timeline.
git add
- Now, lets check the status of the git repository
git commit
- After the file is staged, we need to commit the file into the Git timeline
- Committing is like taking a snapshot of the staged files with all the changes made.
- The command is as follows
- When the above command is used, vim editor is opened . Ā Ā I -> insert mode Ā Ā :wq -> exit vim
- We can also use the
git commit -m "commit message"
to skip using vim.
- So let's consider a case where you pushed a commit which is bad for the pipeline. In this case, we need to revert the commit.
git log
- Let us use the
git log
command to list all the previous commits inside the repo.
- In the above screenshot, one can see all the metadata about the commit, like the name and username. This data was entered through the
git config --global
command.
- Following is the screen when we reverted to the original commit.
Its amazing how version control works right?
Now, lets connect this offline repository to a online one.
For demonstration, here I'll be using GitHub.
Configuring Remote Repository
Let's first Visit GitHub.
Now, let's Sign in. I have an account, Also take a look at GitHub Student Developer Pack.
After signing in, on the leftmost part of the screen, let's click on the New button.
Now, let's choose a repo name which is catchy and relevant
.Now, head over to the end of the page, and click on "Create Repository".
After the creation of the repository, we can add files through browser upload or we can use Git for pushing the files.
Lets use git here.
Using Git to upload files to origin
- Let's head over to the terminal, and type the following command
git remote add origin https://github.com/burpcat/git_chicha.git
- Now to see the linked remote repo's to your local repo, we can use the command
- Now since everything like commits are wrapped up at our local repository, we hence put forth the legendary command!
Note : Here if you can use your own Personal Token to access Git, or sign in if that option exists
Note : The default branch name vary from user to user, some use master, some may use main, some may use default. You could refer this article.
After the command is used, this is how our GitHub Repo looks like.
- There are Git Branches again, which I will discuss in an another article.
Posted on April 28, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.