Git Demystified

adikejre

adikejre

Posted on April 5, 2023

Git Demystified

Git can seem to be an intimidating tool to use and especially for those starting out, it is easy to get lost in the complexities and the vast use cases which might lead you to wonder- Is it worth the headache?
In this blog, I will attempt to show a simple workflow on how to use git in a simplified way and cover basic commands and use cases. This will help in getting comfortable with using git and github and cover basic commands to get started with.

What is Git?

Git is a version control system which lets you to track all the changes made to a file system.
A repository is created and initialized which contains all the files. Git maintains a history of all changes made to these files over time. This allows you to view the changes made to the code, collaborate with other developers, revert to previous versions and essentially time travel between the different versions of the code base.

Creating a Github Repository

We can easily create a repository as shown below.

create repo

Once it is created, we can clone the repository on our local machine so that we can easily make changes in our files. For this we use the command: git clone

cloning
We copy the https key and do git clone as shown below using powershell

powershell

Great! so now we have created an empty repository and cloned that on our local system. You can see the .git folder present in the project's root directory.

.git folder
Now we can start adding files and making changes.

Making changes and pushing the code

Now in the directory where we have cloned, we can create a sample html file and add content.

html file

Now we need to accomplish the following:

  1. Add all our changes to the working tree so that we can push it
  2. Commit our changes to record all the changes we have made
  3. Push our changes to github

For this we use the following commands:

  • git add -A : This adds all our changes made in our directory

  • git commit -m "commit message" : This records the changes and adds a comment to the commit.

  • git status : To see all the changes which are staged but not yet pushed

  • git push : This uploads the changes to the remote repository which is in github

These commands are seen below in action:

commands

And here instantly we can see the file we created and all the changes reflected on our github repository.

Github

Git Branches

Creating separate branches to work on is very useful especially when working on different features which you want to keep separated. It is good practice to work on a branch instead of the main so that the main or master branch is protected and changes are only made to the required branch.

The following commands are used:

  • git branch <branch_name> : Creates a new branch with given name

  • git branch : Gives the name of the current branch

  • git checkout <branch_name> : Shifts to the given branch

branch

Thus it is easy to switch between branches and using different branches for different features is often useful.

We create a branch test1 and push the changes to it.

test1 branch
Note that when pushing to a new branch which does not have an upstream branch in github yet, we use git push --set-upstream origin test1

Now a friend wants to contribute to your project. This is easy to do as they can clone the repository in their system and create a branch and start working on their own.

branching

As shown above, the repository is cloned and test2 branch is created to work on.

This covers the basics of setting up a git project, and a workflow on the basics of git you need as a developer working on your personal project and wants to start with simple version control to make life easier. In the next posts, I will go into more detail on how to work collaboratively using git, make pull requests and cover other useful commands.

💖 💪 🙅 🚩
adikejre
adikejre

Posted on April 5, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Git Demystified
beginners Git Demystified

April 5, 2023

Install Git on Linux
beginners Install Git on Linux

December 10, 2021

Installing git on Mac
beginners Installing git on Mac

December 9, 2021

Installing git on windows
beginners Installing git on windows

December 8, 2021

GitHub tips for beginners
git GitHub tips for beginners

February 2, 2021